ktbyers / netmiko

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

Netmiko not auto accept fingerprint for network devices #2996

Open mvmahadevan opened 2 years ago

mvmahadevan commented 2 years ago

Hey, i was connecting to a cisco devices with the below script, but unfortunately the script stops if the devices is login for the first time. The script works after i have login manually to the device.

Script: cisco_netmiko_show.py from netmiko import ConnectHandler import getpass

with open('devices.txt') as f: devices = f.read().splitlines()

device_list = list()

username = input('Enter the username: ') password = getpass.getpass('Enter the password: ')

for ip in devices: cisco_device = { 'device_type': 'cisco_ios', 'host': ip, 'username': username, 'password': password, 'port': 22, 'verbose': True } device_list.append(cisco_device)

for device in device_list: connection = ConnectHandler(**device)

check_enable_mode = connection.check_enable_mode()
print(f'The device is in enable mode: {check_enable_mode}')

if not check_enable_mode is True:
    connection.enable()

print('*' * 70)
print(f'Connected to the device: {device["host"]}')
output = connection.send_command('sh run | i username') # Sends the command to the device.
print(output)
print(f'Closing connection to {device["host"]}')
print('*' * 70)
connection.disconnect()
ktbyers commented 2 years ago

Can you show me the error or exception that you receive?

mvmahadevan commented 2 years ago

Greetings Kirk, here is the error.

(py-3.9.7) [xxxxxxxxxxxxxxxxxx ~]$ python cisco_netmiko_show.py Welcome to Python Scripting !!! Enter the username: xxxxxxxxxx Enter the password: Traceback (most recent call last): File "/home/g706361/py-3.9.7/lib64/python3.9/site-packages/netmiko/base_connection.py", line 1046, in establish_connection self.remote_conn_pre.connect(**ssh_connect_params) File "/home/g706361/py-3.9.7/lib64/python3.9/site-packages/paramiko/client.py", line 349, in connect retry_on_signal(lambda: sock.connect(addr)) File "/home/g706361/py-3.9.7/lib64/python3.9/site-packages/paramiko/util.py", line 279, in retry_on_signal return function() File "/home/g706361/py-3.9.7/lib64/python3.9/site-packages/paramiko/client.py", line 349, in retry_on_signal(lambda: sock.connect(addr)) socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/g706361/cisco_netmiko_show.py", line 26, in connection = ConnectHandler(*device) File "/home/g706361/py-3.9.7/lib64/python3.9/site-packages/netmiko/ssh_dispatcher.py", line 365, in ConnectHandler return ConnectionClass(args, **kwargs) File "/home/g706361/py-3.9.7/lib64/python3.9/site-packages/netmiko/base_connection.py", line 439, in init self._open() File "/home/g706361/py-3.9.7/lib64/python3.9/site-packages/netmiko/base_connection.py", line 444, in _open self.establish_connection() File "/home/g706361/py-3.9.7/lib64/python3.9/site-packages/netmiko/base_connection.py", line 1068, in establish_connection raise NetmikoTimeoutException(msg) netmiko.exceptions.NetmikoTimeoutException: TCP connection to device failed.

Common causes of this problem are:

  1. Incorrect hostname or IP address.
  2. Wrong TCP port.
  3. Intermediate firewall blocking access.

Device settings: cisco_ios xx.xx.xx.xx:22