You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.3 KiB

5 years ago
import os
import paramiko
import subprocess
from shutil import copy2
xxxpath="xxx/xxx"
5 years ago
# RSA格式私钥可以被paramiko使用执行转换命令ssh-keygen -p -m PEM -t RSA -f ~/.ssh/copyid_rsa
5 years ago
key_path = 'xxx/.ssh/copyid_rsa'
# 定义上传函数,paramiko通过公钥免密连接
5 years ago
def upload(root_path, privateKey):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname='服务器ip', port='端口',
username='用户', key_filename=privateKey, allow_agent=True)
sftp = client.open_sftp()
# 利用os的walk函数获取本地目录文件列表
for dirspath, dirsname, files in os.walk(xxxpath):
root_linux = dirspath.replace('\\', '/')
5 years ago
# sftp的mkdir函数不支持创建多级目录固使用ssh连接后linux原生命令创建
remote_path = os.path.join('xxx/path', root_linux[29:])
5 years ago
stdin, stdout, stderr = client.exec_command(
5 years ago
''.join(['mkdir -p ' + remote_path]))
for file in files:
upload_file = os.path.join(root_linux, file).replace('\\', '/')
print(u'Put files...' + upload_file)
sftp.put(upload_file, os.path.join(
remote_path, file).replace('\\', '/'))
5 years ago
client.close()
5 years ago
sftp.close()
upload(xxxpath, key_path)