Home > Backend Development > Python Tutorial > Manage Cisco devices using python

Manage Cisco devices using python

高洛峰
Release: 2016-10-18 10:47:46
Original
2475 people have browsed it

I found tratto, a small framework for managing cisco equipment written by a foreigner using python, which can be used to execute commands in batches.

There are mainly 3 files after downloading:

Systems.py defines the operating systems of some different devices and their common commands.

Connectivity.py is the code that mainly implements functions. In fact, it mainly uses python’s pexpect module.

Driver.py is a sample file.

[root@safe tratto-master]# cat driver.py

#!/usr/bin/env python
import Connectivity
import Systems
#telnet to a cisco switch
m = Systems.OperatingSystems['IOS']
s = Connectivity.Session("192.168.1.1",23,"telnet",m)
s.login("yourusername", "yourpassword")
# if your need to issue an "enable" command
s.escalateprivileges('yourenablepassword')
s.sendcommand("show clock")
s.sendcommand("show run")
s.logout()
Copy after login

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

First select a device system version, in this case a cisco switch, so IOS is used. The device systems that the author currently writes that 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 selected in the previous step. login Fill in the login credentials.

s.escalateprivileges are privileged credentials. so easy~

The following is a script I wrote to grab some information about the switch and then save it to a file.

[root@safe tratto-master]# cat cisco.py

#!/usr/bin/env python
#
# Cisco Switch commands
# By s7eph4ni3
#
import Connectivity
import Systems
m = 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()
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template