#!/usr/local/bin/python import os import re # Bsd config file config="/etc/rc.conf.local" def gettrees () : ip = [] # Run through all the files in /tmp for filename in os.listdir('/tmp'): # Match lvrouted tree files and get ip address # lvrouted.tree-172.16.4.9 match = re.match(r'lvrouted.tree-(.*)$', filename) if match: # append ip address to list ip.append(match.group(1)) return ip # Get all ips in the subnet (based on ip and mask) def iprange (ip, mask) : # Max number of bits in the subnetmask max=32 # Make sure the mask is integer mask=int(mask) # Don't do anything with certain subnet sizes if mask > 27 and mask < 32 : # Make an ip list iplist = [] # Split the ip oc = ip.split(".") # Calculate the total subnet size max = 2 ** ( max - mask ) # Make sure the last oclet of the ip is integer oc[3] = int(oc[3]) # Calculate the lower end of the subnet min = oc[3] - ( oc[3] % max ) + 1 # Calculate the upper end of the subnet max = min + max - 2 # Run through all possible ip's for oc3 in range(min, max): # Add Ip to iplist iplist.append(str(oc[0]) + "." + str(oc[1]) + "." + str(oc[2]) + "." + str(oc3)) # Return the iplist to the caller return iplist # Open the config file and run through it file = open (config) treeips = gettrees() invalid = [] rcips = [] 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 iface = match.group(1) ip = match.group(2) mask = match.group(3) # Call iprange, get all ip's in the subnet allips = iprange(ip, mask) # Don't iterate through empty list if allips: valid=0 # Check if one of the ip's in the lvrouted list (one must be present) for rangeip in allips: if rangeip in treeips: valid=1 if not valid: invalid.append(iface) if invalid: retval = "LV ERROR:" for iface in invalid: retval = retval + " " + iface print retval exit(2) else: print "LV OK" exit(0)