Created
October 28, 2020 12:32
-
-
Save EONRaider/a59d6c4636c1211c7a854c384ddf19a0 to your computer and use it in GitHub Desktop.
Execute an ICMP ping scan on a network in Python 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# https://gist.github.com/EONRaider/a59d6c4636c1211c7a854c384ddf19a0 | |
__author__ = 'EONRaider, keybase.io/eonraider' | |
import argparse | |
import ipaddress | |
try: | |
from scapy.all import * | |
from scapy.layers.inet import ICMP, IP | |
except ModuleNotFoundError as e: | |
raise SystemExit(f"Requires {e.name} module. Run 'pip install {e.name}' " | |
f"and try again.") | |
"""Simple subnet ICMP Ping scan. Requires administrator privileges.""" | |
def icmp_ping(netmask: str) -> None: | |
for ip_addr in ipaddress.IPv4Network(netmask): | |
try: | |
ans, unans = sr(IP(dst=str(ip_addr))/ICMP(), timeout=2, verbose=0) | |
if not unans: | |
ans.summary( | |
lambda p: p[1].sprintf('[+] STATUS for %IP.src%: Host UP') | |
) | |
else: | |
print(f'[-] STATUS for {ip_addr}: Host DOWN\n') | |
except KeyboardInterrupt: | |
raise SystemExit('Aborting scan...') | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('subnet', type=str, | |
help='An IP address in CIDR notation defining a subnet ' | |
'to scan.') | |
args = parser.parse_args() | |
icmp_ping(args.subnet) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment