ktbyers / netmiko

Multi-vendor library to simplify Paramiko SSH connections to network devices
MIT License
3.63k stars 1.31k forks source link

netmiko.exceptions.NetmikoTimeoutException: Paramiko: 'No existing session' error: try increasing 'conn_timeout' to 15 seconds or larger. #3386

Open SaeedFazlollahzadeh opened 9 months ago

SaeedFazlollahzadeh commented 9 months ago

Description of Issue/Question

Note: Please check https://guides.github.com/features/mastering-markdown/ to see how to properly format your request.

Setup

Netmiko version

(Paste verbatim output from pip freeze | grep netmiko between quotes below)

netmiko==4.3.0

Netmiko device_type (if relevant to the issue)

(Paste device_type between quotes below)

There's no issue with device_type.

Steps to Reproduce the Issue

  1. This is my python code if I use password authentication:
    
    from netmiko import ConnectHandler

cisco1 = { "device_type": "cisco_ios", "host": "192.168.1.1", "username": "myUserName", 'password': 'myPassword', 'timeout': 60 }

with ConnectHandler(**cisco1) as net_connect: output = net_connect.send_command("show ip arp")

print(output)

Output:

Protocol Address Age (min) Hardware Addr Type Interface Internet 192.168.2.2 14 aaaa.aaaa.aaaa ARPA GigabitEthernet1/0/1 Internet 192.168.2.1 - bbbb.bbbb.bbbb ARPA GigabitEthernet1/0/1

But this is my code if I use key authentication:

from netmiko import ConnectHandler

key_file = "/root/.ssh/id_rsa" cisco1 = { "device_type": "cisco_ios", "host": "192.168.1.1", "username": "myUserName", "use_keys": True, "key_file": key_file, 'timeout': 60 }

with ConnectHandler(**cisco1) as net_connect: output = net_connect.send_command("show ip arp")

But I can SSH via bash CLI with no issue and it works fine.

This is my `/root/.ssh/confg` file:

Host switch HostName 192.168.1.1 User myUserName IdentityFile /root/.ssh/id_rsa KexAlgorithms +diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1 Ciphers aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc


### Error Traceback
(Paste the complete traceback of the exception between quotes below)

Traceback (most recent call last): File "/home/python3.10/venv/lib/python3.10/site-packages/netmiko/base_connection.py", line 1137, in establish_connection self.remote_conn_pre.connect(**ssh_connect_params) File "/home/python3.10/venv/lib/python3.10/site-packages/paramiko/client.py", line 485, in connect self._auth( File "/home/python3.10/venv/lib/python3.10/site-packages/paramiko/client.py", line 818, in _auth raise saved_exception File "/home/python3.10/venv/lib/python3.10/site-packages/paramiko/client.py", line 794, in _auth self._transport.auth_publickey(username, key) File "/home/python3.10/venv/lib/python3.10/site-packages/paramiko/transport.py", line 1664, in auth_publickey raise SSHException("No existing session") paramiko.ssh_exception.SSHException: No existing session

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 1, in File "/home/python3.10/venv/lib/python3.10/site-packages/netmiko/ssh_dispatcher.py", line 399, in ConnectHandler return ConnectionClass(*args, **kwargs) File "/home/python3.10/venv/lib/python3.10/site-packages/netmiko/base_connection.py", line 488, in init self._open() File "/home/python3.10/venv/lib/python3.10/site-packages/netmiko/base_connection.py", line 493, in _open self.establish_connection() File "/home/python3.10/venv/lib/python3.10/site-packages/netmiko/base_connection.py", line 1182, in establish_connection raise NetmikoTimeoutException(msg) netmiko.exceptions.NetmikoTimeoutException: Paramiko: 'No existing session' error: try increasing 'conn_timeout' to 15 seconds or larger.


### Relevant Python code
(Please try to essentialize your Python code to the minimum code needed to reproduce the issue)
(Paste the code between the quotes below)

```python
from netmiko import ConnectHandler

key_file = "/root/.ssh/id_rsa"
cisco1 = {
    "device_type": "cisco_ios",
    "host": "192.168.1.1",
    "username": "myUserName",
    "use_keys": True,
    "key_file": key_file,
    'timeout': 60
}

with ConnectHandler(**cisco1) as net_connect:
    output = net_connect.send_command("show ip arp")

How can I connect to my Cisco switch while I have no issue in the bash?

ktbyers commented 8 months ago

Netmiko doesn't use any of this by default (it doesn't use openSSH).

Host switch
    HostName 192.168.1.1
    User myUserName
    IdentityFile /root/.ssh/id_rsa
    KexAlgorithms +diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
    Ciphers aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc

SSH config file processing is very limited in Netmiko and mostly for what is needed for SSH proxy support.

You probably would need to look at the Netmiko log file and see what is failing the SSH key authentication process.

https://github.com/ktbyers/netmiko/blob/develop/COMMON_ISSUES.md