Lim*_*lla 3 ssh libssh2 python-3.2
我正在尝试通过 libssh2 的 no-ack 的 python byndings 将一组文件发送到远程服务器,但由于缺乏文档,我完全不知道库的使用情况。
我尝试使用 libssh2 的 C 文档但没有成功。
由于我使用的是 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()
Run Code Online (Sandbox Code Playgroud)
似乎工作正常。
小智 5
以下是如何在 Python 3.2 中使用 libssh2获取文件。非常感谢 no-Ack 向我展示了这一点。您需要 libssh2 的 Python3 绑定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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5093 次 |
| 最近记录: |