knight
knight

Reputation: 39

Messy Netmiko Output

From the Netmiko documentation it looks like I should get clean output, but from what I've done it seems to show even the \n to indicate a new line. Is something missing in my code at all?

import getpass
from netmiko import (
    ConnectHandler,
    NetmikoTimeoutException,
    NetmikoAuthenticationException,
)

def send_command(device, commands):
    result = {}
    try:
        with ConnectHandler(**device) as ssh:
            ssh.enable()
            for command in commands:
                output = ssh.send_command(command)
                result[command] = output
        return result
    except (NetmikoTimeoutException, NetmikoAuthenticationException) as error:
        print(error)

if __name__ == "__main__":

    username = input("Username: ")
    passwd = getpass.getpass()

    device = {
        "device_type": "cisco_xr",
        "host": "router1",
        "username": username,
        "password": passwd,
    }

    result = send_command(device, "show ip int brief")
    print(result)

Output is as follows:

{'show ip int brief': '\nTue Jul 19 07:27:36.879 BST\n\nInterface                      IP-Address      Status          Protocol Vrf-Name \nTenGigE0/3/0/0                 unassigned   Down            Down     default \nTenGigE0/3/0/1                 unassigned   Down            Down     default \nTenGigE0/3/0/2                 unassigned      Shutdown        Down     default \nTenGigE0/3/0/2.101             unassigned   Shutdown        Down     default \nTenGigE0/3/0/3                 unassigned   Down            Down     default \nTenGigE0/3/0/4                 unassigned   Up              Up       default \nTenGigE0/3/0/5                 unassigned      Down            Down     default \nTenGigE0/3/0/5.1               unassigned      Down            Down     default \nTenGigE0/3/0/5.2               unassigned   Down            Down     default \nTenGigE0/3/0/5.4               unassigned   Shutdown        Down     default \nTenGigE0/3/0/5.6               unassigned   Down            Down     default \nTenGigE0/3/0/6                 unassigned   Up              Up       default \nTenGigE0/3/0/7                 unassigned      Up              Up       default \nTenGigE0/3/0/8                 unassigned      Up              Up       default \nTenGigE0/3/0/9                 unassigned   Down            Down     default \nTenGigE0/3/0/10                unassigned   Shutdown        Down     default \nTenGigE0/3/0/11                unassigned      Down            Down     default \nTenGigE0/3/0/12                unassigned      Shutdown        Down     default \nTenGigE0/3/0/12.1              unassigned   Shutdown        Down     default '}

I think without the \n involved it would be formatted nicely, but shouldn't it output this way, and how do I get rid of the visible line breaks from the output?

Many thanks

Upvotes: 0

Views: 685

Answers (1)

Baris Sonmez
Baris Sonmez

Reputation: 475

With the following method, you can run the "show ip int brief" command on many devices at the same time and print the outputs properly. This method also uses the prompt feature.

import time
from multiprocessing.dummy import Pool as ThreadPool
from netmiko import Netmiko

#You should put all the ip you want to enter the config in the notepad below
f_2 = open("ip_list.txt","r")
ip_list = f_2.readlines()
f_2.close()

#To see devices with access problems, you must create the following notepad
f_3 = open("connection was not established.txt","w")

def ssh(nodeip):
    try:
        cisco = {
            'device_type': 'cisco_ios', 'ip': nodeip, 'username':
            XXXX, 'password': XXXX, }
        net_connect = Netmiko(**cisco)
        print(nodeip.strip() + "  " + "success connection")
    except Exception as e:
        print (e)
        f_3.write(nodeip.strip() + "\n")
        return

    prompt_cisco_fnk = net_connect.find_prompt()
    hostname_fnk = prompt_cisco_fnk.strip("<" + ">")
    print(hostname_fnk) #you can see the prompt feature

    net_connect.send_command_timing("enable")  
    output = net_connect.send_command_timing("config")
    print("config mode entered")

    net_connect.send_command_timing("show ip int brief")  
    print("config done")
 
    net_connect.disconnect()

myPool = ThreadPool(200) #With the Threading method, you can enter devices very quickly at the same time. It is 200 variables.
result = myPool.map(ssh,ip_list) #ssh def is your function name and ip_list is the notepad where you enter the device ips.

Upvotes: 1

Related Questions