在Python中scp文件的最pythonic方法是什么?我所知道的唯一途径是
os.system('scp "%s" "%s:%s"' % (localfile, remotehost, remotefile) )
Run Code Online (Sandbox Code Playgroud)
这是一个hack,并且在类似Linux的系统之外不起作用,并且需要Pexpect模块的帮助以避免密码提示,除非您已经将无密码SSH设置到远程主机.
我知道Twisted的conch,但我宁愿避免通过低级ssh模块自己实现scp.
我知道paramiko,一个支持ssh和sftp的Python模块; 但它不支持scp.
背景:我正在连接到不支持sftp但支持ssh/scp的路由器,所以sftp不是一个选项.
编辑:这是如何使用SCP或SSH将文件复制到Python中的远程服务器?. 但是,这个问题没有给出一个scp特定的答案来处理python中的键.我希望有一种运行代码的方式
import scp
client = scp.Client(host=host, user=user, keyfile=keyfile)
# or
client = scp.Client(host=host, user=user)
client.use_system_keys()
# or
client = scp.Client(host=host, user=user, password=password)
# and then
client.transfer('/etc/local/filename', '/etc/remote/filename')
Run Code Online (Sandbox Code Playgroud)