Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ping Sweep help
#1
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.
Reply
#2
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

"The good thing about standards is that you have so many to choose from" Andy S. Tanenbaum
Reply
#3
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?
Reply
#4
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

"The good thing about standards is that you have so many to choose from" Andy S. Tanenbaum
Reply
#5
Read This Please
Reply
#6
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need some guidance on a script to ping a list of ip's cubangt 11 1,852 Aug-10-2023, 02:39 PM
Last Post: snippsat
  non-stop ping script kucingkembar 1 1,371 Aug-23-2022, 06:29 AM
Last Post: menator01
  Win32\ping.exe windows pops up -very annoying... tester_V 9 3,236 Aug-12-2021, 06:54 AM
Last Post: tester_V
  Looking for discord bot to make loop ping for address ip tinkode 0 1,836 Jul-26-2021, 03:51 PM
Last Post: tinkode
  Ping command using python 3.6.5 Martin2998 6 17,440 Apr-19-2021, 06:24 PM
Last Post: blazejwiecha
  GPIO high if network IP has good ping duckredbeard 3 2,352 Oct-12-2020, 10:41 PM
Last Post: bowlofred
  Create a program that PING a list of IPs skaailet 7 6,383 Mar-26-2020, 10:46 PM
Last Post: snippsat
  ping with output jacklee26 1 4,951 Nov-28-2019, 01:01 PM
Last Post: Axel_Erfurt
  PING PONG GAME akea 0 5,704 May-08-2019, 04:30 PM
Last Post: akea
  Ping Code Problem MTom5 1 2,772 Sep-04-2018, 09:58 PM
Last Post: MTom5

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020