Use Paramiko upload directory, focus on the process of uploading, think of a morning to think out, may be a little wonderful, but still realize this function
#!/usr/bin/pythonImportParamikoImportOSdefUpload_dir (Ip,port,username,password,local_dir,remote_dir): Paramiko.util.log_to_file ("Paramiko.log") Trans=Paramiko. Transport ((Ip,port)) Trans.connect (username=username,password=password) sftp=Paramiko. Sftpclient.from_transport (trans)Try: Sftp.mkdir (remote_dir)exceptexception,e:Passa=os.path.join (Remote_dir,local_dir.split ("/") [-1]) Sftp.mkdir (a) forRoot, subdir, filesinchOs.walk (local_dir): forDirinchSubdir:r_d= Local_dir.split ("/") [:-1] Local_subdir=Os.path.join (root,dir) l_d= Local_subdir.split ("/") R_m=L_d[len (r_d): R_m="/". Join (r_m) Remote_subdir=Os.path.join (remote_dir,r_m)Printremote_subdir Sftp.mkdir (remote_subdir) forFileinchFiles:local_dir_path=Os.path.join (root,file) l_d_p= Local_dir_path.split ("/") r_d_p=L_d_p[len (r_d): R_d_p="/". Join (r_d_p) Remote_dir_path=Os.path.join (remote_dir,r_d_p)PrintRemote_dir_path sftp.put (local_dir_path,remote_dir_path)defMain (): IP="10.2.0.137"Port= 22username="Root"Password="123456"Local_dir="/lll/api"Remote_dir="/KKKKKKKKKKKK"Upload_dir (Ip,port,username,password,local_dir,remote_dir)if __name__=="__main__": Main ()
Paramiko Implementation Upload directory