gpt4 book ai didi

python - "' SCPClient ' object has no attribute ' SCPException '"试图处理来自 Paramiko 的 SCP 模块的异常

转载 作者:太空宇宙 更新时间:2023-11-04 01:54:09 25 4
gpt4 key购买 nike

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

关于python - "' SCPClient ' object has no attribute ' SCPException '"试图处理来自 Paramiko 的 SCP 模块的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57289278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com