0

Im new to python, I managed to telnet into my cisco router using my python codes. I am able to show commands on the screen, but I would like to save the outputs locally on my linux machine, same place where the python script lives.

Any suggestions?

my aim is to store the output locally and then import matplotlib to draw some pretty nifty graphs of bandwidth usages, cpu usages, memory usages and also interface usages.

user2735532
  • 1
  • 1
  • 1
  • This is a multi-part question. How do you programatically get the data out of the router and in to memory? How do you want to serialize your data? To a text file? To a data-base? Once you have that _then_ we can talk about how to plot it. – tacaswell Aug 31 '13 at 16:55
  • Please ask questions if you get stuck anyplace along the way and have _specific_ problems. – tacaswell Aug 31 '13 at 17:02

2 Answers2

0

For what you are looking to do, you should consider using SNMP instead of trying to process the telnet I/O.

You will be able to pull the values off that you are describing and put them in your data storage of choice (text, mysql, etc.)

http://pysnmp.sourceforge.net/

http://www.cisco.com/en/US/docs/ios/12_2/configfun/configuration/guide/fcf014.html

0

The below script will save your data to a file on the server you are running this script from. File name will be routeroutput. Just input the switch IP, Password and enable password in the code below and run it from your server using python.

It needs an additional module called pexpect. You can download and install it from here https://pypi.python.org/pypi/pexpect/

import pexpect


try:
switchIP= 'x.x.x.x'
switchPassword = 'your-switch-password'
switchEnable= 'your-enable-password'
commandTorun= 'The command you want to run'


telnet = 'telnet ' + switchIP

#Login to the switch
t=pexpect.spawn(telnet)
t.expect('word:')
t.sendline(switchPassword)
t.expect('#')
t.sendline(switchEnable)
t.expect('>')

#Send the command
t.sendline('commandTorun')
t.expect('>')
data =  t.before 

#Closing the Telnet Connection
t.sendline('exit')
t.expect('>')
t.sendline('exit')
t.expect(pexpect.EOF)

#Opening the file and writing the data to it
f = open('routeroutput', 'w')
f.write(data)
f.close()

except Exception, e:
print "The Script failed to login"
print str(e)