我想使用 paramiko 将文件传输到 dropbear ssh 服务器。我使用这个文件 (ssh_own.py):
#!/usr/bin/python3.6
import paramiko
import paramiko
from paramiko import client
class ssh:
client = None
def __init__(self, address, username, password):
print("Connecting to server.")
self.client = client.SSHClient()
self.client.set_missing_host_key_policy(client.AutoAddPolicy())
self.client.connect(address,
username = username,
password = password,
look_for_keys=False)
def sendCommand(self,
command):
if(self.client):
stdin, stdout, stderr = self.client.exec_command(command)
output = []
while not stdout.channel.exit_status_ready():
portion = stdout.readlines()
# print(portion)
if len(portion) > 0:
output.append(portion)
result = self.output_to_string(output)
return result
else:
raise Exception("Connection not opened.")
def output_to_string(self, output):
result = ""
for line in output:
for el in line:
# result += str(line, "utf8")
result += el
return result
还有另外一个小文件用于发出请求(test.py):
#!/usr/bin/python3.6
import ssh_own
import os
home = os.environ["HOME"]
ssh_client = ssh_own.ssh("ip", "username", "password")
ftp_client = ssh_client.client.open_sftp()
ftp_client.put("/home/localuser/README.md", "/home/username/README.md")
ftp_client.close()
当我运行 ssh_own.py 时,出现此错误:
Connecting to server.
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/paramiko/sftp_client.py", line 103, in __init__
server_version = self._send_version()
File "/usr/local/lib/python3.6/dist-packages/paramiko/sftp.py", line 107, in _send_version
t, data = self._read_packet()
File "/usr/local/lib/python3.6/dist-packages/paramiko/sftp.py", line 174, in _read_packet
x = self._read_all(4)
File "/usr/local/lib/python3.6/dist-packages/paramiko/sftp.py", line 161, in _read_all
raise EOFError()
EOFError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./test.py", line 13, in <module>
ftp_client = ssh_client.client.open_sftp()
File "/usr/local/lib/python3.6/dist-packages/paramiko/client.py", line 521, in open_sftp
return self._transport.open_sftp_client()
File "/usr/local/lib/python3.6/dist-packages/paramiko/transport.py", line 980, in open_sftp_client
return SFTPClient.from_transport(self)
File "/usr/local/lib/python3.6/dist-packages/paramiko/sftp_client.py", line 140, in from_transport
return cls(chan)
File "/usr/local/lib/python3.6/dist-packages/paramiko/sftp_client.py", line 105, in __init__
raise SSHException('EOF during negotiation')
paramiko.ssh_exception.SSHException: EOF during negotiation
有人知道是否可以从 paramiko 将文件传输到 dropbear 服务器吗?还是只是不兼容?我还在另一台运行 openssh 的 Ubuntu 机器上测试了这一点,它运行良好。
答案1
事实证明我必须安装 stfp,因为 dropbear 似乎没有这个,而 paramiko 使用 sftp 进行文件传输。scp 与 dropbear 配合得很好,然而,这正是让我感到困惑的地方。安装 sftp 后,使用 paramiko 进行的所有文件传输都正常。