473,320 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 6359
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

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

Similar topics

2
by: praba kar | last post by:
Dear all, I want to parse the system functions output but I couldn't do it. Kindly assist me in this task. eg) bytesused = os.system('du -sh /Users/enmail') if I print this bytesused...
15
by: Jeannie | last post by:
Hello group! I'm in Europe, traveling with my laptop, and I don't any compilers other than Borland C++ 5.5. available. I also don't have any manuals or help files available. Sadly, more...
19
by: Johnny Google | last post by:
Here is an example of the type of data from a file I will have: Apple,4322,3435,4653,6543,4652 Banana,6934,5423,6753,6531 Carrot,3454,4534,3434,1111,9120,5453 Cheese,4411,5522,6622,6641 The...
5
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
5
by: qazwart | last post by:
I am reading from a "cvs rlog" command, and I need both the STDOUT and STDERR. Unfortunately, something very strange is happening. If I do this: $cmd = "$cvs_cmd -q rlog -NS...
2
by: Lawrence Krubner | last post by:
Imagine a template system that works by getting a file, as a string, and then putting it through eval(), something like this: $formAsString = $controller->command("readFileAndReturnString",...
3
by: Leighya | last post by:
Im currently working on this xml file but when i load it to Mozilla, i got an error "Error Loading Stylesheet: Xpath parse failure: invalid variable name" It loads on IE properly. Only with the...
109
by: bonneylake | last post by:
Hey Everyone, Well i am having a problem outputting for my report and hoping someone can explain what i am doing wrong. What the report is about is comparing my company's part price's to d and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.