ktbyers / netmiko

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

Netmiko Log #778

Closed dventer closed 6 years ago

dventer commented 6 years ago

i have many device, and i set logging for my script..

logging.basicConfig(filename='network_script.log', level=logging.INFO) logger = logging.getLogger("Netmiko")

when i run my script, there is one or more device that cannot login, but i don't know which device, because in log file, there is no 'ip' or hostname information when failed.

is there any solution for this?

INFO:paramiko.transport:Authentication (password) successful!
INFO:paramiko.transport:Connected (version 2.0, client Cisco-1.25)
INFO:paramiko.transport:Authentication (password) successful!
INFO:paramiko.transport:Connected (version 2.0, client Cisco-1.25)
INFO:paramiko.transport:Authentication (password) successful!
INFO:paramiko.transport:Connected (version 2.0, client Cisco-1.25)
INFO:paramiko.transport:Authentication (publickey) failed.
INFO:paramiko.transport:Disconnect (code 2): Protocol error: expected packet type 50, got 5
INFO:paramiko.transport:Connected (version 2.0, client Cisco-1.25)
INFO:paramiko.transport:Authentication (publickey) successful!
INFO:paramiko.transport:Connected (version 2.0, client Cisco-1.25)
INFO:paramiko.transport:Authentication (publickey) successful!
INFO:paramiko.transport:Connected (version 2.0, client Cisco-1.25)
ktbyers commented 6 years ago

For the device that is failing:

INFO:paramiko.transport:Authentication (publickey) failed.
INFO:paramiko.transport:Disconnect (code 2): Protocol error: expected packet type 50, got 5

What type of device is it (model, vendor, os version).

Also make sure you are running the latest released version of Netmiko (2.1.1).

dventer commented 6 years ago

yes i know..but in Log, there is no information about 'ip'.. i use cisco ASR1000 ver 15.5(3)S5 the latest version that i detect is 2.1.0

[root@xxx ~]# /usr/local/bin/pip3.6 install netmiko Requirement already satisfied: netmiko in /usr/local/lib/python3.6/site-packages Requirement already satisfied: paramiko>=2.0.0 in /usr/local/lib/python3.6/site-packages (from netmiko) Requirement already satisfied: scp>=0.10.0 in /usr/local/lib/python3.6/site-packages (from netmiko) Requirement already satisfied: pyyaml in /usr/local/lib/python3.6/site-packages (from netmiko) Requirement already satisfied: pyserial in /usr/local/lib/python3.6/site-packages (from netmiko) Requirement already satisfied: textfsm in /usr/local/lib/python3.6/site-packages (from netmiko) Requirement already satisfied: pyasn1>=0.1.7 in /usr/local/lib/python3.6/site-packages (from paramiko>=2.0.0->netmiko) Requirement already satisfied: cryptography>=1.5 in /usr/local/lib/python3.6/site-packages (from paramiko>=2.0.0->netmiko) Requirement already satisfied: pynacl>=1.0.1 in /usr/local/lib/python3.6/site-packages (from paramiko>=2.0.0->netmiko) Requirement already satisfied: bcrypt>=3.1.3 in /usr/local/lib/python3.6/site-packages (from paramiko>=2.0.0->netmiko) Requirement already satisfied: idna>=2.1 in /usr/local/lib/python3.6/site-packages (from cryptography>=1.5->paramiko>=2.0.0->netmiko) Requirement already satisfied: cffi>=1.7; platform_python_implementation != "PyPy" in /usr/local/lib/python3.6/site-packages (from cryptography>=1.5->paramiko>=2.0.0->netmiko) Requirement already satisfied: asn1crypto>=0.21.0 in /usr/local/lib/python3.6/site-packages (from cryptography>=1.5->paramiko>=2.0.0->netmiko) Requirement already satisfied: six>=1.4.1 in /usr/local/lib/python3.6/site-packages (from cryptography>=1.5->paramiko>=2.0.0->netmiko) Requirement already satisfied: pycparser in /usr/local/lib/python3.6/site-packages (from cffi>=1.7; platform_python_implementation != "PyPy"->cryptography>=1.5->paramiko>=2.0.0->netmiko) [root@xxx ~]# /usr/local/bin/pip3.6 freeze | grep netmiko netmiko==2.1.0

ktbyers commented 6 years ago

Yes, add some print or logging statements for the device that is failing so you can isolate which device is failing.

If you post your code, I can possibly give you some additional recommendations on how to do this.

dventer commented 6 years ago

This is my code..

from netmiko import Netmiko
from datetime import datetime
now = datetime.now()
logging.basicConfig(filename='network_script.log', level=logging.INFO)
logger = logging.getLogger("Netmiko")
path = '/opt/backup/' + '{:02d}-{:02d}-{:4d}/'.format(now.day,now.month,now.year)
try:
    os.mkdir(path)
except OSError as exc:
    if exc.errno != errno.EEXIST:
        raise
    pass
#os.mkdir( path, 0o027 );
bgp_a = {
        'ip': 'bgp-a',
        'username': 'backup',
        'use_keys': True,
        'device_type': 'cisco_ios',
}
bgp_b = {
        'ip': 'bgp-b',
        'username': 'backup',
        'use_keys': True,
        'device_type': 'cisco_ios',
}
intl3 = {
        'ip': 'intl3',
        'username': 'backup',
        'use_keys': True,
        'device_type': 'cisco_ios',
}
rtr_bca = {
        'ip': 'rtr-bca',
        'username': 'backup',
        'use_keys': True,
        'device_type': 'cisco_ios',
}
module_sw = {
        'ip': 'module-sw',
        'username': 'backup',
        'use_keys': True,
        'device_type': 'cisco_ios',
}
dmzl3 = {
        'ip': 'dmzl3',
        'username': 'backup',
        'use_keys': True,
        'device_type': 'cisco_ios',
}
extl3 = {
        'ip': 'extl3',
        'username': 'backup',
        'use_keys': True,
        'device_type': 'cisco_ios',
}
backbone = {
        'ip': 'backbone',
        'username': 'backup',
        'use_keys': True,
        'device_type': 'cisco_ios',
}
for device in (bgp_a,bgp_b,intl3,rtr_bca,module_sw,dmzl3,extl3,backbone):
        net_connect = Netmiko(**device)
        result = net_connect.send_command("show run full")
        print('Backup {ip} in progress...'.format_map(device))
        file = open('/opt/backup/'+'{:02d}-{:02d}-{:4d}/'.format(now.day,now.month,now.year) + '{ip}'.format_map(device) + '-{:02d}-{:02d}-{:4d}'.format(now.day,now.month,now.year), "w")
        file.write(result)
ktbyers commented 6 years ago

So your program should crash on the first device that fails? So the device that failed should be the last IP address that was printed:

print('Backup {ip} in progress...'.format_map(device))