“'SCPClient' 对象没有属性 'SCPException'” 尝试处理来自 Paramiko 的 SCP 模块的异常时

"'SCPClient' object has no attribute 'SCPException'" when trying to handle an exception from SCP module for Paramiko

我正在编写用于自动将文件传送到 Cisco 设备的脚本,当设备未配置为 SCP 服务器时,我收到一个错误,导致我的整个脚本终止。

我已经为其他脚本终止异常添加了两个异常,但是当我尝试为这个 SCP 错误添加它时,我收到以下错误:

try:
    ssh_client =paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    print((time.strftime('%d-%b-%Y %H:%M:%S'))+" - Attempting SSH connection with "+ip)
    ssh_client.connect(hostname=ip,username=user,password=passw)
    print((time.strftime('%d-%b-%Y %H:%M:%S'))+" - Successfully established SSH connection with "+ip)
    scp = SCPClient(ssh_client.get_transport(), progress = progress)
    print((time.strftime('%d-%b-%Y %H:%M:%S'))+" - Transferring file to "+ip)
    scp.put(src, remote_path=dst)
    print((time.strftime('%d-%b-%Y %H:%M:%S'))+" - Successfully transferred "+src+" to "+ip)
    ssh_client.close()
    print((time.strftime('%d-%b-%Y %H:%M:%S'))+" - Closed SSH session with "+ip)
    print("###########")
    f = open("scpaudit.txt","a")
    f.write(time.strftime('%d-%b-%Y %H:%M:%S')+" "+user+" transferred "+src+" to "+ip)
    f.close()
except paramiko.ssh_exception.AuthenticationException:
    print("-> ERROR: Auth error for "+user+" on "+ip)
    print("#########")
    f = open("scpaudit.txt","a")
    f.write(time.strftime('%d-%b-%Y %H:%M:%S')+" ERROR: Authentication Error for "+user+" to "+ip)
    f.close()
except paramiko.ssh_exception.NoValidConnectionsError:
    print("-> ERROR: Unable to connect to "+ip)
    print("#########")
    f = open("scpaudit.txt","a")
    f.write(time.strftime('%d-%b-%Y %H:%M:%S')+" ERROR: Unable to connect to "+ip)
    f.close()

当我遇到没有启用 SCP 服务器的设备时,我收到以下错误:

Traceback (most recent call last):
  File "scpscript.py", line 100, in <module>
yes_or_no("I confirm that the above information is correct and I would like to proceed. ")
  File "scpscript.py", line 93, in yes_or_no
return session()
  File "scpscript.py", line 65, in session
scp.put(src, remote_path=dst)
  File "/usr/local/lib/python2.7/dist-packages/scp.py", line 158, in put
self._recv_confirm()
   File "/usr/local/lib/python2.7/dist-packages/scp.py", line 363, in _recv_confirm
raise SCPException(asunicode(msg[1:]))
scp.SCPException: Administratively disabled.

然后我将以下异常添加到末尾(ninja edit at 07:15 - 将错误的异常复制到 Whosebug):

except scp.SCPException:
    print("-> SCP administratively disabled on remote device "+ip)

...然后导致以下错误:

Traceback (most recent call last):
  File "scpscript.py", line 100, in <module>
yes_or_no("I confirm that the above information is correct and I would like to proceed. ")
  File "scpscript.py", line 93, in yes_or_no
return session()
  File "scpscript.py", line 85, in session
except scp.SCPException(asunicode(msg[:])):
AttributeError: 'SCPClient' object has no attribute 'SCPException'

没主意了。 try 中的行是否可以组织得更好?我感谢任何反馈!谢谢。

您在 scp 模块名称和您的本地 scp 变量之间存在冲突。

一种可能的解决方案是更改变量的名称。按照 ssh_client 模式,您可以使用 scp_client:

scp_client = SCPClient(ssh_client.get_transport(), progress = progress)

scp_client.put(src, remote_path=dst)