我正试图通过no-ack的python byndings为libssh2发送一组文件到远程服务器,但由于缺少文档,我完全迷失了库的使用。
我试过使用libssh2的C docks失败了。
由于我使用的是python 3.2,paramiko和pexpect是不可能的。有人可以帮忙吗?
编辑:我刚刚在他的帖子中找到了no-Ack的博客评论中的一些代码。
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()
似乎工作正常。
以下是如何在Python 3.2中使用libssh2获取文件。对于no-Ack的主要荣誉是向我展示这一点。你需要libssh2 https://github.com/wallunit/ssh4py的Python3绑定
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()
要使用Paramiko库在Python中执行此操作(即不通过subprocess.Popen或类似方法包装scp)。
相关:qazxsw poi
下面很容易,但它不是通用的意味着如果你运行在linux dosent工作,如果你在Windows中运行。告诉我你是否知道在所有O.S平台上制作以下普遍性。
https://stackoverflow.com/a/69596/1270589