-
Notifications
You must be signed in to change notification settings - Fork 1
/
dhcp_script.py
173 lines (153 loc) · 5.83 KB
/
dhcp_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python2
# -*- coding: <UTF-8> -*-
"""In a space with a pfSense Router, get #of active devices and misc. stats."""
import ssl
import os
#import urllib2
import cookielib
from subprocess import Popen, PIPE
from bs4 import BeautifulSoup
import yaml
import mechanize
DEBUG = False
def get_lease_file(current_dir, pfSenseKeySet, pfSenseCfg):
"""Login to pfSense, get DHCP Leases page and save it to a file."""
cj = cookielib.CookieJar()
br = mechanize.Browser()
br.set_cookiejar(cj)
br.set_handle_robots(False)
#Don't verify pfSense's cert signing authority
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
# Legacy Python that doesn't verify HTTPS certificates by default
pass
else:
# Handle target environment that doesn't support HTTPS verification
ssl._create_default_https_context = _create_unverified_https_context
br.open(pfSenseCfg['url'])
br.select_form(nr=0)
br.form['usernamefld'] = pfSenseKeySet['user']
br.form['passwordfld'] = pfSenseKeySet['pass']
br.submit()
htmlFilePath = current_dir + "/status_dhcp_leases.php"
with open(htmlFilePath, 'w') as htmlFile:
htmlFile.write(br.response().read())
return 0
def parse_html(current_dir):
"""Parse DHCP Leases page, return hosts by MAC and their state."""
final = []
htmlFilePath = current_dir + "/status_dhcp_leases.php"
with open(htmlFilePath, 'r') as lease_html:
soup = BeautifulSoup(lease_html.read(), "lxml")
#Get and parse the <table> element
table = soup.find('table', attrs={'class':'table'})
soupT = BeautifulSoup(str(table), "lxml")
body = soupT.find('tbody')
bodys = body.findAll('tr')
for i in range(len(bodys)):
tmp = []
#Get each line of the DHCP lease table body
bodys[i] = bodys[i].findAll('td')
#Add the 3d and 8th element of each line as an array (MAC & online status)
tmp.append(str(bodys[i][2].getText()).translate(None, '\n\t'))
tmp.append(str(bodys[i][7].getText()).translate(None, '\n\t'))
final.append(tmp)
return final
def m_pub(current_dir, mosquittoKeySet, mqttCfg, topicPath, data):
"""Publish at mqtt Instance"""
command = "mosquitto_pub -h "
command += mqttCfg['host']
command += " -p "
command += str(mqttCfg['port'])
command += " -u "
command += mosquittoKeySet['user']
command += " -P "
command += mosquittoKeySet['pass']
command += " --cafile "
command += current_dir
command += mqttCfg['cafile']
command += " -t \""
command += topicPath
command += "\" -m \""
command += data
command += "\""
if DEBUG:
print command
comm = Popen(command, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
stdout, stderr = comm.communicate()
return 0
def send_users(current_dir, mqttKeySet, mqttCfg, noUsers):
"""Construct /hackers subtopic & publish."""
topic = mqttCfg['topicRoot'] + "/hackers"
m_pub(current_dir, mqttKeySet, mqttCfg, topic, str(noUsers))
return 0
def send_new_macs(current_dir, mqttKeySet, mqttCfg, newMACs):
"""Construct /stats topic & publish."""
topic = mqttCfg['topicRoot'] + "/stats"
data = str(len(newMACs))
m_pub(current_dir, mqttKeySet, mqttCfg, topic, data)
return 0
def handle_new_macs(current_dir, mqttKeySet, mqttCfg, newMACs):
"""Add new MACs to the yml file & publish"""
macFilePath = current_dir + "/uniqueMAC.yml"
with open(macFilePath, 'a') as mac_file:
for macAddress in newMACs:
macStr = "\n - " + macAddress
mac_file.write(macStr)
send_new_macs(current_dir, mqttKeySet, mqttCfg, newMACs)
return 0
def handle_users(current_dir, mqttKeySet, mqttCfg, count):
"""Check if #of users has changed & publish"""
lastHackPath = current_dir + "/lastHack.txt"
try:
prevUs = open(lastHackPath, 'r')
except IOError:
prevUs_data = ""
else:
prevUs_data = prevUs.read()
prevUs.close()
if prevUs_data != str(count):
send_users(current_dir, mqttKeySet, mqttCfg, count)
prevUs = open(lastHackPath, 'w')
prevUs.write(str(count))
def main():
"""Read configuration, pass around variables."""
count = 0
newMACs = []
#Read config file and get neutral MAC Addresses, keySets and current_dir
ymlPath = os.path.dirname(os.path.abspath(__file__)) + "/config.yml"
with open(ymlPath, 'r') as ymlfile:
cfg = yaml.load(ymlfile)
staticMAC = cfg['macAddresses']
pfSenseKeySet = cfg['pfSense']
pfSenseCfg = cfg['pfSenseCfg']
mosquittoKeySet = cfg['mosquitto']
mqttCfg = cfg['mqttCfg']
current_dir = cfg['userspace']['cwd']
#Read unique MACs that have been on the network (for statistics)
uniqueMACPath = current_dir + "/uniqueMAC.yml"
with open(uniqueMACPath, 'r') as uniqueMACFile:
mac_file = yaml.load(uniqueMACFile)
uniqueMACs = mac_file['uniqueMAC']
if uniqueMACs is None:
uniqueMACs = []
#Run script to get the HTML containing the DHCP leases
get_lease_file(current_dir, pfSenseKeySet, pfSenseCfg)
#Parse HTML
active_hosts = parse_html(current_dir)
for dataSet in active_hosts:
#If device is online and not one of the neutral device count one
if dataSet[1] == 'online' and dataSet[0] not in staticMAC:
count += 1
if dataSet[0] not in uniqueMACs:
newMACs.append(dataSet[0])
if newMACs:
handle_new_macs(current_dir, mosquittoKeySet, mqttCfg, newMACs)
handle_users(current_dir, mosquittoKeySet, mqttCfg, count)
#Final clean-up
command = "rm " + current_dir + "/status_dhcp_leases.php"
comm = Popen(command, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
stdout, stderr = comm.communicate()
if __name__ == '__main__':
main()