Skip to content

Instantly share code, notes, and snippets.

@thepacketgeek
Created October 22, 2013 23:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save thepacketgeek/7109829 to your computer and use it in GitHub Desktop.
Save thepacketgeek/7109829 to your computer and use it in GitHub Desktop.
Ping sweep using Python's netaddr
from scapy.all import *
import netaddr
# Define IP range to ping
network = "172.16.20.0/24"
# make list of addresses out of network, set live host counter
addresses = netaddr.IPNetwork(network)
liveCounter = 0
# Send ICMP ping request, wait for answer
for host in addresses:
if (host == addresses.network or host == addresses.broadcast):
continue
resp = sr1(IP(dst=str(host))/ICMP(),timeout=2,verbose=0)
if (str(type(resp)) == "<type 'NoneType'>"):
print str(host) + " is down or not responding."
elif (int(resp.getlayer(ICMP).type)==3 and int(resp.getlayer(ICMP).code) in [1,2,3,9,10,13]):
print str(host) + " is blocking ICMP."
else:
print str(host) + " is responding."
liveCounter += 1
print "Out of " + str(addresses.size) + " hosts, " + str(liveCounter) + " are online."
@adam-4d2e414c57412e5245

Thank you for sharing your knowledge and providing such a simple and straightforward solution from which many will learn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment