简体   繁体   中英

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.

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.

So is there a way to copy a file without using sftp?

You can use scp to send files, and sshpass to pass password.

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 :

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM