How to send a file using scp using python 3.2?
Asked Answered
D

3

3

I'm trying to send a group of files to a remote server through no-ack's python byndings for libssh2, but I am totally lost regarding the library usage due to the lack of documentation.

I've tried using the C docs for libssh2 unsuccesfully.

Since I'm using python 3.2, paramiko and pexpect are out of the question. Anyone can help?

EDIT: I just found some code in no-Ack's blog comments to his post.

import libssh2, socket, os

SERVER = 'someserver'
username = 'someuser'
password = 'secret!'

sourceFilePath = 'source/file/path'
destinationFilePath = 'dest/file/path'

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((SERVER, 22))

session = libssh2.Session()
session.startup(sock)

session.userauth_password(username, password)

sourceFile = open(sourceFilePath, 'rb')

channel = session.scp_send(destinationFilePath, 0o644, os.stat(sourceFilePath).st_size)

while True:
    data = sourceFile.read(4096)
    if not data:
        break
    channel.write(data)

exitStatus = channel.exit_status()
channel.close()

Seems to work fine.

Detergent answered 16/1, 2013 at 20:15 Comment(3)
Can you share code examples of what you've tried?Reproachful
I just found out some example code in @no-ack's page. Seems to work fine.Detergent
please include the code as an answer to your question and mark it as correct. Otherwise lots of people will read this question wanting to help and you will just be wasting their time.Platform
F
5

And here's how to get files with libssh2 in Python 3.2. Major kudos to no-Ack for showing me this. You'll need the Python3 bindings for libssh2 https://github.com/wallunit/ssh4py

import libssh2, socket, os

SERVER = 'someserver'
username = 'someuser'
password = 'secret!'

sourceFilePath = 'source/file/path'
destinationFilePath = 'dest/file/path'


sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((SERVER, 22))

session = libssh2.Session()
session.startup(sock)

session.userauth_password(username, password)
(channel, (st_size, _, _, _)) = session.scp_recv(sourceFilePath, True)

destination = open(destinationFilePath, 'wb')

got = 0
while got < st_size:
    data = channel.read(min(st_size - got, 1024))
    got += len(data)
    destination.write(data)

exitStatus = channel.get_exit_status()
channel.close()
Formal answered 2/5, 2013 at 14:47 Comment(0)
O
0

To do this in Python (i.e. not wrapping scp through subprocess.Popen or similar) with the Paramiko library.

Revelent : https://mcmap.net/q/120286/-how-to-copy-a-file-to-a-remote-server-in-python-using-scp-or-ssh

Orgasm answered 24/1, 2017 at 19:30 Comment(1)
Thx for the answer. At the time Paramiko wasn't available for Python 3.Detergent
B
0

Below is easy but it is not universal means works if you run in linux dosent work if you run in windows. tell me if you know to make below universal i.e across all O.S platforms.

import os

os.system("sshpass -p 'your password' scp /opt/pysftp_server.txt [email protected]:/home")
Boatright answered 15/9, 2018 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.