2021-12-19 21:32:19 -06:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# File: generate-pihole-dns-dhcp.py
|
|
|
|
#
|
|
|
|
# Description: This file generates the DNS and DHCP files for pihole.
|
|
|
|
#
|
|
|
|
# Package: AniNIX/Ubiqtorate
|
|
|
|
# Copyright: WTFPL
|
|
|
|
#
|
|
|
|
# Author: DarkFeather <darkfeather@aninix.net>
|
|
|
|
|
|
|
|
import os
|
2022-05-23 21:30:24 -05:00
|
|
|
import subprocess
|
2021-12-19 21:32:19 -06:00
|
|
|
import sys
|
|
|
|
import yaml
|
|
|
|
|
2022-03-25 06:08:12 -05:00
|
|
|
rolepath='../roles/Nazara/files'
|
|
|
|
dnsfilepath=rolepath+"/dns"
|
|
|
|
dhcpfilepath=rolepath+"/dhcp"
|
2021-12-19 21:32:19 -06:00
|
|
|
|
|
|
|
def WriteDHCPEntry(content,hosttype,hostclass):
|
|
|
|
### 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
|
|
|
|
global dhcpfile
|
|
|
|
|
|
|
|
with open(dhcpfilepath,'a') as dhcpfile:
|
|
|
|
for host in content['all']['children'][hosttype]['children'][hostclass]['hosts']:
|
|
|
|
try:
|
2022-03-25 06:08:12 -05:00
|
|
|
dhcpfile.write('dhcp-host=' + content['all']['children'][hosttype]['children'][hostclass]['hosts'][host]['mac'] + ',' + content['all']['children'][hosttype]['children'][hostclass]['hosts'][host]['ip'] + ',' + host + '.' + content['all']['vars']['replica_domain'] + '\n')
|
2021-12-19 21:32:19 -06:00
|
|
|
except:
|
|
|
|
print(host + ' is not complete for DHCP.')
|
|
|
|
|
|
|
|
def WriteDNSEntry(content,hosttype,hostclass):
|
|
|
|
### 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
|
|
|
|
global dnsfile
|
|
|
|
|
|
|
|
with open(dnsfilepath,'a') as dnsfile:
|
2022-05-23 21:30:24 -05:00
|
|
|
|
|
|
|
# Write host entries
|
2021-12-19 21:32:19 -06:00
|
|
|
for host in content['all']['children'][hosttype]['children'][hostclass]['hosts']:
|
|
|
|
try:
|
2022-03-25 06:08:12 -05:00
|
|
|
dnsfile.write(content['all']['children'][hosttype]['children'][hostclass]['hosts'][host]['ip'] + ' ' + host + '.' + content['all']['vars']['replica_domain'] + ' ' + host + '\n')
|
2021-12-19 21:32:19 -06:00
|
|
|
except:
|
|
|
|
print(host + ' is not complete for DNS.')
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
# 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')
|
|
|
|
with open(dnsfilepath,'w') as dnsfile:
|
2023-02-22 21:54:52 -06:00
|
|
|
vips=subprocess.run(["/bin/bash", "-c", "echo | openssl s_client -connect "+content['all']['vars']['external_domain']+":443 | openssl x509 -text -noout | grep DNS: | tr ',' '\n' | sed 's/\s\+DNS://' | grep -ivE ^"+content['all']['vars']['external_domain']+" | tr '\n' ' '"], capture_output=True).stdout.decode("utf-8")
|
2022-05-23 21:30:24 -05:00
|
|
|
dnsfile.write(content['all']['vars']['webfront']+' '+content['all']['vars']['external_domain']+' '+vips+"\n")
|
2021-12-19 21:32:19 -06:00
|
|
|
|
|
|
|
# Add DNS entries for each host
|
|
|
|
hosttype = 'managed'
|
2022-03-25 06:08:12 -05:00
|
|
|
for hostclass in ['physical','virtual','geth_hubs']:
|
2021-12-19 21:32:19 -06:00
|
|
|
WriteDNSEntry(content,hosttype,hostclass)
|
|
|
|
WriteDHCPEntry(content,hosttype,hostclass)
|
|
|
|
hosttype = 'unmanaged'
|
2023-10-08 12:28:14 -05:00
|
|
|
for hostclass in ['ovas','test_ovas','appliances','adhoc_appliances','iot']:
|
2021-12-19 21:32:19 -06:00
|
|
|
WriteDNSEntry(content,hosttype,hostclass)
|
|
|
|
WriteDHCPEntry(content,hosttype,hostclass)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if len(sys.argv) != 2:
|
|
|
|
print("You need to supply an inventory file.")
|
|
|
|
sys.exit(1)
|
|
|
|
GenerateFiles(sys.argv[1])
|
|
|
|
sys.exit(0)
|