Advertisement
rfmonk

scapy_icmp.py

Jun 26th, 2014
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # ping the network to look for inactive machines
  4. # requires scapy-2.2.0 or better
  5.  
  6.  
  7. import argparse
  8. import time
  9. import sched
  10. from scapy.all import sr, srp, IP, UDP, ICMP, TCP, ARP, Ether
  11. RUN_FREQUENCY = 10
  12. scheduler = sched.scheduler(time.time, time.sleep)
  13.  
  14.  
  15. def detect_inactive_hosts(scan_hosts):
  16.     """ scan net to see if hosts are up or down
  17.     use ranges like 192.168.2.2-4 see scapy docs """
  18.     global scheduler
  19.     scheduler.enter(RUN_FREQUENCY, 1, detect_inactive_hosts, (scan_hosts, ))
  20.     inactive_hosts = []
  21.     try:
  22.         ans, unans = sr(IP(dst=scan_hosts)/ICMP(), retry=0, timeout=1)
  23.         ans.summary(lambda(s,r) : r.sprintf("%IP.src% is alive"))
  24.         for inactive in unans:
  25.             print "%s is inactive" %inactive.dst
  26.             inactive_hosts.append(inactive.dst)
  27.         print "Total %d hosts are inactive" % (len(inactive_hosts))
  28.     except KeyboardInterrupt:
  29.         exit(0)
  30.  
  31. if __name__ == "__main__":
  32.     parser = argparse.ArgumentParser(description=' Python network utils')
  33.     parser.add_argument('--scan-hosts', action="store", dest="scan_hosts",\
  34.         required=True)
  35.     given_args = parser.parse_args()
  36.     scan_hosts = given_args.scan_hosts
  37.     scheduler.enter(1, 1, detect_inactive_hosts, (scan_hosts, ))
  38.     scheduler.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement