2021-12-19 21:32:19 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#!/usr/bin/env python3
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# File: generate-pihole-dns-dhcp.py
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-01 00:44:23 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-19 21:32:19 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# Description: This file generates the DNS and DHCP files for pihole.
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-01 00:44:23 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#     It expects that the inventory has two levels of grouping.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#
							 | 
						
					
						
							
								
									
										
										
										
											2025-04-12 02:59:24 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								# Package: AniNIX/Kapisi
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-19 21:32:19 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# Copyright: WTFPL
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-01 00:44:23 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-19 21:32:19 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# Author: DarkFeather <darkfeather@aninix.net>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import os
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-01 00:44:23 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import re
							 | 
						
					
						
							
								
									
										
										
										
											2022-05-23 21:30:24 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import subprocess
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-19 21:32:19 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import sys
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-01 00:44:23 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								from kapisi_lib import *
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-19 21:32:19 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-01 00:44:23 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								rolepath='../roles/Chappaai/files'
							 | 
						
					
						
							
								
									
										
										
										
											2022-03-25 06:08:12 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								dnsfilepath=rolepath+"/dns"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								dhcpfilepath=rolepath+"/dhcp"
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-01 00:44:23 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								entryset={}
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-19 21:32:19 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-04-12 02:59:24 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								def WriteDHCPEntries(dhcpfile):
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-19 21:32:19 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    ### Create the DHCP entry
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    # param content: the yaml content to parse
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    # param hosttype: managed or unmanaged
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    # param hostclass: the type of host as classified in the yaml
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-01 00:44:23 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    global entryset
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    for host in entryset:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Entries should be:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # dhcp-host=mac,ip,fqdn
							 | 
						
					
						
							
								
									
										
										
										
											2025-04-12 02:59:24 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        dhcpfile.write('dhcp-host=' + entryset[host][1] + ',' + entryset[host][0] + ',' + entryset[host][2] + '\n')
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-19 21:32:19 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2025-04-12 02:59:24 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								def WriteDNSEntries(dnsfile):
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-19 21:32:19 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    ### Create the DNS entry
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    # param content: the yaml content to parse
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    # param hosttype: managed or unmanaged
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    # param hostclass: the type of host as classified in the yaml
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-01 00:44:23 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    global entryset
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    for host in entryset:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Entries should be:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # ip host fqdn
							 | 
						
					
						
							
								
									
										
										
										
											2025-04-12 02:59:24 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        dnsfile.write(entryset[host][0] + ' ' + entryset[host][2] + ' ' + host + '\n')
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-19 21:32:19 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								def GenerateFiles(file):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    ### Open the file and parse it
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    # param file: the file to work on
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    global dnsfile
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2022-03-25 06:08:12 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if not os.path.isdir(rolepath):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        os.mkdir(rolepath)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-19 21:32:19 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    # Parse the yaml
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    with open(file, 'r') as stream:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        content = yaml.safe_load(stream)
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-01 00:44:23 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        external_domain = content['all']['vars']['external_domain']
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-19 21:32:19 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Clear the DNS file
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        with open(dhcpfilepath,'w') as dhcpfile:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            dhcpfile.write('dhcp-range='+content['all']['vars']['dhcprange']+'\n')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            dhcpfile.write('dhcp-option=option:dns-server,'+content['all']['vars']['dns']+'\n\n')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            dhcpfile.write('dhcp-range='+content['all']['vars']['staticrange']+'\n')
							 | 
						
					
						
							
								
									
										
										
										
											2025-04-12 02:59:24 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            WriteDHCPEntries(dhcpfile)
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-19 21:32:19 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        with open(dnsfilepath,'w') as dnsfile:
							 | 
						
					
						
							
								
									
										
										
										
											2025-10-21 13:54:23 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            dnsfile.write(content['all']['vars']['webfront']+' '+external_domain+' '+content['all']['vars']['internal_subdomains'].replace(' ','.'+external_domain+' ')+'.'+external_domain+' '+content['all']['vars']['external_subdomains'].replace(' ','.'+external_domain+' ')+'.'+external_domain+' '+content['all']['vars']['hosted_domains']+"\n")
							 | 
						
					
						
							
								
									
										
										
										
											2025-04-12 02:59:24 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            WriteDNSEntries(dnsfile)
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-01 00:44:23 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            print('Files should be in '+rolepath);
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-19 21:32:19 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-01 00:44:23 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								### Main function
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# param sys.argv: Input arguments
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-19 21:32:19 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								if __name__ == '__main__':
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-01 00:44:23 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if len(sys.argv) < 2:
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-19 21:32:19 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        print("You need to supply an inventory file.")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        sys.exit(1)
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-01 00:44:23 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if len(sys.argv) == 3:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        entryset = TrackIPEntries(sys.argv[1],sys.argv[2])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        entryset = TrackIPEntries(sys.argv[1])
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-19 21:32:19 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    GenerateFiles(sys.argv[1])
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-01 00:44:23 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    #dumper.dump(entryset)
							 | 
						
					
						
							
								
									
										
										
										
											2021-12-19 21:32:19 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    sys.exit(0)
							 |