Python Output Displaying Horizontal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python Output Displaying Horizontal
# 1  
Old 03-02-2014
Python Output Displaying Horizontal

I feel bad for posting so much lately. I've just been working on a project for fun to force myself to learn Python better. Recently I decided to incorporate this ping.py script on github found here. I'm not going to bore you with all the changes I made, but the problem now lies in this function from ping.py here:

Code:
def verbose_ping(dest_addr, timeout=2, count=4):

    global response

    for i in range(count):
        delay = do_one(dest_addr, timeout)
        if delay == None:
            f = open(os.devnull,"w")
        else:
            delay = round(delay * 1000.0, 4)
            response = ('{}'.format(dest_addr))

Then there's my code which calls the ping.py code:

Code:
#!/usr/bin/python

import re
import ping, socket
import subprocess
from subprocess import Popen, PIPE

pattern = re.compile(r'inet.\S+(\S+\d+\.\d+\.\d+\.)')

p1 = Popen(["/sbin/ifconfig"], stdout=subprocess.PIPE)

output = p1.communicate()[0]
result = re.findall(pattern, output)

for i in xrange(1, 255):
    test = result[1] + str(i)
    try:
        ping.verbose_ping(test, count=1)
    except socket.error, e:
        print "Ping Error:", e

for item in ping.response:
    print item

I should have had 10.0.0.1 and 10.0.0.139 displaying, but after waiting for the output to process I only got one ip address and it came out horizontal:

Code:
# ./sundayadd.py 
1
0
.
0
.
0
.
1
3
9

I've been going at this for most of today and I think my eyes are going to bleed again. If anyone has a suggestion while I unplug for a while its much appreciated.
# 2  
Old 03-02-2014
use printf instead of print
This User Gave Thanks to SriniShoo For This Post:
# 3  
Old 03-02-2014
I actually just found I could get the output straight if I put a comma in the last loop:

Code:
for item in ping.response:
    print item,

I still only get one ip and using item.replace didn't remove the extra white space. I just tried using printf with the following:

Code:
#!/usr/bin/python
import re
import ping, socket
import subprocess
from subprocess import Popen, PIPE

import sys
def printf(format, *args):
    sys.stdout.write(format % args)

pattern = re.compile(r'inet.\S+(\S+\d+\.\d+\.\d+\.)')

p1 = Popen(["/sbin/ifconfig"], stdout=subprocess.PIPE)

output = p1.communicate()[0]
result = re.findall(pattern, output)

for i in xrange(1, 255):
    test = result[1] + str(i)
    try:
        ping.verbose_ping(test, count=1)
    except socket.error, e:
        print "Ping Error:", e

for item in ping.response:
    #print item.replace(" ", "")
    printf('This is a test: %r', item)

While it didn't fix the output, it did provide a clue. It seems the characters of the ip addresses are going in one at a time instead of together:

Code:
# ./sundayadd.py 
This is a test: '1'This is a test: '0'This is a test: '.'This is a test: '0'This is a test: '.'This is a test: '0'This is a test: '.'
This is a test: '1'This is a test: '3'This is a test: '9'

Just not sure where this is happening...
# 4  
Old 03-03-2014
When you use ping.response in a for-loop, it returns an iterator over which the loop iterates. I don't have the ping module in my python installation, so I cannot test it. But try one of the following:
Code:
print "This is a test: " + str(ping.response)

or
Code:
printf("This is a test: ")
for item in ping.response:
    printf("%r", item)

If the former solution works, I would recommend you to use that over the latter one.
# 5  
Old 03-04-2014
Sorry I'm just now getting back to this. Had a long day with a shift change for new training at work. I ran the two suggestions:

First:
Code:
# ./monday.py 
This is a test: 10.0.0.139
This is a test: 10.0.0.139
This is a test: 10.0.0.139
This is a test: 10.0.0.139
This is a test: 10.0.0.139
This is a test: 10.0.0.139
This is a test: 10.0.0.139
This is a test: 10.0.0.139
This is a test: 10.0.0.139
This is a test: 10.0.0.139

Second:
Code:
# ./monday.py 
This is a test: '1''0''.''0''.''0''.''1''3''9'

Looks like this confirms my suspicion that the ip addresses are being broken into characters somewhere. I guess its overwriting the list with each new ip's characters. The 10.0.0.139 is the last on that subnet. It seems the first code also iterated ten times and there are ten characters in 10.0.0.139 if you count the dots. Still at a loss for where this is coming from though.

