Example of using a Python script to obtain Cisco device information

Source: Internet
Author: User
Tags cisco switch
Today, a small framework tratto for managing Cisco devices, written in Python, can be used to execute commands in bulk.

There are 3 main files after download:

systems.py defines the operating systems of a number of different devices and their common commands.

Connectivity.py is the main implementation of the code, in fact, the main is the use of Python pexpect module.

driver.py is an example file.

[Root@safe tratto-master]# cat driver.py#!/usr/bin/env pythonimport connectivityimport Systems#telnet to a Cisco Switchm = systems.operatingsystems[' IOS ']s = connectivity.session ("192.168.1.1", "23°c", "Telnet", m) s.login ("YourUserName", " YourPassword ") # If your need to issue an" enable "commands.escalateprivileges (' Yourenablepassword ') S.sendcommand (" Show Clock ") S.sendcommand (" Show Run ") S.logout ()

The above is the content of example driver.py, which is very simple to use.

First select a device system version, this example Cisco switch, so iOS is used. The device systems that the author now writes about can be supported are:

Operatingsystems = {  ' IOS ': Ciscoios,  ' Webns ': Ciscowebns,  ' OSX ': appleosx,  ' SOS ': Securecomputingsidewinder,  ' AOS ': ArubaOS,  ' Obsd ': OpenBSD,  }

Then fill in the IP, port, telnet or SSH, and finally the system version that was selected in the previous step. Login to fill in the login credentials.

S.escalateprivileges is a privileged credential. So easy~

Here's a script that I wrote, grabbed some information from the switch, and then saved it to a file.

[Root@safe tratto-master]# cat cisco.py#!/usr/bin/env python## Cisco Switch commands# by S7eph4ni3#import Connectivityimport systemsm = systems.operatingsystems[' IOS ']iplist = [' 192.168.1.1 ', ' 192.168.1.2 ']cmdlist = [' Show ip int brief ', ' show CDP nei detail ', ' show Arp ', ' Show ver ']for IP in iplist:  if ip = = ' 192.168.1.1 ':    s = Connectivity. Session (ip,23, "Telnet", m)    S.login ("", "passwd")  else:    s = connectivity.session (ip,22, "ssh", m)    S.login ("username", "passwd")  s.escalateprivileges (' enpasswd ')  f = open (ip+ '. txt ', ' w+ ') for  cmd in Cmdlist:    a = S.sendcommand (cmd)    f.write (ip+cmd+ ' \ n ') F.write (a    + ' \ n ') f.close (  )  s.logout ( )
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.