3 Replies - 307 Views - Last Post: 22 February 2014 - 07:15 PM Rate Topic: -----

#1 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Code adding a new line when it shouldn't...

Posted 22 February 2014 - 02:12 AM

I'm new to Python, but this is driving me nuts. For some reason when I try to print out the ip addresses in my for loop I get the last octet on a new line. I know Python is white-space specific sometimes, but I've tried moving the %s and variable in % () everywhere with no luck. Here's what I'm using:

#!/usr/bin/python

import os
from array import *

f = os.popen('/sbin/ifconfig | awk \'/inet addr/ && !/127.0.0.1/ {print substr($2,6)}\' | sed \'s/.//8g\'')
now = f.read()

pint=1
pingit = "ping -c 1 %s" % (now)

for i in range(1,255):
    print "%s%s" % (pingit,i)



Any advice appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Code adding a new line when it shouldn't...

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • Joined: 21-June 11

Re: Code adding a new line when it shouldn't...

Posted 22 February 2014 - 12:12 PM

The output produced by ping ends with a newline. You can use rstrip to remove any line breaks (and other white space) at the end of the string.
Was This Post Helpful? 1
  • +
  • -

#3 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 528
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Code adding a new line when it shouldn't...

Posted 22 February 2014 - 03:40 PM

What sepp2k said is correct but it actually starts with your popen call. I looked at your code with:

f = os.popen('/sbin/ifconfig | awk \'/inet addr/ && !/127.0.0.1/ {print substr($2,6)}\' | sed \'s/.//8g\'')
now = f.read()
print "%r" % now



And the output received is '192.168\n' so a newline needs to be handled with the popen call. Here's rstrip:

now = str.rstrip(f.read())
print "%r" % now



So, now the output is '192.168'.

Moving on to the next bit of code:

pint=1
pingit = "ping -c 1 %s" % (now)
print "%r" % pingit



The resulting output is 'ping -c 1 192.168' so you have an almost valid command.

Without running the for loop, let's just see what it would be at 5.

pingit = "ping -c 1 %s" % (now)
print "%s%s" % (pingit,5)



And that produces this output: ping -c 1 192.1685 which is not valid.

So, you are missing some octets. I see that you have pint=1 and it's not used, so let's just plop that in there:

print "%s.%s.%s" % (pingit, pint, 5)



Now the output is: ping -c 1 192.168.1.5 which is valid.

So, let's put that in the for loop now:

for i in xrange(255):
	print "%s.%s.%s" % (pingit, pint, i+1)



I changed it to xrange and also I offset the count in the display itself with i+1. These are implementation details that you are welcome to change if you like.

output said:

ping -c 1 192.168.1.1
ping -c 1 192.168.1.2
ping -c 1 192.168.1.3
{snip -- brevity}
ping -c 1 192.168.1.253
ping -c 1 192.168.1.254
ping -c 1 192.168.1.255


And so there you have it. Hope this helps.

#!/usr/bin/python

import os

f = os.popen('/sbin/ifconfig | awk \'/inet addr/ && !/127.0.0.1/ {print substr($2,6)}\' | sed \'s/.//8g\'')
now = str.rstrip(f.read())

pint=1
pingit = "ping -c 1 %s" % (now)

for i in xrange(255):
	print "%s.%s.%s" % (pingit, pint, i+1)



EDIT: I don't know what from array import * is for. I removed it.

This post has been edited by MentalFloss: 22 February 2014 - 03:43 PM

Was This Post Helpful? 2
  • +
  • -

#4 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: Code adding a new line when it shouldn't...

Posted 22 February 2014 - 07:15 PM

Nice! Guess I have a lot more to learn about debugging with Python. The "from array import *" was something I found searching for arrays, but it seems to work fine without it. Thank you both!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1