1. 程式人生 > >ubuntu16.04下使用python3:paramiko庫批量遠端傳檔案;執行命令

ubuntu16.04下使用python3:paramiko庫批量遠端傳檔案;執行命令

#!/usr/bin/env python3

-- coding:utf-8 --

import paramiko,fileinput

def scp_ssh(ip,mypath,topath):
t = paramiko.Transport((ip,22))
t.connect(username = ‘yunnao’, password = ‘123456’)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(mypath,topath)
t.close()
def command(ip,cm):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,22,'user,‘password’)
stdin, stdout, stderr = ssh.exec_command(cm)
# print(stdout.readlines())
ssh.close()
for x in fileinput.input(‘host.txt’):
print(x)
command(x,‘chmod -R 511

xxx.sh’)
scp_ssh(str(x), ‘/home/root/xxx.sh’, ‘/home/user_root/xxx.sh’)