Thanks again for the suggestions.
# 6  
Old 03-04-2014
Try:
Code:
print "{0}".format(''.join(item))

# 7  
Old 03-04-2014
Just woke up and tried this:
Code:
for item in ping.response:
    print "{0}".format(''.join(item))

Seems to go horizontal again:
Code:
# ./monday.py 
1
0
.
0
.
0
.
1
3
9

Let's see if I can hammer something new out before work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help! output format from vertical to horizontal

Hi All, please help to achieve the desired output Example: I have a file which contains the below data empname robert empid 787 design consultant empname alex empid 898 design advocate Desired output should be empname empid design robert 787 consultant (19 Replies)
Discussion started by: rocky2013
19 Replies

2. Shell Programming and Scripting

Displaying log file pattern output in tabular form output

Hi All, I have result log file which looks like this (below): from the content need to consolidate the result and put it in tabular form 1). Intercomponents Checking Passed: All Server are passed. ====================================================================== 2). OS version Checking... (9 Replies)
Discussion started by: Optimus81
9 Replies

3. Shell Programming and Scripting

Output with horizontal formats

// AIX 5.3 & 6.1 This command powermt display dev=all returns the output of Pseudo name=hdiskpower50 Symmetrix ID=000190101757 Logical device ID=05F0 state=alive; policy=SymmOpt; priority=0; queued-IOs=0 ==============================================================================... (2 Replies)
Discussion started by: Daniel Gate
2 Replies

4. Shell Programming and Scripting

displaying a column in horizontal line separated by ', '

cat my.log blah blah blah < 1 djfh jsdfhk jksdfh < 2 dshkfl opeir pqowi < 4 khasd wouipeui say i am perfroming some action similar to below... cat my.log | egrep "<" | awk -F' ' '{print $2}' | grep -v "" it gives output as below 1 2 4 is there anyway to modify above same... (4 Replies)
Discussion started by: vivek d r
4 Replies

5. Shell Programming and Scripting

Output Parameter value not displaying

Hi all, Below is my shell script that calls a stored procedure(with output parameter) I have tried executing this script but it doesn't display the output parameter value. Can anyone help me ? :( #!/bin/bash ###############################################################################... (7 Replies)
Discussion started by: saviochacko
7 Replies

6. Shell Programming and Scripting

Formatting isql output to horizontal format

Hi I am formatting informix isql output(vertical) to horizontal format. Suppose I have the following content in the flat file from isql output - item_nbr 0 usfn_label Subscriber Class usfn_name SBCLASS usfn_value bl5 item_nbr 1 usfn_label Switch Name usfn_name switchName... (2 Replies)
Discussion started by: nsinha
2 Replies

7. Shell Programming and Scripting

Displaying Output in Columns

I'm writing a script to analyze the logs of an smtp relay machine and I'd like the final output to be displayed in columns showing results from the previous day, week, month, and 45 days. The problem I'm running into is that I can't figure out how to display the columns neatly so there is no... (1 Reply)
Discussion started by: jjamd64
1 Replies

8. Shell Programming and Scripting

Displaying output from df -k in GB instead of KB

Hello all, Code below: echo "Oracle Filesystems" echo "------------------" echo for j in `df -l -k |grep total|grep ora|grep -v storage|grep -v vg00|awk '{print $1}'` do echo $j is `df -l -k $j |grep total|grep ora|grep -v storage|grep -v vg00|awk -F":" '{print $2}'|awk '{print $1}'` KB... (6 Replies)
Discussion started by: LinuxRacr
6 Replies

9. Shell Programming and Scripting

displaying output in a table.

Hello, I've just finished my first script (about displaying open ports on the computer and who opened them) and everything is fine with it but I want to style it a little bit just for the sake of learning how to do this. What I want to do is make the display like the one of ps, for example,... (6 Replies)
Discussion started by: sanchopansa
6 Replies

10. Shell Programming and Scripting

Displaying Script command with output

How do you display the script command for a particular output result? Is there an editor that will allow for an echo or sysout of the script that causes an output result? We have hundreds of lines in the script, which we would like to see along with the output, and don't want to have to put in... (5 Replies)
Discussion started by: JeDi
5 Replies
Login or Register to Ask a Question