Python Forum

Full Version: Ping Sweep help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all,

I'm currently learning Python while having while having to make use of it for a course. Would someone be able to help with explaining some parts of this Ping Sweep that I don't understand? I have some beginner level java programming experience from 6 years but I'm definitely rusty.

import subprocess
#I don't understand this command.

for ping in range(1,254):
#this defines a range of numbers for "ping". I think I have an understanding of this.

   address = "10.11.1." + str(ping)
#creates the address variable with "10.11.1" in the first 3 octets

   res = subprocess.call(['ping', '-c', '3', address])
#I only understand what's happening in the brackets and it's creating the res variable.

   if res == 0:
       print "ping to", address, "OK"
#The output if res = 0

   elif res == 2:
       print "no response from", address
#The output if res = 2, I take it you use "elif" if there are more than 2 possible outcomes?

   else:
       print "ping to", address, "failed!"
#The output if nothing matches the previous 2 outcomes.
Hi,

My thoughts on the questions you asked.

Some brief answers, not sure how much detail you need, so please feel free to ask...

import subprocess

The import command "imports" a module, in this case called subprocess. This will contain standard pieces of code that the program you are writing will use.

for ping in range(1,254):
This will loop from 1( as in the 1 before the comma) unitl 254.




if res == 0:

This if statement will process the code below the colon ":" that is indented by 4 spaces, if the "res" variables equals zero "0"
Once the your code below stops being indented then it will no longer be executed as part of this "if" statement.
#The output if res = 0

elif res == 2:
print "no response from", address
#The output if res = 2, I take it you use "elif" if there are more than 2 possible outcomes?
Correct - code executed as above

else:
print "ping to", address, "failed!"
#The output if nothing matches the previous 2 outcomes.

Correct - code executed as above

Hope this helps

Bass
AWESOME, Thanks! I that helps a lot now when I try to run it in Linux I get a "syntax error near unexpected token ('" do you know what could be causing that?
Hi,
I don't use Linux as such, so not fully qualified to answer, but...

Doing a quick search on Google, it looks like your Python script is being run outside of Python and to fix this you need to add this line to the top of your script:
#!/usr/bin/python

This should ensure that the script is executed in Python. I may be wrong - but it should be worth a try. Add the line above and see what happens...

Good Luck

Bass
Read This Please
Since Python 3.3 there is a nice module in the stdlib for ipaddress.

import ipaddress
start_ip = ipaddress.ip_address('192.168.0.1')
for i in range(254):
   print(start_ip + i)

21.28. ipaddress — IPv4/IPv6 manipulation library

If you want to get a range for a whole network, you should use following:
ip_network_24 = ipaddress.ip_network('192.168.0.0/24') # Netmask = 255.255.255.0
ip_network_16 = ipaddress.ip_network('192.168.0.0/16') # Netmask = 255.255.0.0

print('Whole 192.168.0.0/24 network:')
for ip in ip_network_24.hosts():
   print(ip)
# Network Address and Braodcast Address are excluded.
# 254 hosts

print('Whole 192.168.0.0/16 network:')
for ip in ip_network_16.hosts():
   print(ip)
# 65534 hosts
This module saves you against string manipulation. All important logic is inside the module and has a good abstraction. This saves brain-time.