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

时间:2019-07-31 11:02:49

标签: python paramiko scp

我正在编写一个脚本,用于自动将文件传送到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中的行是否可以组织得更好?我感谢任何反馈!谢谢。

1 个答案:

答案 0 :(得分:2)

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

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

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

scp_client.put(src, remote_path=dst)