12-19-2018 06:12 PM
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.
12-19-2018 07:31 PM
12-20-2018 01:09 AM
Yes pls, if you can share me one
BR
12-20-2018 08:45 PM
12-22-2018 05:23 PM
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
08-03-2020 07:31 PM
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
08-03-2020 09:16 PM
12-20-2018 12:52 AM
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.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide