python3实现SCP上传下载文件

原文参考

https://stackoverflow.com/questions/250283/how-to-scp-in-python

安装依赖包

执行pip3 install scp paramiko通过pip安装scpparamiko模块

python3实现SCP上传下载

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')

你可能感兴趣的:(Python,python,开发语言,scp,经验分享,程序人生)