7

SSH 経由で他のボックスとの間でファイルをダウンロードおよびアップロードする、作成中のアプリケーションで問題が発生しています。私が経験している問題は、ファイルを正常に取得 (ダウンロード) できるが、ファイルを別のサーバーに配置 (アップロード) しようとすると EOFError() 例外が発生することです。paramiko\sftp.py の _write_all() を見たところ、ストリームにデータを書き込めなかったときにエラーが発生したように見えましたか? 私はネットワーク プログラミングの経験がないので、誰かがそれが何をしようとしているのかを知っていて、それを私に伝えることができれば、私はそれを感謝します.

接続を ssh() として処理する関数の単純化されたバージョンを作成しました。runCommand() はアプリケーションでアップロードが失敗する方法を示し、simpleTest() は sftp put がどのように機能するかを示しますが、SFTP オブジェクトの保存方法以外に runCommand() と simpleTest() の違いはわかりません。1 つはディクショナリに格納され、もう 1 つは単独で格納されます。辞書が問題で、ファイルのダウンロードがうまくいかないように思えますが、そうではありません。

誰かがこの動作の原因を知っているか、またはこの方法が問題を引き起こしている場合に接続を管理する別の方法を推奨できますか?

Paramiko 1.7.6 で Python 2.7 を使用しています。Linux と Windows の両方でこのコードをテストしましたが、同じ結果が得られました。

編集:コードが含まれています。

import os
import paramiko

class ManageSSH:
     """Manages ssh connections."""
    def __init__(self):
        self.hosts = {"testbox": ['testbox', 'test', 'test']}
        self.sshConnections = {}
        self.sftpConnections = {}
        self.localfile = "C:\\testfile"
        self.remotefile = "/tmp/tempfile"
        self.fetchedfile = "C:\\tempdl"

    def ssh(self):
        """Manages ssh connections."""
        for host in self.hosts.keys():
            try:
                self.sshConnections[host]
                print "ssh connection is already open for %s" % host
            except KeyError, e:         # if no ssh connection for the host exists then open one
                # open ssh connection
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect(self.hosts[host][0], 22, self.hosts[host][1], self.hosts[host][2])
                self.sshConnections[host] = ssh
                print "ssh connection to %s opened" % host
            try:
                self.sftpConnections[host]
                print "sftp connection is already open for %s" % host
            except KeyError, e:
                # open sftp connection
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect(self.hosts[host][0], 22, self.hosts[host][1], self.hosts[host][2])
                self.sftpConnections[host] = ssh.open_sftp()
                print "sftp connection to %s opened" % host

    def runCommand(self):
        """run commands and return output"""
        for host in self.hosts:
            command = "if [ -d /tmp ]; then echo -n 1; else echo -n 0; fi"
            stdin, stdout, stderr = self.sshConnections[host].exec_command(command)
            print "%s executed on %s" % (command, host)
            print "returned %s" % stdout.read()
            self.sftpConnections.get(self.remotefile, self.fetchedfile)
            print "downloaded %s from %s" % (self.remotefile, host)
            self.sftpConnections[host].put(self.localfile, self.remotefile)
            print "uploaded %s to %s" % (self.localfile, host)
            self.sftpConnections[host].close()
            self.sshConnections[host].close()

    def simpleTest(self):
        host = "testbox"
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(host, 22, 'test', 'test')
        sftp = ssh.open_sftp()
        print "sftp connection to %s opened" % host
        sftp.get(self.remotefile, self.fetchedfile)
        print "downloaded %s from %s" % (self.localfile, host)
        sftp.put(self.localfile, self.remotefile)
        print "uploaded %s to %s" % (self.localfile, host)
        sftp.close()

if __name__ == "__main__":
    test = ManageSSH()
    print "running test that works"
    test.simpleTest()
    print "running test that fails"
    test.ssh()
    test.runCommand()

出力:

running test that works
sftp connection to testbox opened
downloaded C:\testfile from testbox
uploaded C:\testfile to testbox
running test that fails
ssh connection to testbox opened
sftp connection to testbox opened
if [ -d /tmp ]; then echo -n 1; else echo -n 0; fi executed on testbox
returned 1
downloaded /tmp/tempfile from testbox
Traceback (most recent call last):
  File "paramikotest.py", line 71, in <module>
    test.runCommand()
  File "paramikotest.py", line 47, in runCommand
    self.sftpConnections[host].put(self.localfile, self.remotefile)
  File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 561, in put

    fr = self.file(remotepath, 'wb')
  File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 245, in open
    t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
  File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 627, in _request
    num = self._async_request(type(None), t, *arg)
  File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 649, in _async_request
    self._send_packet(t, str(msg))
  File "C:\Python27\lib\site-packages\paramiko\sftp.py", line 172, in _send_packet
    self._write_all(out)
  File "C:\Python27\lib\site-packages\paramiko\sftp.py", line 138, in _write_all

    raise EOFError()
EOFError
4

3 に答える 3

7

問題を解決できました。Paramiko.Transport を使用してから、 fromを使用する代わりにSFTPClientwithを作成することになっていました。paramiko.SFTPClient.from_transport(t)open_sftp()SSHClient()

次のコードが機能します。

t = paramiko.Transport((host, 22))  
t.connect(username=username, password=password)  
sftp = paramiko.SFTPClient.from_transport(t)
于 2011-08-12T14:19:22.337 に答える
2

私が見ているように、ssh=SSHClient() で SSHClient-Object を作成し、次に sftp=ssh.open_sftp() で sftp-object を作成します。sftp のみを使用したい場合は、ssh をローカル変数に格納すると、gc が実行されますが、ssh が gc の場合、sftp は魔法のように動作を停止します。理由はわかりませんが、sftp が存在する間は ssh を保存してみてください。

于 2013-02-24T18:32:25.473 に答える
0

あなたの編集を読んだ後、問題はここにあると思います

stdin, stdout, stderr = self.sshConnections[host].exec_command(command)

この行は明らかにftpのものを切断します

編集済み

于 2011-03-21T15:24:20.760 に答える