0

I'm a Java/C programming, struggling to learn Python. (I'm writing in Python 2.4.3 on a Linux machine.) I have a simple program which telnets to a Cisco router, logs in, then captures the config. The router's config is stored in a variable named "output":

#!/usr/bin/python

import telnetlib
import datetime
import sys

def telnetFunction( host ):
  print("Telnetting to device "+host+"...\n")
  tn = telnetlib.Telnet(host)
  # ...use tn.read_until() and tn.write() to log into the router...  ...this works...
  tn.write("terminal length 0"+"\n")
  tn.write("show run"+"\n")
  tn.write("exit"+"\n")
  output=tn.read_all()      # router config stored as "output"
  return output

host = "192.168.102.133"
output=telnetFunction( host )

The above code works. From using a bunch of different print() statements, I can see that the router's config is all in "output", which I am assuming is an array of newline-terminated strings....? Not entirely sure about that; the telnetlib documentation doesn't specify.

My problem now is I need my program to step through output again, extracting one string at a time. I need something like:

while(iterating through output)
   tmpString = output.getNextStr()

So if output looked like this:

Current configuration : 34211 bytes\n!\nversion 12.3\nno service pad\n...etc...

I need tmpString to equal the follow in each iteration of the loop above:

tmpString = "Current configuration : 34211 bytes"
tmpString = "!"
tmpString = "version 12.3"
tmpString = "no service pad"

I've been Googling for a few hours, but am stumped. (Part of the problem is I'm not clear that "output" is indeed an array. Could it be a string?) I've played with split() and using []'s but no luck so far. Does anyone see where I'm going wrong? Thanks, -ROA

6
  • You are working with lists not arrays, firstly. In any event, output looks like it's a string. Commented May 10, 2017 at 19:51
  • You're just splitting on the \ns? Commented May 10, 2017 at 19:52
  • 9
    Why are you using Python 2.4.3? It's over 11 years old. Commented May 10, 2017 at 19:52
  • @juanpa.arrivillaga Interesting. Lists, not arrays? I never would have guessed that. Do you know this from experience, or is there some documentation out there that I haven't uncovered? The hardest thing about learning python is that sometimes you just don't know what data type you are working with. Thanks...! Commented May 11, 2017 at 13:15
  • @aryamccarthy Correct. The '\n' characters are part of the original chunk of data I read in Commented May 11, 2017 at 13:16

2 Answers 2

2

As you stated in your example, let say output is defined as

output = 'Current configuration : 34211 bytes\n!\nversion 12.3\nno service pad'

Your next step is to split the string by \n so

output_list = output.split('\n')

this will generate the following list

output_list = ['Current configuration : 34211 bytes', '!', 'version 12.3', 'no service pad']

then you can iterate over this list.

to tie it all together

#!/usr/bin/python

import telnetlib
import datetime
import sys

def telnetFunction( host ):
  print("Telnetting to device "+host+"...\n")
  tn = telnetlib.Telnet(host)
  # ...use tn.read_until() and tn.write() to log into the router...  ...this works...
  tn.write("terminal length 0"+"\n")
  tn.write("show run"+"\n")
  tn.write("exit"+"\n")
  output=tn.read_all()      # router config stored as "output"
  return output

host = "192.168.102.133"
output=telnetFunction( host )
output_list = output.split('\n')
for item in output_list:
    print item
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Your code solved the problem, thanks. I wasn't aware you could convert a large string into a list with split() - that's def a trick for my notebook.
1

Your variable tn is, I'm assuming, treated like a file handle. So when you read this 'file' once, the pointer is at the end of the file and needs to be reset:

tn.seek(0)

Call this before you execute your second loop.

Alternatively, you can iterate over output like so:

for line in output.split("\n"):
    print line

1 Comment

To be honest, I don't think tn is a file handle. It seems to be an array or a string or something saved in local memory. However, your use of split() looks promising, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.