之前写的脚本只能分发一个配置,每次分发多个配置总要执行很多次,很不爽,于是就有了这个脚本
from multiprocessing import Process
import paramiko
import sys
file = sys.argv[1::]
Username = "root"
Password = "123456"
Dest_Path = [
"/data/x5online/", #分发配置的目的地,这里测试只写了两个,可以自己加
"/tmp/"
]
Port = 22
def sftpPut(ip):
try:
s = paramiko.Transport((ip,Port))
s.connect(username=Username,password=Password)
sftp = paramiko.SFTPClient.from_transport(s)
for localFile in file:
for remoteFile in Dest_Path:
sftp.put(localFile,remoteFile + localFile)
print("%s put successful." %ip.strip())
except:
print("%s not exits."%ip.strip())
def ipProcess():
with open("ip.txt","r") as IP_file:
for ip in IP_file.readlines():
p = Process(target=sftpPut,args=(ip,))
p.start()
if __name__ == '__main__':
ipProcess()
其中ip.txt文件内容为:
192.168.170.129
192.168.170.150
192.168.170.143
用法:python "程序名称" 文件名1 文件名2 文件名3 文件名4
天天向上,空杯心态。