0

I've written a simple script that allows me to use a while loop to iterate a ping command over an ip range.

I'm unsure as to why I keep getting this message back afterwards:

Example of feedback from Terminal

The code is as follows:

import os

ipnum = 0

iprange = '10.11.1.%s' % (ipnum)
while ipnum < 255:
    os.system("ping " + iprange)
    ipnum + 1**

Any help would be appreciated thanks.

4
  • Just to note I did import os - the question omitted that part out. Commented Nov 7, 2019 at 17:02
  • Did you mean ipnum += 1? Commented Nov 7, 2019 at 17:02
  • Hi thanks Zain I changed the code to the += operator - still getting the error message. Commented Nov 7, 2019 at 17:04
  • 1
    You seem to be pinging 10.11.1.0 continuously, because the string doesn't magically update once ipnum does. Commented Nov 7, 2019 at 17:05

3 Answers 3

1

It sounds like what you want is something like:

for ipnum in range(0, 255):
    os.system("ping 10.11.1.%s" % ipnum)

What your code currently does is ping 10.11.1.0 255 times, because the string doesn't update just because ipnum does. You format it once at the beginning and then don't modify it.

Sign up to request clarification or add additional context in comments.

Comments

1

You use some tools like nmap, angryip. If you want a python, we can rewrite that

#!/usr/bin/python
import subprocess
target = 200
up = 0
down = 0
while (target < 255):
    ip = "10.11.1." +str(target)
    output = subprocess.Popen(["ping","-c","1",ip],stdout = subprocess.PIPE).communicate()[0]

    if ('Unreachable' in output):
            print 'Host ' + ip + " is offline or unavailable"
            down+= 1
    else:
            print "Host " + ip + " is online"
            up+= 1

    target = target+1


print "A total of " + str(up+down) + " hosts were scanned."
print str(up) + " hosts were alive, and " + str(down) + " hosts were unreachable. "
quit()

Comments

0

If all you're trying to do is enumerate a list of active IPs in given a range, why not use something that already exists for that purpose?

Checkout nmap:

sudo nmap -sn -n 10.11.1.0- -oG - | grep -i Host | cut -d" " -f2

The above command:

  • nmap : scan the netwrok
  • -sn + -sP : skip port scanning
    • faster host discovery
  • -n : skip reverse dns resolution
    • faster host discovery
  • 10.11.1.- : enumerate over 10.11.1.0
    • the dash specifies to enumerate
  • -oG - : convert the output for data processing
  • grep -i Host : filter the output to only show enumerated values
    • removes the header and footer of the nmap output
  • cut -d" " -f2 : split the output into columns separated by spaces, then filter to only show the second column.
    • shows just the ip addresses

Sample output

sudo nmap -sn -n 10.11.1.0.- -oG - | grep Host | cut -d" " -f2                                                            
# 10.11.1.0.1
# 10.11.1.0.100
# 10.11.1.0.101
# 10.11.1.0.103
# 10.11.1.0.106
# 10.11.1.0.169
# 10.11.1.0.108

Nmap supports scanning the current hosts' network range, as well any other network range.


If you need to execute this directly from python, use the subprocess module. https://docs.python.org/2/library/subprocess.html


More information about nmap:

https://linux.die.net/man/1/nmap

2 Comments

Because it's an exercise I need to get my head around in Python. I'm aware of the various tools and Bash but I need to write this in Python. Thanks though
Its actually very common for scripts in other languages to call bash scripts or external tools like nmap. Since this was tagged with linux and bash, I assumed my solution could be appropriate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.