ktbyers / netmiko

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

Authentication failed/ Netmiko #2969

Closed DOTTRAM closed 2 years ago

DOTTRAM commented 2 years ago

Hello. I using this code. But i cant to connect devices.

`from netmiko import ConnectHandler import getpass

my_devices = ['10.42.49.69', '10.42.49.66'] #list of devices device_list = list() #create an empty list to use it later

for device_ip in my_devices: device = { "device_type": "cisco_ios", "host": device_ip, "username": "ciscogtkz", "password": "Jkbvgbflf80", } device_list.append(device)

print(device_list) #list of dictionaries

for each_device in device_list: connection = ConnectHandler(**each_device) print(f'Connecting to {each_device["host"]}') output = connection.send_command('show run | incl hostname') print(output) connection.disconnect()`

I have next issue C:\Users\Администратор\PycharmProjects\pythonProject3\venv\Scripts\python.exe C:\Users\Администратор\PycharmProjects\pythonProject3\Cisco\Test333.py [{'device_type': 'cisco_ios', 'host': '10.42.49.69', 'username': 'ciscogtkz', 'password': 'Jkbvgbflf80'}, {'device_type': 'cisco_ios', 'host': '10.42.49.66', 'username': 'ciscogtkz', 'password': 'Jkbvgbflf80'}] Traceback (most recent call last): File "C:\Users\Администратор\AppData\Local\Programs\Python\Python310\lib\site-packages\netmiko\base_connection.py", line 1046, in establish_connection self.remote_conn_pre.connect(**ssh_connect_params) File "C:\Users\Администратор\AppData\Local\Programs\Python\Python310\lib\site-packages\paramiko\client.py", line 435, in connect self._auth( File "C:\Users\Администратор\AppData\Local\Programs\Python\Python310\lib\site-packages\paramiko\client.py", line 766, in _auth raise saved_exception File "C:\Users\Администратор\AppData\Local\Programs\Python\Python310\lib\site-packages\paramiko\client.py", line 753, in _auth self._transport.auth_password(username, password) File "C:\Users\Администратор\AppData\Local\Programs\Python\Python310\lib\site-packages\paramiko\transport.py", line 1564, in auth_password return self.auth_handler.wait_for_response(my_event) File "C:\Users\Администратор\AppData\Local\Programs\Python\Python310\lib\site-packages\paramiko\auth_handler.py", line 259, in wait_for_response raise e paramiko.ssh_exception.AuthenticationException: Authentication failed.

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Users\Администратор\PycharmProjects\pythonProject3\Cisco\Test333.py", line 21, in connection = ConnectHandler(*each_device) File "C:\Users\Администратор\AppData\Local\Programs\Python\Python310\lib\site-packages\netmiko\ssh_dispatcher.py", line 365, in ConnectHandler return ConnectionClass(args, **kwargs) File "C:\Users\Администратор\AppData\Local\Programs\Python\Python310\lib\site-packages\netmiko\base_connection.py", line 439, in init self._open() File "C:\Users\Администратор\AppData\Local\Programs\Python\Python310\lib\site-packages\netmiko\base_connection.py", line 444, in _open self.establish_connection() File "C:\Users\Администратор\AppData\Local\Programs\Python\Python310\lib\site-packages\netmiko\base_connection.py", line 1083, in establish_connection raise NetmikoAuthenticationException(msg) netmiko.exceptions.NetmikoAuthenticationException: Authentication to device failed.

Common causes of this problem are:

  1. Invalid username and password
  2. Incorrect SSH-key file
  3. Connecting to the wrong device

Device settings: cisco_ios 10.42.49.69:22

Authentication failed.

Process finished with exit code 1

Then i connect. I am write this date. May be i need change "username" to "logins as" and "password" to "Password". I tryed this. But it still doesn't work. How fix this prolblem? login as: dottram Using keyboard-interactive authentication. Password: C2960-3_chelnygaz#

ktbyers commented 2 years ago

What is the vendor and model that you are connecting to?

Can you post the show version output here?

DOTTRAM commented 2 years ago

I already resolve previous problem. But i have something new problem. How to fix this?

Log C:\Users\Администратор\PycharmProjects\pythonProject3\venv\Scripts\python.exe C:\Users\Администратор\PycharmProjects\pythonProject3\Cisco\Test4444.py [{'device_type': 'cisco_ios', 'host': '10.42.64.65', 'username': 'dottram', 'password': '2736233Ad123'}, {'device_type': 'cisco_ios', 'host': '10.42.91.167', 'username': 'dottram', 'password': '2736233Ad123'}, {'device_type': 'cisco_ios', 'host': '10.42.91.168', 'username': 'dottram', 'password': '2736233Ad123'}, {'device_type': 'cisco_ios', 'host': '10.42.91.160', 'username': 'dottram', 'password': '2736233Ad123'}, {'device_type': 'cisco_ios', 'host': '10.42.91.161', 'username': 'dottram', 'password': '2736233Ad123'}, {'device_type': 'cisco_ios', 'host': '10.42.91.162', 'username': 'dottram', 'password': '2736233Ad123'}, {'device_type': 'cisco_ios', 'host': '10.42.91.163', 'username': 'dottram', 'password': '2736233Ad123'}, {'device_type': 'cisco_ios', 'host': '10.42.91.164', 'username': 'dottram', 'password': '2736233Ad123'}, {'device_type': 'cisco_ios', 'host': '10.42.91.165', 'username': 'dottram', 'password': '2736233Ad123'}, {'device_type': 'cisco_ios', 'host': '10.42.91.166', 'username': 'dottram', 'password': '2736233Ad123'}, {'device_type': 'cisco_ios', 'host': '10.42.31.65', 'username': 'dottram', 'password': '2736233Ad123'}, {'device_type': 'cisco_ios', 'host': '10.42.31.66', 'username': 'dottram', 'password': '2736233Ad123'}, {'device_type': 'cisco_ios', 'host': '10.42.7.68', 'username': 'dottram', 'password': '2736233Ad123'}, {'device_type': 'cisco_ios', 'host': '10.42.7.65', 'username': 'dottram', 'password': '2736233Ad123'}, {'device_type': 'cisco_ios', 'host': '10.42.7.66', 'username': 'dottram', 'password': '2736233Ad123'}, {'device_type': 'cisco_ios', 'host': '10.42.7.67', 'username': 'dottram', 'password': '2736233Ad123'}, {'device_type': 'cisco_ios', 'host': '10.42.43.65', 'username': 'dottram', 'password': '2736233Ad123'}] Connecting to 10.42.64.65 Traceback (most recent call last): File "C:\Users\Администратор\PycharmProjects\pythonProject3\Cisco\Test4444.py", line 39, in output2 = connection.send_command('write memory') File "C:\Users\Администратор\AppData\Local\Programs\Python\Python310\lib\site-packages\netmiko\utilities.py", line 592, in wrapper_decorator return func(self, *args, **kwargs) File "C:\Users\Администратор\AppData\Local\Programs\Python\Python310\lib\site-packages\netmiko\base_connection.py", line 1721, in send_command raise ReadTimeout(msg) netmiko.exceptions.ReadTimeout: Pattern not detected: 'c3560\-shemordangaz\#' in output.

Things you might try to fix this:

  1. Explicitly set your pattern using the expect_string argument.
  2. Increase the read_timeout to a larger value.

You can also look at the Netmiko session_log or debug log for more information. Process finished with exit code 1

This is my code `from netmiko import ConnectHandler import getpass

my_devices = ['10.42.64.65','10.42.91.167', '10.42.91.168','10.42.91.160','10.42.91.161','10.42.91.162','10.42.91.163','10.42.91.164', '10.42.91.165','10.42.91.166','10.42.31.65','10.42.31.66','10.42.7.68','10.42.7.65', '10.42.7.66','10.42.7.67','10.42.43.65'] #list of devices device_list = list() #create an empty list to use it later

for device_ip in my_devices: device = { "device_type": "cisco_ios", "host": device_ip, "username": "dottram", "password": "2736233Ad123", } device_list.append(device)

print(device_list) #list of dictionaries

for each_device in device_list: connection = ConnectHandler(**each_device) print(f'Connecting to {each_device["host"]}')

#config_commands = [
    #'archive',
    #'log config',
    #'notify syslog contenttype plaintext',
    #'exit',
    #'path tftp://10.42.141.70/$h',
    #'write-memory',
    #'time-period 10080',
    #'exit',
    #'exit',
    #'write memory']
#output = connection.send_config_set(config_commands)
output2 = connection.send_command('write memory')
print("Config output: \n {} \n".format(output2))
print("show running-config | section archive: \n {} \n".format(output2))
connection.disconnect()`

login as: dottram Using keyboard-interactive authentication. Password:

c3560-shemordangaz#sj c3560-shemordangaz#sho c3560-shemordangaz#show vers c3560-shemordangaz#show version Cisco IOS Software, C3560 Software (C3560-IPBASEK9-M), Version 15.0(2)SE10a, REL EASE SOFTWARE (fc3) Technical Support: http://www.cisco.com/techsupport Copyright (c) 1986-2016 by Cisco Systems, Inc. Compiled Thu 03-Nov-16 14:11 by prod_rel_team

ROM: Bootstrap program is C3560 boot loader BOOTLDR: C3560 Boot Loader (C3560-HBOOT-M) Version 12.2(44)SE5, RELEASE SOFTWARE (fc1)

c3560-shemordangaz uptime is 15 weeks, 5 days, 5 hours, 23 minutes System returned to ROM by power-on System restarted at 09:39:09 MSK Fri Jun 17 2022 System image file is "flash:c3560-ipbasek9-mz.150-2.SE10a.bin"

This product contains cryptographic features and is subject to United States and local country laws governing import, export, transfer and use. Delivery of Cisco cryptographic products does not imply third-party authority to import, export, distribute or use encryption. Importers, exporters, distributors and users are responsible for compliance with U.S. and local country laws. By using this product you agree to comply with applicable laws and regulations. If you are unable to comply with U.S. and local laws, return this product immediately.

A summary of U.S. laws governing Cisco cryptographic products may be found at: http://www.cisco.com/wwl/export/crypto/tool/stqrg.html

If you require further assistance please contact us by sending email to export@cisco.com.

cisco WS-C3560G-24TS (PowerPC405) processor (revision F0) with 131072K bytes of memory. Processor board ID FOC1435Y5CT Last reset from power-on 2 Virtual Ethernet interfaces 28 Gigabit Ethernet interfaces The password-recovery mechanism is disabled.

512K bytes of flash-simulated non-volatile configuration memory. Base ethernet MAC Address : 68:BD:AB:5F:43:80 Motherboard assembly number : 73-10215-06 Power supply part number : 341-0098-02 Motherboard serial number : FOC14361KMD Power supply serial number : DCA143292JP Model revision number : F0 Motherboard revision number : A0 Model number : WS-C3560G-24TS-S System serial number : FOC1435Y5CT Top Assembly Part Number : 800-26851-03 Top Assembly Revision Number : A0 Version ID : V05 CLEI Code Number : COMB200BRA Hardware Board Revision Number : 0x09

Switch Ports Model SW Version SW Image


Configuration register is 0xF

c3560-shemordangaz# wr c3560-shemordangaz# write mem c3560-shemordangaz# write memory Building configuration... [OK]...

ktbyers commented 2 years ago

Use Netmiko's save_config() method (for saving running-config to startup-config).

DOTTRAM commented 2 years ago

Thank you so much. Its Working. How about a device who had long connection without tacacs? What need write in the code to be fix this?