0

I am using Python telnetlib for taking the "running config" output from router.How to store the "show running-config" output in a variable.And print the variable.My requirement is the each and every output will display in the console when executing each and every line of the code.Is there any option to aviod these print statements.

import telnetlib
#import getpass
ipaddr = "10.1.1.1"
passwd = "abcd123"
tn = telnetlib.Telnet(ipaddr)
if Password:
    try:
          print (tn.write (password + "\n"))
          print(tn.read_until("Router>"))
          print(tn.write("enable\n"))
          print(tn.read_until("Password:"))
          print(tn.write(passwd + "\n"))
          print(tn.read_until("Router#"))
          print(tn.write("show clock\n"))
          print(tn.read_until("#"))
          print(tn.write("show running-config\n"))
          print(tn.write("\040\n"))
          print(tn.write("\040\n"))
          print(tn.write("\040\n"))
          print(tn.read_until("#"))
          print(tn.write("logout\n"))
          print(tn.read_until(">"))
          print tn.close
gmanikandan
  • 475
  • 3
  • 10
  • 16
  • You don't necessarily need to print every operation; you could instead store them in things like variables, objects or dictionaries and then perform print operations afterwards. By the looks of things though, it seems very transactional so it mightn't be able to be made more elegant. If you could provide a sample of the output, it might be possible. – dilbert Jul 22 '13 at 09:49

1 Answers1

0

If I understand you correctly you wish to print out to your local console the output of each command which you run on the remote console. I am not sure why it needs to be synchronous except you say that is a requirement. You might want to make sure you understand the requirements. In any case, since your requirement is that the output be printed, you don't need to print your input...

I highly recommend storing the output into a variable even if you need to print it immediately simply because I see no benefit of retrieving the data unless you are going to act on the data and if you merely print the data you cannot act on it. Store it in a variable and then it can be printed as well as acted upon. I doubt the human eye would be able to tell the difference in storing it and then writing it all at once rather than piecemeal.

Your try block, as written, will never happen because you have to read from the telnet session first before you can evaluate if 'Password:' is on the remote console.

Some further suggestions:

First, write terminal length 0, that will avoid having to handle paused output.

Second, since I am lazy, any variables I know I am only using to pass to the remote unit I save with a newline character.

Third, always give it a timeout or else it runs the risk of waiting forever for a match that might never come.

Fourth, have a look at Telnet.expect(list, [timeout]). I find it far more useful than a simple read_until; it allows you to look for multiple responses and act on each of them accordingly. It is quite useful for handling errors. It returns a three item tuple that represents the index of the matched item (-1 if no match) as well as the matched text (or everything in the buffer if no match).

Fifth, write a decorator for your telnet session to log in. You know it will be used at least once every time you interact with a remote unit, and more if you are loading new firmware. Develop a library of functions that you can reuse rather than writing it out each time. Lazy is good.

import telnetlib
import sys

ipaddr = "10.1.1.1"
passwd = "abcd123"


def login(tn):
  global passwd
  passwd=passwd+'\n'

  def error_check(tmp):
    if tmp[0]==-1: 
      print "Unexpected response"
      print "tmp[2]
      sys.exit()

  tmp=tn.expect(["Password:",], 5)
  error_check(tmp)
  tn.write(passwd)
  tmp=expect([">",],5)
  error_check(tmp)
  tn.write('en\n')
  tmp=expect(["Password", "#'],5)
  error_check(tmp)
  if tmp(0)==0:   #if someone left enable unlocked, don't send a password
    tn.write(passwd)        
  tmp=expect(["#',],5)
  error_check(tmp)         

tn = telnetlib.Telnet(ipaddr)
login(tn)
tn.write('terminal length 0')
tmp=expect(["#',],5)
tn.write('sho clock')
now=tn.expect(["#",], 5)
print now
tn.write('sho run')
print run
cfg=tn.expect(["#",], 5)
tn.write("logout\n"))
bye=tn.expect([">",], 5)
print bye
tn.close()  
Sisyphus
  • 318
  • 1
  • 5
  • 9