def upload_to_linux(local_dir = LOCAL_DIR, remote_dir = REMOTE_DIR):
try:
t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
print('Upload file start %s ' % datetime.datetime.now())
for root, dirs, files in os.walk(local_dir):
for filespath in files:
local_file = os.path.join(root, filespath)
print('待上传的本地文件:%s' % local_file)
a = local_file.replace(local_dir, '').replace('\\', '/').lstrip('/')
remote_file = os.path.join(remote_dir, a)
print('远程目的地文件:%s' % 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("Upload %s to remote %s" % (local_file, remote_file))
for name in dirs:
local_path = os.path.join(root, name)
print(0, local_path, local_dir)
a = local_path.replace(local_dir, '').replace('\\', '')
print(1, a)
print(1, remote_dir)
remote_path = os.path.join(remote_dir, a)
print(33, remote_path)
try:
sftp.mkdir(remote_path)
print(44, "mkdir path %s" % remote_path)
except Exception as e:
print(55, e)
print('Upload file success %s ' % datetime.datetime.now())
t.close()
except Exception as e:
print(88, e)