首页 > 解决方案 > 尝试为 Paramiko 处理来自 SCP 模块的异常时,“'SCPClient' 对象没有属性 'SCPException'”

问题描述

我正在编写一个脚本,用于自动将文件传送到 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.

因此,我将以下异常添加到末尾(忍者在 07:15 编辑 - 将错误的异常复制到 stackoverflow):

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是否可以更好地组织线路?我很感激任何反馈!谢谢你。

标签: pythonparamikoscp

解决方案


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

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

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

scp_client.put(src, remote_path=dst)

推荐阅读