Paramiko 读取 SSH 协议横幅时出错

发布于 2025-01-09 19:57:58 字数 2411 浏览 0 评论 0原文

我在处理此错误时遇到问题:“读取 SSH 协议横幅时出错”+ str(e) paramiko.ssh_exception.SSHException:读取 SSH 协议横幅时出错。

这是我正在尝试运行的代码:

from netmiko import ConnectHandler

from netmiko.ssh_exception import  NetMikoTimeoutException
from paramiko.ssh_exception import SSHException



ip_list = ['192.168.1.77','192.168.1.73','192.168.1.75','192.168.1.74']
for ip in ip_list:
    cisco = {
        'device_type': 'cisco_ios',
        'host':   ip,
        'username': 'gianluca',
        'password': 'gianluca',
        'secret': 'gianluca',
        "fast_cli": False
    }
    try:
        net_connect = ConnectHandler(**cisco)
    except NetMikoTimeoutException:
        print(f'Machine with ip address {ip} is not reachable')
        continue

    except SSHException:
    
        print(f'Ssh not enabled on Machine with ip address {ip}')
        continue

    except:
        print(f'Oops, something went wrong on the Machine with ip address {ip}!')
        continue

    net_connect.enable()   
    net_connect.send_config_from_file('bbb.txt')
    print(f'Machine with ip address {ip} was configured correctly')

但是当我到达一台没有启用 ssh 的机器时,我收到一条大的红色错误消息,如下所示:

Exception: Error reading SSH protocol banner
Traceback (most recent call last):
  File "C:\Users\gianluca.platania\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\transport.py", line 2211, in _check_banner
    buf = self.packetizer.readline(timeout)
  File "C:\Users\gianluca.platania\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\packet.py", line 380, in readline
    buf += self._read_timeout(timeout)
  File "C:\Users\gianluca.platania\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\packet.py", line 609, in _read_timeout
    raise EOFError()
EOFError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\gianluca.platania\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\transport.py", line 2039, in run
    self._check_banner()
  File "C:\Users\gianluca.platania\AppData\Local\Programs\Thonny\lib\site-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

我试图不让它出现在处理异常,但似乎没有任何作用。有什么想法吗?

PS:我在 thonny 上也遇到这个错误: 未安装“paramiko.ssh_exception”的库存根(或与 Python 3.7 不兼容)

I'm having issues handling this error: "Error reading SSH protocol banner" + str(e)
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner.

this is the code I'm trying to run:

from netmiko import ConnectHandler

from netmiko.ssh_exception import  NetMikoTimeoutException
from paramiko.ssh_exception import SSHException



ip_list = ['192.168.1.77','192.168.1.73','192.168.1.75','192.168.1.74']
for ip in ip_list:
    cisco = {
        'device_type': 'cisco_ios',
        'host':   ip,
        'username': 'gianluca',
        'password': 'gianluca',
        'secret': 'gianluca',
        "fast_cli": False
    }
    try:
        net_connect = ConnectHandler(**cisco)
    except NetMikoTimeoutException:
        print(f'Machine with ip address {ip} is not reachable')
        continue

    except SSHException:
    
        print(f'Ssh not enabled on Machine with ip address {ip}')
        continue

    except:
        print(f'Oops, something went wrong on the Machine with ip address {ip}!')
        continue

    net_connect.enable()   
    net_connect.send_config_from_file('bbb.txt')
    print(f'Machine with ip address {ip} was configured correctly')

but when I reach a machine that doesn't have ssh enabled I get a large red error message, which is the following:

Exception: Error reading SSH protocol banner
Traceback (most recent call last):
  File "C:\Users\gianluca.platania\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\transport.py", line 2211, in _check_banner
    buf = self.packetizer.readline(timeout)
  File "C:\Users\gianluca.platania\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\packet.py", line 380, in readline
    buf += self._read_timeout(timeout)
  File "C:\Users\gianluca.platania\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\packet.py", line 609, in _read_timeout
    raise EOFError()
EOFError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\gianluca.platania\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\transport.py", line 2039, in run
    self._check_banner()
  File "C:\Users\gianluca.platania\AppData\Local\Programs\Thonny\lib\site-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

I'm trying to not make this appear handling the exception, but it seems like nothing works. Any idea?

PS: I also get this error on thonny:
Library stubs not installed for "paramiko.ssh_exception" (or incompatible with Python 3.7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉殇 2025-01-16 19:57:58

在我看来,问题在于你永远不会关闭或断开连接。因此,您必须使用 tryfinally 语句断开每个连接

net_connect = None
try:
   net_connect = ConnectHandler(**cisco)
   
finally:
   if net_connect:
       net_connect.disconnect()

有关更多信息,请阅读 文档

注意:finally 块总是被调用,无论是否产生异常

In my opinion the problem is that you never close or disconnect the connection. So you have to disconnect each connection with a try and finally statement

net_connect = None
try:
   net_connect = ConnectHandler(**cisco)
   
finally:
   if net_connect:
       net_connect.disconnect()

For more information read the documentation

Note: The finally block is always called, regardless of whether an exception is generated

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文