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