gpt4 book ai didi

python - 无法从 python3/paramiko 捕获多个异常

转载 作者:行者123 更新时间:2023-12-03 08:20:55 51 4
gpt4 key购买 nike

在 python3 中,我使用 multiprocessing.Pool 并行运行 paramiko 以连接到子网上的每个 ssh 设备。我得到了一系列我似乎无法捕获的错误。

我在这里放了各种 try/except 语句,但没有一个能捕捉到这个错误。我什至用 try/except 包装了 Pool 设置和语句,但那里没有任何改变。所有 paramiko 的东西都在 try_login 函数中,所以它应该来自那里,但所有这些都在 try/except 中,甚至还有一个通用的 except 应该得到所有东西。

我也尝试过组合导致此输出的异常。

#!/usr/bin/env python3

import paramiko
import socket
import ipaddress

network_address = '192.168.50.0/24'
username = ''
password = ''
timeout = 3
num_threads = 30

def trylogin(ipaddress):
global username, password
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ipaddress, username=username, password=password, timeout=timeout)
(stdin, stdout, stderr) = ssh.exec_command('cat /var/serial_number')
imei = stdout.readline()
if imei != '': print("[+] {}: Success! Found ".format(ipaddress))
return [ipaddress, imei]
except paramiko.AuthenticationException:
print("[-] {}: Authentication Exception!".format(ipaddress))
except paramiko.SSHException:
print("[-] {}: SSH Exception!".format(ipaddress))
except (paramiko.ssh_exception.SSHException, OSError, EOFError):
pass
except EOFError:
pass
except OSError:
pass
except paramiko.ssh_exception.NoValidConnectionsError:
pass
except paramiko.ssh_exception.SSHException:
pass
except Exception as e:
print('caught a new fish')
print(e)
pass
finally:
try:
ssh.close()
except:
pass
return

network = ipaddress.ip_network(network_address)
for ipaddr in network.hosts():
print('Checking {}'.format(str(ipaddr)))
trylogin(str(ipaddr))

当它运行时会发生大多数应该发生的事情。但是我收到一个错误,显示我认为已处理的两个异常。首先是 OSError,然后是 paramiko.ssh_exception.SSHException。我不明白为什么我不能捕获这些。
$ ./find_ssh.py
[-] 192.168.50.1 trying...
[-] 192.168.50.2 trying...
[-] 192.168.50.3 trying...
[-] 192.168.50.4 trying...
[-] 192.168.50.5 trying...
[-] 192.168.50.6 trying...
[-] 192.168.50.7 trying...
[-] 192.168.50.8 trying...
[-] 192.168.50.9 trying...
[-] 192.168.50.10 trying...
[-] 192.168.50.10: Authentication Exception!
[-] 192.168.50.11 trying...
[-] 192.168.50.12 trying...
[-] 192.168.50.13 trying...
[-] 192.168.50.14 trying...
[-] 192.168.50.14: SSH Exception!
[-] 192.168.50.15 trying...
Exception: Error reading SSH protocol banner[Errno 9] Bad file descriptor
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 2211, in _check_banner
buf = self.packetizer.readline(timeout)
File "/usr/local/lib/python3.5/dist-packages/paramiko/packet.py", line 380, in readline
buf += self._read_timeout(timeout)
File "/usr/local/lib/python3.5/dist-packages/paramiko/packet.py", line 607, in _read_timeout
x = self.__socket.recv(128)
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 2039, in run
self._check_banner()
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 2216, in _check_banner
"Error reading SSH protocol banner" + str(e)
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner[Errno 9] Bad file descriptor

[-] 192.168.50.16 trying...
[-] 192.168.50.17 trying...
[-] 192.168.50.18 trying...
[-] 192.168.50.19 trying...
[-] 192.168.50.20 trying...

编辑:修改为删除多处理调用。它更慢但更清晰。

最佳答案

这是一个丑陋的解决方法,而不是一个答案,但它使错误消失,这是有用的。

添加行:

paramiko.util.log_to_file("main_paramiko_log.txt", level = "INFO")

这样做的效果是创建了一个包含 paramiko 输出的文件。将级别从 INFO 更改为 WARN 或 ERR 很简单。这个特定的错误系列现在作为错误打印到日志文件中,不再打印到屏幕上。

显然 Uncaught Error 在 paramiko 中很常见,有 a variety of related issues发布在他们的github上,在发布之前我没有意识到这一点。

关于python - 无法从 python3/paramiko 捕获多个异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57192712/

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