您好,登錄后才能下訂單哦!
單臺(tái)登錄:
#! /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通過(guò)密鑰文件登陸ssh和聽(tīng)過(guò)sftp上傳文件
Python使用paramiko庫(kù)遠(yuǎn)程安全連接SSH
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。