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
outputlooks like it's a string.\ns?