https://stackoverflow.com/questions/250283/how-to-scp-in-python
执行pip3 install scp paramiko
通过pip安装scp
和paramiko
模块
import paramiko
from scp import SCPClient
def createSSHClient(server, port, user, password):
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(server, port, user, password)
return client
# 通过服务器地址,端口,用户,密码创建ssh会话
ssh = createSSHClient(server, port, user, password)
# 上传文件,将本地test.txt上传到远程test2.txt, 可以替换成变量
with SCPClient(ssh.get_transport()) as scp:
scp.put('test.txt', 'test2.txt')
# 下载文件, 将远程test2.txt下载到本地test.txt
with SCPClient(ssh.get_transport()) as scp:
scp.get('test.txt', 'test2.txt')