Skip to content

Instantly share code, notes, and snippets.

@fryguy04
Created June 23, 2019 15:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save fryguy04/7d12b789260c47c571f42e5bc733a813 to your computer and use it in GitHub Desktop.
Save fryguy04/7d12b789260c47c571f42e5bc733a813 to your computer and use it in GitHub Desktop.
Scrape pfSense DHCP List into Python Struct
#!/usr/bin/env python
# Scrapes pfSense DHCP Leases into List of (IP, MAC, Hostname) format.
# Change URL/Username/Password below ... pip install lxml ... then you are all set.
#
# Modified 6/23/2019 (FryGuy)
# Edits: Aligned IP/MAC/Hostname into struct accounting for blank lines
# Minor: Cleaned up spacing, created global url/user/password vars, removed write to file
# Original Code/Inspiration: https://gist.github.com/pletch/037a4a01c95688fff65752379534455f
import sys
import requests
from lxml import html
import re
import time
import json
# Change these to your network
url = "https://192.168.1.1/status_dhcp_leases.php" #change url to match your pfsense machine address
user = 'admin'
password = 'CHANGEME'
def scrape_pfsense_dhcp(url, user, password):
ip = []
mac = []
dhcp_name = []
s = requests.session()
r = s.get(url,verify = False)
matchme = 'csrfMagicToken = "(.*)";var'
csrf = re.search(matchme,str(r.text))
payload = {
'__csrf_magic' : csrf.group(1),
'login' : 'Login',
'usernamefld' : user,
'passwordfld' : password
}
r = s.post(url,data=payload,verify = False)
r = s.get(url,verify = False)
tree = html.fromstring(r.content)
ip.extend([element.text for element in tree.xpath('//body[1]//div[1]//div[1]//div[2]//table[1]//tbody//tr//td[2]')])
mac.extend([element.text for element in tree.xpath('//body[1]//div[1]//div[1]//div[2]//table[1]//tbody//tr//td[3]')])
for node in tree.xpath('//body[1]//div[1]//div[1]//div[2]//table[1]//tbody//tr//td[4]'):
if node.text is None:
dhcp_name.append('no_hostname')
else:
dhcp_name.append(node.text)
for i in range(len(mac)):
mac[i] = mac[i].strip()
dhcp = list(zip(ip, mac, dhcp_name))
return(dhcp)
if __name__ == "__main__":
dhcp_list = scrape_pfsense_dhcp(url, user, password)
for x in dhcp_list:
print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment