getting an iterable output from a telnet client in python -


i trying write script perform validation checks on network device (router,switch). using telnet send commands device. store output of command telnetobject.read_until(prompt) file , run validation checks on stored output iterating on 1 line @ time. there way iterate on output without storing in file? issue read_until option gives output in 1 string , output of device trying test has output in tabular form not have consistent delimiter on each line.

this example: import telnetlib

tn=telnetlib.telnet('ip address') pwd='password' uid='userid'  tn.read_until("login: ") tn.write(uid + "\n") tn.read_until("password:") tn.write(pwd +'\n') tn.read_until(">",5) tn.write('port statistics show' + '\n') op1=tn.read_until("*>",5) open ('stattest-temp.txt','w') stat:  stat.write(op1) 

above code particular output , store in file below code iterate on output..problem is in tabular form , starts multiple '_ _ _' characters , each line in table has different number of elements if split using delimiter, hence becomes difficult iterate on output tn.read_until 1 giant string'

with open ('stattest-temp.txt','r') f:    line_no,line in enumerate(f):    if line_no>5:     if '+---------------------------------+----------------+-----------+-----------+------------+\r' in line:      break     else:      x=line[:-1].split("|")      #print x      x1=x[1].strip()      x2=x[2].strip()      x3=x[3].strip()      x4=x[4].strip()      x5=x[5].strip()      if x4 =="yes":        print x1,x4        print 'traffic loss observed'       elif x4=="no":       print "session recovered" 

i looking way part 2 of above code without having write output file

i think if split output string on newline characters, should replicate effect of saving file , iterating through lines. set before for loop:

 f = op1.split('\n')   line_no,line in enumerate(f):    if line_no>5:     if '+---------------------------------+----------------+-----------+-----------+------------+\r' in line:      break     else:      x=line[:-1].split("|")      #print x      x1=x[1].strip()      x2=x[2].strip()      x3=x[3].strip()      x4=x[4].strip()      x5=x[5].strip()      if x4 =="yes":        print x1,x4        print 'traffic loss observed'       elif x4=="no":       print "session recovered" 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -