Managing Cisco devices with Python

Source: Internet
Author: User
Tags cisco switch
Discover a foreigner using Python to write a small framework Tratto for managing Cisco devices that 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 = Con Nectivity. Session ("192.168.1.1", +, "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 ()
  • Related Article

    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.