Before the online server distribution configuration is the shell and expect script distribution, the script is written for a long time, last week changed the Ansible, and now write one in Python, 30 lines of code to achieve the requirements, the previous Shell wrote a fast 200 lines, egg pain, the code is as follows:
From multiprocessing import processimport paramikoimport sysfile = sys.argv[1]username = "root" Password = "123456" Dest_pa th = ["/data/x5online/%s"%file, "/tmp/%s"%file]port = 22def 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,rem Otefile) 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 ()
Chicken soup Once, interested in Python, learn quickly, like last year to Linux is very obsessed with, a year, now I am a game company's operation and maintenance leader. Come on
Python's brevity is not a substitute for the shell.