83 lines
2.9 KiB
Python
Executable File
83 lines
2.9 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# File: generate-pihole-dns-dhcp.py
|
|
#
|
|
# Description: This file generates the DNS and DHCP files for pihole.
|
|
# It expects that the inventory has two levels of grouping.
|
|
#
|
|
# Package: AniNIX/Ubiqtorate
|
|
# Copyright: WTFPL
|
|
#
|
|
# Author: DarkFeather <darkfeather@aninix.net>
|
|
|
|
import os
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
from kapisi_lib import *
|
|
|
|
rolepath='../roles/Chappaai/files'
|
|
dnsfilepath=rolepath+"/dns"
|
|
dhcpfilepath=rolepath+"/dhcp"
|
|
entryset={}
|
|
|
|
def WriteDHCPEntries(replica_domain,dhcpfile):
|
|
### 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 entryset
|
|
for host in entryset:
|
|
# Entries should be:
|
|
# dhcp-host=mac,ip,fqdn
|
|
dhcpfile.write('dhcp-host=' + entryset[host][1] + ',' + entryset[host][0] + ',' + host + '.' + replica_domain + '\n')
|
|
|
|
def WriteDNSEntries(replica_domain,dnsfile):
|
|
### 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 entryset
|
|
for host in entryset:
|
|
# Entries should be:
|
|
# ip host fqdn
|
|
dnsfile.write(entryset[host][0] + ' ' + host + '.' + replica_domain + ' ' + host + '\n')
|
|
|
|
def GenerateFiles(file):
|
|
### Open the file and parse it
|
|
# param file: the file to work on
|
|
global dnsfile
|
|
|
|
if not os.path.isdir(rolepath):
|
|
os.mkdir(rolepath)
|
|
|
|
# Parse the yaml
|
|
with open(file, 'r') as stream:
|
|
content = yaml.safe_load(stream)
|
|
replica_domain = content['all']['vars']['replica_domain']
|
|
external_domain = content['all']['vars']['external_domain']
|
|
|
|
# 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')
|
|
WriteDHCPEntries(replica_domain,dhcpfile)
|
|
with open(dnsfilepath,'w') as dnsfile:
|
|
dnsfile.write(content['all']['vars']['webfront']+' '+external_domain+' '+content['all']['vars']['external_subdomains'].replace(' ','.'+external_domain+' ')+'.'+external_domain+' '+content['all']['vars']['hosted_domains']+"\n")
|
|
WriteDNSEntries(replica_domain,dnsfile)
|
|
print('Files should be in '+rolepath);
|
|
|
|
### Main function
|
|
# param sys.argv: Input arguments
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) < 2:
|
|
print("You need to supply an inventory file.")
|
|
sys.exit(1)
|
|
if len(sys.argv) == 3:
|
|
entryset = TrackIPEntries(sys.argv[1],sys.argv[2])
|
|
else:
|
|
entryset = TrackIPEntries(sys.argv[1])
|
|
GenerateFiles(sys.argv[1])
|
|
#dumper.dump(entryset)
|
|
sys.exit(0)
|