python - 尝试为 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
是否可以更好地组织线路?我很感激任何反馈!谢谢你。
解决方案
scp
您的模块名称和本地scp
变量之间存在冲突。
一种可能的解决方案是更改变量的名称。按照ssh_client
模式,您可以使用scp_client
:
scp_client = SCPClient(ssh_client.get_transport(), progress = progress)
scp_client.put(src, remote_path=dst)
推荐阅读
- c# - 当我没有引用它时,“无法加载文件或程序集 Oracle.ManageddataAccess”
- jquery - 如何在具有多个类的表中按计数重命名表行
- python-3.x - 可选择使用 jit 将参数传递给另一个函数
- java - 模棱两可的方法调用模拟 RestTemplate.exchange()
- objective-c - 如何在 dispatch_async 内将 NSInteger __block 变量设置为 comboboxFilter.indexOfSelectedItem
- arrays - 将数字列表输入求和公式MATLAB
- javascript - 如何将数字添加到溢出包含的图像
- openshift - 在 OpenShift 的 Restricted SCC 中以另一个用户身份运行进程
- symfony - Symfony ChoiceType 在下拉列表中返回错误的值
- linux - 使用来自另一个文件的随机数据更新源文件中的数据