Python Ping scan
Posted on February 27, 2013
- and tagged as
- networking,
- python
A simple ping scan can be performed with any number of tools, from nmap to a cmd.exe, however, doing it through Python allows us to easily run additional code if a host is up.
The netaddr and ping modules are required.
Syntax is script.py -nw [subnet in CIDR format]
import argparse
import netaddr
import ping
parser = argparse.ArgumentParser()
parser.add_argument(''-nw'',''--network'', type=str, required=True, help="Network to be scanned in CIDR format (eg. 192.168.1.0/24)", dest=''subnet'')
args = parser.parse_args()
subnet = args.subnet
nw = netaddr.IPNetwork(subnet)
def pingtest(target):
# ping target, returns True if target responds, otherwise returns False
r = ping.quiet_ping(target,timeout=0.5,count=1)
if r[0] != 100:
return(True)
else:
return(False)
for ip in nw.iter_hosts():
x = pingtest(str(ip))
if x == True:
print(str(ip) + " is up...")
# do other stuff