Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #
- # ping the network to look for inactive machines
- # requires scapy-2.2.0 or better
- import argparse
- import time
- import sched
- from scapy.all import sr, srp, IP, UDP, ICMP, TCP, ARP, Ether
- RUN_FREQUENCY = 10
- scheduler = sched.scheduler(time.time, time.sleep)
- def detect_inactive_hosts(scan_hosts):
- """ scan net to see if hosts are up or down
- use ranges like 192.168.2.2-4 see scapy docs """
- global scheduler
- scheduler.enter(RUN_FREQUENCY, 1, detect_inactive_hosts, (scan_hosts, ))
- inactive_hosts = []
- try:
- ans, unans = sr(IP(dst=scan_hosts)/ICMP(), retry=0, timeout=1)
- ans.summary(lambda(s,r) : r.sprintf("%IP.src% is alive"))
- for inactive in unans:
- print "%s is inactive" %inactive.dst
- inactive_hosts.append(inactive.dst)
- print "Total %d hosts are inactive" % (len(inactive_hosts))
- except KeyboardInterrupt:
- exit(0)
- if __name__ == "__main__":
- parser = argparse.ArgumentParser(description=' Python network utils')
- parser.add_argument('--scan-hosts', action="store", dest="scan_hosts",\
- required=True)
- given_args = parser.parse_args()
- scan_hosts = given_args.scan_hosts
- scheduler.enter(1, 1, detect_inactive_hosts, (scan_hosts, ))
- scheduler.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement