S1J0
S1J0

Reputation: 199

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?

Upvotes: 1

Views: 3930

Answers (2)

johnthagen
johnthagen

Reputation: 9169

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)

Upvotes: 1

Kenly
Kenly

Reputation: 26698

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

import os

os.system('sshpass  -p "password" scp local_file [email protected].?:/remotepath/remote_file')

Upvotes: 1

Related Questions