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

python: logon to devices and collect data

irfanrayma
Level 1
Level 1

I have to build script so that it will logon to all devices in our network and collect show inventory, show ip int bri and copy it in the csv file.

 

is there any easy way to do it using python or any other language? I am new to automation.

7 Replies 7

Joseph W. Doherty
Hall of Fame
Hall of Fame
"Easy" is very subjective.
I've used both PERL and Python scripts (from a Win 7 host) to access Cisco devices, interactively. The former via telnet, the latter via telnet or SSH.
With the telnet packages in CPAN, I found telnet access, using PERL, rather simple.
With Python, I used v3.6 which limits packages you might find (compared to using v2.7) and also supporting SSH further limited available packages. So, I ended up using a command line version of PuTTY and invoked it from my Python script. The major issue then was interacting with the PuTTY program and not waiting on the command line PuTTY program to close after sending it its first command. (How to do this, under Windows, isn't well documented as most doing this kind of scripting would be running from a Linux host.)
I've also used PERL to query Cisco devices via SMTP, which seems to run much faster than interacting with the device using telnet.

Mark Malone
VIP Alumni
VIP Alumni

Hi

im jst looking to see if i have an EEM script that will help you get all the commands but for now this will get your running config sorted and sent to either flash or to an external server every day to start with very stable feature

 

archive
 path flash:Archive.cfg
 maximum 2
 write-memory
 time-period 1440

 

or to tftp server

archive
 path tftp: put your url here
 maximum 2
 write-memory
 time-period 1440

 

you can also send it using other protocols


(config-archive)#path ?
  bootflash:  Write archive on bootflash: file system
  flash:      Write archive on flash: file system
  ftp:        Write archive on ftp: file system
  http:       Write archive on http: file system
  https:      Write archive on https: file system
  pram:       Write archive on pram: file system
  rcp:        Write archive on rcp: file system
  scp:        Write archive on scp: file system
  tftp:       Write archive on tftp: file system

 

I dont know Python well so Joe maybe be able to help with that bit but heres an eem should work too , its basically logging in every 12 hours runs your commands and you can send it to email server then , you can add in more lines just add new actions , leave the amil server though as last section , tweak it to what you need addresses and email servers

 

 

event manager applet CollectInfo
  event snmp oid 1.3.6.1.4.1.9.9.109.1.1.1.1.3.1 get-type exact entry-op gle entry-val 12345 poll interval 43200
  action 1.0 cli command "enable"
  action 2.0 cli command "show running-config"
  action 3.0 cli command "show inventory"
  action 4.0 cli command "show ip int brief"
  action 5.0 mail server "10.1.1.99" to "HTCserver@domain.com" from "HTR@domain.com" subject "CollectInfo" body "$_cli_result"

This is what the email server line looks like when standard without anything filled in , you can tweak this yourself test it

action 5.0 mail server "$_email_server" to "$_email_to" from "$_email_from" subject ""

Jon Marshall
Hall of Fame
Hall of Fame

If you want to use Perl then use the Net::SSH:Cisco module although like Joe I only used Perl for telnet. 

 

If you want to use Python then the Netmiko module will do the job assuming SSH. 

 

To be honest, as long as you understand the basics of programming the hardest part is usually installing the specific modules because neither of the above come with the standard distributions and depending on your OS you can often get incompatible libraries etc. but assuming you can then there are any number of tutorials you can search for that should get you started. 

 

I am currently getting up to speed with Python as that seems to be the scripting language of choice for network automation. 

 

Jon

I've used the non-Cisco variant of the telnet package - didn't find it much of a limitation when accessing Cisco devices (in my environment, we're both Cisco and brand "J").
Yea, Python is the new scripting darling for automation. It has some features I like over PERL. I also prefer the 3.x train, but that does exclude all the Python packages that don't yet support it.

Seb Rupik
VIP Alumni
VIP Alumni

Hi there,

I've been playing with Python for a few months now, mainly with APIC-EM automation. This script below is a reworking of one I wrote to send config command to switches:

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

 

It doesn't make sense to save the output of 'sh inv' and 'sh ip int br' as the columns would be widly different. Instead run the script and pipe it to a file. For homework you can work out how to get the text contents into CSVs. Run it with the following:

 

python3 print_output.py -c ip_addresses.csv > ./print_output.txt

You will need a CSV file with the following structure:

device_ip
10.10.1.1
10.10.1.2
10.10.1.3

And finally the script you need (print_output.py):

#!/usr/bin/env python3

import getpass
import re
import csv
from argparse import ArgumentParser
from netmiko import ConnectHandler

if __name__ == "__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:
            ssh_session = ConnectHandler(device_type='cisco_ios', ip=device_row['device_ip'],
                                         username=ssh_username, password=ssh_password)
            print("-------- {0} ---------".format(device_row['device_ip']))
            print(ssh_session.send_command("sh inv"))
            print(ssh_session.send_command("sh ip int br"))

If you are not already, I recommend using PyCharm community edition as your IDE:

https://www.jetbrains.com/pycharm/

Good luck! :)

 

cheers,

Seb.

I also had the same project. i learned every possible programming language at varsity and python wasn't one of them.But when i was asked to do the project i was specifically told to do it in python and it was simpler than how i would have done it in the other languages. so i would say use python and create a SSH or Telnet script to connect to the devices then run your commands. but preferably any language you comfortable with

Review Cisco Networking for a $25 gift card