2017-11-13 2 views
0

私は、リモートサーバーにファイルを送信するために、このメソッドを使用します。SSHClient paramiko Python?

def runSendArchive(host, port, username, password, remote_directory, archive): 
    try: 
     s = paramiko.SSHClient() 
     s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
     s.connect(host, username, password, port) 

     sftp = s.open_sftp() 
     sftp.put(archive, remote_directory) 
     print "3 - The file was uploaded via SSH!" 

    except (BadHostKeyException, AuthenticationException, SSHException, socket.error) as e: 
     print "4 - Error! The file was not uploaded: ", e 

それは私に例外を返します:

except (BadHostKeyException, AuthenticationException, SSHException, socket.error) as e: NameError: global name 

'BadHostKeyException' is not defined

このライブラリ権利を使用する方法は?

File "run.py", line 65, in runSendArchive 
    sftp.put(archive, remote_directory) 
    File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 721, in put 
    return self.putfo(fl, remotepath, file_size, callback, confirm) 
    File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 677, in putfo 
    with self.file(remotepath, 'wb') as fr: 
    File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 338, in open 
    t, msg = self._request(CMD_OPEN, filename, imode, attrblock) 
    File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 774, in _request 
    return self._read_response(num) 
    File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 826, in _read_response 
    self._convert_status(msg) 
    File "C:\Python27\lib\site-packages\paramiko\sftp_client.py", line 859, in _convert_status 
    raise IOError(text) 
IOError: Failure 

65ラインであるsftp.put(archive, remote_directory)

答えて

2

ラインから判断BadHostKeyExceptionがであるので、あなたはimport paramiko

を呼び出している

paramiko.SSHClient() 

は今、私は次のエラーを取得します、あなたは

from paramiko.ssh_exception import BadHostKeyException, AuthenticationException, SSHException 

http://docs.paramiko.org/en/2.3/api/ssh_exception.html

  • paramiko.ssh_exception.BadHostKeyException
  • paramiko.ssh_exception.AuthenticationException
  • paramiko.ssh_exception.SSHException

存在を参照を追加する必要がありますそのモジュールであなたのコード例については

は私が輸入を試みたあなたはrunSendArchive機能

import socket 
import paramiko 
from paramiko.ssh_exception import BadHostKeyException, AuthenticationException, SSHException 
+0

前に以下を追加する必要がありますスニペット: 'インポートparamiko.ssh_exception.AuthenticationException' – OPV

+0

それは私を与える:'インポートparamiko.ssh_exception.AuthenticationException ImportError:モジュール名がAuthenticationExceptionの場合 – OPV

+0

'paramiko.ssh_exceptionからインポートBadHostKeyException、AuthenticationException、SSHException' –

関連する問題