-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsite-delete-record.py
More file actions
80 lines (60 loc) · 2.04 KB
/
Copy pathsite-delete-record.py
File metadata and controls
80 lines (60 loc) · 2.04 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/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 the last didgits from an IP address.
#Used for removing a PTR record
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')
#Datatype to String
def get_datatype_string(getType):
choices = {dns.rdatatype.CNAME:'CNAME', dns.rdatatype.A:'A', dns.rdatatype.MX:'MX'}
return choices.get(getType, 'default')
def main():
#Define global key
key = dns.tsigkeyring.from_text({
'ddns.key': 'vgwHU16TVZaG6o57OdnMqJGaQf89Rd6/FCfBjEFKQTg='
})
ip = None
#Declare parametres gathered from webpage.
sub_domain = sys.argv[1]
parentZone = sys.argv[2]
if len(sys.argv) == 4:
data_type = convertType(sys.argv[3])
else:
data_type = dns.rdatatype.A
full_fqdn_name = sub_domain + '.' + parentZone
#Only get FQDN if IP is used
if (get_datatype_string(data_type) != "CNAME"):
ip = socket.gethostbyname(full_fqdn_name)
#Insert query into the DNS BIND09 Server
zone = dns.update.Update(parentZone, keyring=key)
update = zone.delete(sub_domain, data_type)
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 (IPAddress(ip) in IPNetwork("136.186.230.0/24")):
reverse_zone = dns.update.Update("230.186.136.in-addr.arpa.", keyring=key)
ip_last_octet = get_last_octet_number(ip)
pdate2 = reverse_zone.delete(ip_last_octet, dns.rdatatype.PTR)
response2 = dns.query.tcp(reverse_zone, '127.0.0.1')
except Exception as e:
print(e)
if __name__ == '__main__':
main()