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'

จะใช้ห้องสมุดนี้ได้อย่างไร?

ตอนนี้ฉันได้รับข้อผิดพลาดดังต่อไปนี้:

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)


person OPV    schedule 13.11.2017    source แหล่งที่มา


คำตอบ (1)


พิจารณาจากบรรทัด

paramiko.SSHClient()

คุณกำลังโทรมา import paramiko

เนื่องจาก BadHostKeyException อยู่ใน paramiko.ssh_exception คุณต้องเพิ่ม

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_Exceptionion.AuthenticationException
  • paramiko.ssh_Exception.SSHException

อยู่ในโมดูลนั้น

สำหรับข้อมูลโค้ดตัวอย่างของคุณ คุณจะต้องเพิ่มส่วนต่อไปนี้ก่อนฟังก์ชัน runSendArchive

import socket
import paramiko
from paramiko.ssh_exception import BadHostKeyException, AuthenticationException, SSHException
person Daniel F    schedule 13.11.2017
comment
ฉันลองนำเข้าแล้ว: import paramiko.ssh_exception.AuthenticationException - person OPV; 14.11.2017
comment
มันทำให้ฉัน: ` นำเข้า paramiko.ssh_ยกเว้น.AuthenticationException ImportError: ไม่มีโมดูลชื่อ AuthenticationException` - person OPV; 14.11.2017
comment
โปรดดูคำถามอีกครั้ง - person OPV; 14.11.2017
comment
stackoverflow.com/questions/3091326/put-in-sftp -using-paramiko --› remote_directory ควรเป็น remote_path (รวมถึงชื่อไฟล์ใหม่บนเซิร์ฟเวอร์ เช่น /home/user/upload.txt) - person Daniel F; 14.11.2017