[英]Python: How to copy a file via SSH and paramiko without sftp
I want to copy a file in python(3.4) using the paramiko library. 我想使用paramiko库在python(3.4)中复制文件。
My approach: 我的方法:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(192.168.1.1, 22, root, root)
sftp = ssh.open_sftp()
sftp.put(local_file, remote_file)
sftp.close()
The error I get: 我得到的错误:
EOF during negotiation
The problem is that the connected system doesn't use sftp. 问题是连接的系统不使用sftp。
So is there a way to copy a file without using sftp? 那么有没有办法在不使用sftp的情况下复制文件?
You can use scp
to send files, and sshpass
to pass password. 您可以使用
scp
发送文件,并使用sshpass
传递密码。
import os
os.system('sshpass -p "password" scp local_file root@192.168.1.?:/remotepath/remote_file')
Use the built in paramiko.Transport
layer and create your own Channel
: 使用内置的
paramiko.Transport
图层并创建自己的Channel
:
with paramiko.SSHClient() as ssh:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.1.1', 22, 'root', 'root')
transport = ssh.get_transport()
with transport.open_channel(kind='session') as channel:
file_data = open('local_data', 'rb').read()
channel.exec_command('cat > remote_file')
channel.sendall(file_data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.