实现方案
pycharm新建文件upload_file.py,运行即可
upload_file.py文件内容如下
# !/usr/bin/env python
# -*- coding: UTF-8 -*-
import paramiko
import datetime
import os
hostname = '***.***.***.**' #upload IP addr
username = '****'
password = '****'
port = 22
def upload(local_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):
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_dir))
a = local_file.replace(local_dir, '').replace('\\', '/').lstrip('/')
print('01', a, '[%s]' % remote_dir)