python的简洁是shell无法代替的

时间:2021-02-17 21:08:56

之前线上服务器分发配置都是用shell和expect脚本分发,脚本写了很长,上周换了ansible,现在自己用python写一个,就30行代码就可以实现需求,之前的shell写了快200行了,蛋疼,代码如下:

from multiprocessing import Process
import paramiko
import sys
file = sys.argv[1]
Username = "root"
Password = "123456"
Dest_Path = [ "/data/x5online/%s" %file,
"/tmp/%s" %file ]
Port = 22
def sftpPut(ip):
try:
s = paramiko.Transport((ip,Port))
s.connect(username=Username,password=Password)
sftp = paramiko.SFTPClient.from_transport(s)
localFile = file
for remoteFile in Dest_Path: sftp.put(localFile,remoteFile)
print("%s put successful." %ip)
except:
print("%s not exits."%ip)
def ipProcess():
for i in range(10,40):
ip = '192.168.170.%s'%i
p = Process(target=sftpPut,args=(ip,))
p.start()
if __name__ == '__main__':
ipProcess()

鸡汤一回,对python感兴趣了,学的才快。