Paramiko 读取 SSH 协议横幅时出错
我在处理此错误时遇到问题:“读取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,问题在于你永远不会关闭或断开连接。因此,您必须使用
try
和finally
语句断开每个连接有关更多信息,请阅读 文档
注意:
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
andfinally
statementFor more information read the documentation
Note: The
finally
block is always called, regardless of whether an exception is generated