#!/usr/local/bin/python import re import subprocess from subprocess import Popen,PIPE, call # Bsd config file config="/etc/rc.conf.local" blocked_interfaces = ('wlan0' , 'wlan1') # Ping all ips in 'ips' def ping (ips, inf) : all=0 suc=0 # Run through all ips for ip in ips: # Do the ping in a shell and return the exitcode cmd='ping -t 1 %s' % ip ret = subprocess.call(cmd, shell=True, stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT ) # Update Statistics all+=1 if ret : suc+=1 # Overall stats and return them ret = (suc*100/all) return inf + ": " + str(ret) # Get all ips in the subnet (based on ip and mask) def iprange (inf) : ips = [] # Shell command: arp (all) (do not resolve) (interface) command = "arp -ani %s" % inf # Execute command output = Popen(command.split(), stdout=PIPE).communicate()[0] # output can be empty, we don't want that if output: # Multi line output iterate through them for line in output.splitlines(): # Filter ip addresses ips.append( line.split(' ')[1].strip('()') ) return ips # Open the config file and run through it file = open (config) for line in file.readlines(): # Get variable's out of the config file and validate it match = re.match(r'ipv4_addrs_(.*?)="(.*?)/([\d]{1,2})(.*)"$', line) if match: # Get info inf = match.group(1) ip = match.group(2) mask = match.group(3) if inf not in blocked_interfaces: # Call iprange, get all ip's in the subnet allips = iprange(inf) print allips # Call ping, print info if allips: print ping(allips, inf)