-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite-add-record.py
67 lines (53 loc) · 1.77 KB
/
site-add-record.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
#!/usr/local/bin/python
#Required imports for DNSPython
import dns.zone
import dns.query
import dns.update
import dns.rdataset
import dns.resolver
import dns.tsigkeyring
import dns.rdtypes.IN.A
import socket
import sys
import os
from dns.exception import DNSException
from netaddr import IPNetwork, IPAddress
#Get last hectet of an IP address. Used for PTR records
def get_last_octet_number(ipaddress):
octets = ipaddress.split('.')
return octets[-1]
#Get converted data type if fourth parameter is passed. Used to support other record types.
def convertType(type):
choices = {'cname':dns.rdatatype.CNAME, 'a':dns.rdatatype.A, 'mx':dns.rdatatype.MX}
key = type.lower()
return choices.get(key, 'default')
def main():
#Global key
key = dns.tsigkeyring.from_text({
'ddns.key': 'vgwHU16TVZaG6o57OdnMqJGaQf89Rd6/FCfBjEFKQTg='
})
#Declare parametres gathered from webpage.
sub_domain = sys.argv[1]
ip = sys.argv[2]
parentZone = sys.argv[3]
if len(sys.argv) == 5:
data_type = convertType(sys.argv[4])
else:
data_type = dns.rdatatype.A
#Insert query into the DNS BIND09 Server
zone = dns.update.Update(parentZone, keyring=key)
zone.add(sub_domain, 86400, data_type, ip)
response = dns.query.tcp(zone, '127.0.0.1')
try:
#Check if IP address in in Swinburne IP range, ADD PTR record if is.
if (data_type != dns.rdatatype.CNAME):
if (IPAddress(ip) in IPNetwork("136.186.230.0/24")):
full_fqdn_name = sub_domain + '.' + parentZone
reverse_zone = dns.update.Update("230.186.136.in-addr.arpa.", keyring=key)
ip_last_octet = get_last_octet_number(ip)
reverse_zone.add(ip_last_octet, 86400, dns.rdatatype.PTR, full_fqdn_name)
response2 = dns.query.tcp(reverse_zone, '127.0.0.1')
except Exception as e:
print(e)
if __name__ == '__main__':
main()