上传文件
import paramiko
#上传文件
def sftp_upload(ip,port,user,pwd):
client=paramiko.Transport((ip,port))
client.connect(username=user,password=pwd)
sftp=paramiko.SFTPClient.from_transport(client)
remote_path="/root"
local_path="C:/Users/xiaoqingdu/Desktop/"
sftp.put(local_path,remote_path)
client.close()
下载文件
#远程下载文件
def sftp_upload(ip,port,user,pwd):
client=paramiko.Transport((ip,port))
client.connect(username=user,password=pwd)
sftp=paramiko.SFTPClient.from_transport(client)
remote_path="/b_iscsi"
local_path="C:/Users/xiaoqingdu/Desktop"
client.close()
远程上传文件夹
#递归远程上传文件夹 def sftp_upload_dir(ip,port,user,pwd): remote_path = "/b_iscsi" local_path = "C:/Users/xiaoqingdu/Desktop/test" client=paramiko.Transport((ip,port)) client.connect(username=user,password=pwd) sftp=paramiko.SFTPClient.from_transport(client) print('upload file start %s'%datetime.datetime.now()) for root,dirs,files in os.walk(local_path): print('[%s][%s][%s]'%(root,dirs,files)) for filespath in files: local_file=os.path.join(root,filespath) print(11,'[%s][%s][%s][%s]'%(root,filespath,local_file,local_path)) a=local_file.replace(local_path,'').replace('\\','/').lstrip('/') print('01',a,'[%s]'%remote_path) remote_file=os.path.join(remote_path,a).replace('\\','/') print(22,remote_file) try: sftp.put(local_file,remote_file) except Exception as e: sftp.mkdir(os.path.split(remote_file)[0]) sftp.put(local_file,remote_file) print("66 upload %s to remote %s"%(local_file,remote_file)) for name in dirs: local_dir=os.path.join(root,name) print(0,local_dir,local_path) a=local_dir.replace(local_path,'').replace('\\','/').lstrip('/') print(1,a) print(1,remote_path) remote_dir=remote_path+a print(33,remote_dir) try: sftp.mkdir(remote_dir) print(44,"mkdir path %s"%remote_path) except Exception as e: print(55,e) print('77,upload file sucess %s'%datetime.datetime.now()) client.close()