471,593 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,593 software developers and data experts.

parse command output in variable with loop

29
would like to read and parse certain fields from multiple lines ( say lines 5 and 9 and the first and sixth fields respectively)
from the variable of the file the command output created
tried this code below but having problems parsing out_ping
Expand|Select|Wrap|Line Numbers
  1. ip_array = ('192.168.1.1', '192.168.1.1', '124.128.x.x')
  2. for i in ip_array:
  3.     out_ping = os.popen('ping' + ' ' + i, 'r+').read()
  4.     #sys.stdout.flush()
  5.     print out_ping
  6.  
  7. out_text = ('out_ping',5,9, 'r')    
  8. for line in out_text:
  9.    z = line.split()
  10. if line 5 z[0] == "Reply" and line 9 z[6] < 80ms:
  11.         #sys.stdout.writelines(z[2] + reachable + "\n")
  12.         print z[2] + reachable + "\n"  
  13. else
  14. print "congested"
Mar 13 '09 #1
1 6023
bvdet
2,851 Expert Mod 2GB
Your first for loop appears to work OK. Then you create a tuple and iterate on it in another for loop. The first item in the tuple is the string object 'out_ping'. Look at this:
Expand|Select|Wrap|Line Numbers
  1. >>> 'out_ping'.split()
  2. ['out_ping']
  3. >>> 
The following if statement is indented incorrectly. It only checks the last iteration of the preceding for loop.

Look at the following and see if you can adapt it for your purpose:
Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. ip_array = ('192.168.1.1',)
  4. ping_results = []
  5. for ip in ip_array:
  6.     f = os.popen('ping %s' % (ip), 'r+')
  7.     out_ping = [item.strip() for item in f.readlines() if item.strip()]
  8.     f.close()
  9.     ping_results.append(out_ping)
  10.  
  11. for result in ping_results:
  12.     chk1 = result[4]
  13.     chk2 = int(''.join([s for s in result[8].split()[-1] if s.isdigit()]))
  14.     if chk1.startswith("Reply") and chk2 < 80:
  15.         print chk1
  16.         print result[8]
  17.  
Mar 13 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by praba kar | last post: by
19 posts views Thread by Johnny Google | last post: by
AdrianH
5 posts views Thread by AdrianH | last post: by
AdrianH
1 post views Thread by AdrianH | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.