cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
3255
Views
5
Helpful
7
Replies

Command Script

pradip.saha
Level 1
Level 1

I want to get same set of multiple show command outputs like "show version", "show interfaces brief", etc from hundreds of devices. How can I get this done quickly. If the solution is script, can you share any sample script. Else what is the other way around. And finally I want to redirect the output to a particular format for easy reference.

 

Pls. advice. Thanks in advance.

 

 

 

7 Replies 7

Francesco Molino
VIP Alumni
VIP Alumni
Hi

You can do such thing using tools like Cisco Prime, kiwi cat tool. With these tools, it would be difficult to get the output into a specific format.

Or you can use a Python script using textfsm library to convert the output into csv file for example. If you want I can create a quick script and share it with you.

Thanks
Francesco
PS: Please don't forget to rate and select as validated answer if this answered your question

Yes pls, if you can share me one

 

 

BR

Ok let me build it and I'll share it tomorrow using textfsm library

Thanks
Francesco
PS: Please don't forget to rate and select as validated answer if this answered your question

You'll find 2 files:

- 1 is for the script to run

- 1 is where we use textfsm librairies (it's like a module of our main script)

 

All textfsm templates can be found on internet like: https://github.com/networktocode/ntc-templates

 

I reused an old script I did where it runs commands/use templates based on device type (IOS, NXOS, ASA).

It uses also a library called click to pass some parameters to the script:

1. if you use --iplist xxxxx behind the script name ==> you need to replace xxxxx by a text file name containing all your devices' IPs (1 by line)

2. if you use --iponly xxxx behind the script name ==> replace xxxx by IP of 1 device

 

I create them a long time ago in a quick and dirty way to help and train people on basics. I have some more sophisticated but I believe this one could help you and it's built in a simple way if you want to customize it.

 

Can't attach them to the post.

Here a dropbox link to download them: https://www.dropbox.com/s/42h0wp18gqsjsv3/Python_Example_TextFSM_script99.zip?dl=0

 

 

 


Thanks
Francesco
PS: Please don't forget to rate and select as validated answer if this answered your question

Hello Francesco, I hope you are in good health, as well as yours.
Sorry to bother you, but I would like you to share the script I posted above, since I'm going a little crazy wanting to use Textfsm Templates and not get it to work with several show commands, and have the output .csv corresponding to each command.
I hope you can help me, since I have been trying for more than 3 weeks something that I cannot make it work.
Thanks for your time.

Best Regards

Hi

You mean you want me to re-share the example i shared using Dropbox?
If so, here the link:
https://u.pcloud.link/publink/show?code=XZmBc3kZpMn1OTCUQthUYmJpt4w1nBXNnNA7

It has an expiry date

Thanks
Francesco
PS: Please don't forget to rate and select as validated answer if this answered your question

Seb Rupik
VIP Alumni
VIP Alumni

Here you go:

#!/usr/bin/env python3
import getpass
import csv
import netmiko
import paramiko
from argparse import ArgumentParser


def main():
    parser = ArgumentParser(description='Arguments for running oneLiner.py')
    parser.add_argument('-c', '--csv', required=True, action='store', help='Location of CSV file')
    args = parser.parse_args()

    ssh_username = input("SSH username: ")
    ssh_password = getpass.getpass('SSH Password: ')

    with open(args.csv, "r") as file:
        reader = csv.DictReader(file)
        for device_row in reader:
            try:
                ssh_session = netmiko.ConnectHandler(device_type='cisco_ios', ip=device_row['device_ip'],
                                                     username=ssh_username, password=ssh_password)

                print("+++++ {0} +++++".format(device_row['device_ip']))
                ssh_session.send_command("terminal length 0")
                print(ssh_session.send_command("sh ip int br"))
                ssh_session.send_command("terminal length 30")
                ssh_session.disconnect()

            except (netmiko.ssh_exception.NetMikoTimeoutException,
                    netmiko.ssh_exception.NetMikoAuthenticationException,
                    paramiko.ssh_exception.SSHException) as s_error:
                print(s_error)


if __name__ == "__main__":
    main()

https://github.com/sebrupik/srupik-apic-em-tools/blob/master/frozenPony/src/oneLinerSimple.py

 

The CSV file it needs has a single column with the header 'device_ip' .

If you want to extend it, just add move lines by the print statement, eg:

 

print(ssh_session.send_command("sh ver"))
print(ssh_session.send_command("sh ip int br"))

 

The script prints to the terminal, so you will need to pipe the output to a file. I would also be fairly trivial to extend it further to output to a text file for each device.

 

cheers,

Seb.