1

I use this method to send file to remote server:

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

It returns me an exception:

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

'BadHostKeyException' is not defined

How to use this library right?

Now I get the following error:

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 line is sftp.put(archive, remote_directory)

1 Answer 1

4

Judging from the line

paramiko.SSHClient()

you are calling import paramiko

Since BadHostKeyException is in paramiko.ssh_exception, you need to add an

from paramiko.ssh_exception import BadHostKeyException, AuthenticationException, SSHException

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

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

reside in that module.

For your example code snippet you would need to add the following before the runSendArchive function

import socket
import paramiko
from paramiko.ssh_exception import BadHostKeyException, AuthenticationException, SSHException
Sign up to request clarification or add additional context in comments.

7 Comments

I tried import: import paramiko.ssh_exception.AuthenticationException
It gives me: ` import paramiko.ssh_exception.AuthenticationException ImportError: No module named AuthenticationException`
from paramiko.ssh_exception import BadHostKeyException, AuthenticationException, SSHException
NameError: global name 'socket' is not defined
import socket
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.