1 | #!/usr/local/bin/python
|
---|
2 |
|
---|
3 | import os
|
---|
4 | import re
|
---|
5 |
|
---|
6 | # Bsd config file
|
---|
7 | config="/etc/rc.conf.local"
|
---|
8 |
|
---|
9 | def gettrees () :
|
---|
10 | ip = []
|
---|
11 |
|
---|
12 | # Run through all the files in /tmp
|
---|
13 | for filename in os.listdir('/tmp'):
|
---|
14 |
|
---|
15 | # Match lvrouted tree files and get ip address
|
---|
16 | # lvrouted.tree-172.16.4.9
|
---|
17 | match = re.match(r'lvrouted.tree-(.*)$', filename)
|
---|
18 | if match:
|
---|
19 |
|
---|
20 | # append ip address to list
|
---|
21 | ip.append(match.group(1))
|
---|
22 |
|
---|
23 | return ip
|
---|
24 |
|
---|
25 | # Get all ips in the subnet (based on ip and mask)
|
---|
26 | def iprange (ip, mask) :
|
---|
27 |
|
---|
28 | # Max number of bits in the subnetmask
|
---|
29 | max=32
|
---|
30 |
|
---|
31 | # Make sure the mask is integer
|
---|
32 | mask=int(mask)
|
---|
33 |
|
---|
34 | # Don't do anything with certain subnet sizes
|
---|
35 | if mask > 27 and mask < 32 :
|
---|
36 |
|
---|
37 | # Make an ip list
|
---|
38 | iplist = []
|
---|
39 |
|
---|
40 | # Split the ip
|
---|
41 | oc = ip.split(".")
|
---|
42 |
|
---|
43 | # Calculate the total subnet size
|
---|
44 | max = 2 ** ( max - mask )
|
---|
45 |
|
---|
46 | # Make sure the last oclet of the ip is integer
|
---|
47 | oc[3] = int(oc[3])
|
---|
48 |
|
---|
49 | # Calculate the lower end of the subnet
|
---|
50 | min = oc[3] - ( oc[3] % max ) + 1
|
---|
51 |
|
---|
52 | # Calculate the upper end of the subnet
|
---|
53 | max = min + max - 2
|
---|
54 |
|
---|
55 | # Run through all possible ip's
|
---|
56 | for oc3 in range(min, max):
|
---|
57 |
|
---|
58 | # Add Ip to iplist
|
---|
59 | iplist.append(str(oc[0]) + "." + str(oc[1]) + "." + str(oc[2]) + "." + str(oc3))
|
---|
60 |
|
---|
61 | # Return the iplist to the caller
|
---|
62 | return iplist
|
---|
63 |
|
---|
64 | # Open the config file and run through it
|
---|
65 | file = open (config)
|
---|
66 | treeips = gettrees()
|
---|
67 | invalid = []
|
---|
68 | rcips = []
|
---|
69 |
|
---|
70 | for line in file.readlines():
|
---|
71 |
|
---|
72 | # Get variable's out of the config file and validate it
|
---|
73 | match = re.match(r'ipv4_addrs_(.*?)="(.*?)/([\d]{1,2})(.*)"$', line)
|
---|
74 | if match:
|
---|
75 |
|
---|
76 | # Get info
|
---|
77 | iface = match.group(1)
|
---|
78 | ip = match.group(2)
|
---|
79 | mask = match.group(3)
|
---|
80 |
|
---|
81 | # Call iprange, get all ip's in the subnet
|
---|
82 | allips = iprange(ip, mask)
|
---|
83 |
|
---|
84 | # Don't iterate through empty list
|
---|
85 | if allips:
|
---|
86 |
|
---|
87 | valid=0
|
---|
88 |
|
---|
89 | # Check if one of the ip's in the lvrouted list (one must be present)
|
---|
90 | for rangeip in allips:
|
---|
91 | if rangeip in treeips:
|
---|
92 | valid=1
|
---|
93 |
|
---|
94 | if not valid:
|
---|
95 | invalid.append(iface)
|
---|
96 |
|
---|
97 | if invalid:
|
---|
98 | retval = "LV ERROR:"
|
---|
99 | for iface in invalid:
|
---|
100 | retval = retval + " " + iface
|
---|
101 | print retval
|
---|
102 | exit(2)
|
---|
103 | else:
|
---|
104 | print "LV OK"
|
---|
105 | exit(0)
|
---|
106 |
|
---|
107 |
|
---|