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 ( )