473,651 Members | 2,793 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parse command output in variable with loop

29 New Member
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 6377
bvdet
2,851 Recognized Expert Moderator Specialist
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
1856
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 variable the output of bytesused variable is the below
15
3197
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 crippling problems! I never used C++ much, so, I don't remember commands or fuctions well. My questions are simple. I posted the DOS window output so you can see the Borland C++ version I am using: E:\TZ>bcc32 file2
19
3211
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 first position is the info (the product) I want to retreive for the corresponding code. Assuming that the codes are unique for each product and all code data is on one line.
5
64619
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 article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
1
64093
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 article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
5
1671
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 -r$from_label::$to_label neo 2>&1"; // Hard coded module DEBUG ("if (!($cvs_fh = popen(\"$cmd\", 'r'))) {"); if (!($cvs_fh = popen("$cmd", 'r'))) { generate_error("Canot open command \"$cmd\" for reading"); }
2
3216
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", $formName); // 06-22-07 - the next commands try to import all the functions that the
3
6917
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 mozilla browser i get an error. Would someone help me pls. Here is the code: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"...
109
12760
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 h, synnex and techdata. What i was told to do was take are part numbers and combine them with d and h, synnex and techdata to display only one column of parts (did this). Then i was told to create a cfloop using the query i created to combine the...
0
8352
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8802
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8465
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7297
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2699
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1587
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.