單臺(tái)登錄:
創(chuàng)新互聯(lián)專注于迪慶州企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城建設(shè)。迪慶州網(wǎng)站建設(shè)公司,為迪慶州等地區(qū)提供建站服務(wù)。全流程按需規(guī)劃網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
#! /bin/bash
# -*- coding: utf-8 -*-
import paramiko
ssh = paramiko.SSHClient()
key = paramiko.AutoAddPolicy()
ssh.set_missing_host_key_policy(key)
pkey = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa')
ssh.connect('103.11.30.2', 22, 'root', pkey ,timeout=5)
stdin, stdout, stderr = ssh.exec_command('ls')
print(stdout.read().decode())
print(stderr.read())
# 關(guān)閉連接
ssh.close()
逐行讀取多個(gè)IP多臺(tái)登陸,如下
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time,shutil,os
import paramiko
appname = "root"
ssh = paramiko.SSHClient()
key = paramiko.AutoAddPolicy()
ssh.set_missing_host_key_policy(key)
pkey = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa')
uptime = time.strftime("%Y%m%d")
warpath="/var/tmp/cslc/aw/update/"+uptime
def update_all_aw():
for line in open("awip.conf"):
print line
ssh.connect(line, 22, appname, pkey ,timeout=5)
stdin, stdout, stderr = ssh.exec_command('python /var/tmp/awpy/createcopy.py')
print(stdout.read().decode())
print(stderr.read())
ssh.close()
print("+++++++下一個(gè)++++++")
return
awip.conf文件防止多臺(tái)IP地址即可。
參考:
python密鑰登錄主機(jī)
pythonparamiko通過密鑰文件登陸ssh和聽過sftp上傳文件
Python使用paramiko庫遠(yuǎn)程安全連接SSH