0

我需要使用 SFTP 上传一些文件,这可以从命令行工作:

$sftp myuser@my_remote_host
Connected to my_remote_host
sftp>

这是我的 Paramiko 脚本:

#!/usr/bin/env python
import paramiko
import sys
import os

host = "my_remote_host"
port = 22

transport = paramiko.Transport((host, port))

username = "myuser"      

LOCAL_PATH = "/tmp/"
REMOTE_PATH = "/dcs/tmp/"
FILE = "myfile"

transport.connect(username = username)

sftp = paramiko.SFTPClient.from_transport(transport)
path = LOCAL_PATH + FILE
sftp.put(LOCAL_PATH + FILE, REMOTE_PATH + FILE)

sftp.close()
transport.close()
print 'Upload done.'

执行时出现此错误:

No handlers could be found for logger "paramiko.transport"
Traceback (most recent call last):
  File "./my_sftp.py", line 19, in ?
    sftp = paramiko.SFTPClient.from_transport(transport)
  File "/usr/lib/python2.4/site-packages/paramiko/sftp_client.py", line 102, in from_transport
    chan = t.open_session()
  File "/usr/lib/python2.4/site-packages/paramiko/transport.py", line 655, in open_session
    return self.open_channel('session')
  File "/usr/lib/python2.4/site-packages/paramiko/transport.py", line 745, in open_channel
    raise e
EOFError

添加私钥时出现此错误:

path = os.path.join(os.environ['HOME'], '.ssh', 'id_dsa')
key = paramiko.DSSKey.from_private_key_file(path)
transport.connect(username = username, pkey=key)

Traceback (most recent call last):
  File "./my_sftp.py", line 24, in ?
    transport.connect(username = username, pkey=key)
  File "/usr/lib/python2.4/site-packages/paramiko/transport.py", line 1007, in connect
    self.auth_publickey(username, pkey)
  File "/usr/lib/python2.4/site-packages/paramiko/transport.py", line 1234, in auth_publickey
    return self.auth_handler.wait_for_response(my_event)
  File "/usr/lib/python2.4/site-packages/paramiko/auth_handler.py", line 174, in wait_for_response
    raise e
paramiko.AuthenticationException: Authentication failed.
4

1 回答 1

2

在您的第一个示例中,您不能仅使用用户名进行身份验证,因此无法启动会话。

如果没有更多信息,我无法说出为什么您的私钥示例不起作用。该服务器的密钥是否可能不正确?命令行上的 SSH 可能正在尝试多个密钥,或者从代理获取它。

SSHClient无论如何,从课堂开始会更容易。它将所有身份验证和主机验证部分打包在一个包中。它还有一个open_sftp()方便的方法来返回一个SFTPClient实例。

于 2010-11-18T16:37:51.640 回答