scripts-misc/powerdns/dns-a-delete

58 lines
2 KiB
Text
Raw Permalink Normal View History

2023-03-31 10:42:21 +02:00
#!/usr/bin/env python3
import json
from findzone import *
import sys
import subprocess
record = sys.argv[1].strip()
ip = sys.argv[2].strip()
apihost = str(os.environ.get('PDNS_PRIMARY_SSH')).split("@")[1]
apischeme = str(os.environ.get('PDNS_API_SCHEME')) if os.environ.get('PDNS_API_SCHEME') else "http"
apiport = int(str(os.environ.get('PDNS_API_PORT'))) if os.environ.get('PDNS_API_PORT') else 8081
zone = findzone(record)
host = record.replace(zone, '').rstrip(".")
reverseip_array = ip.split(".")
reverserecord = f"{reverseip_array[3]}.{reverseip_array[2]}.{reverseip_array[1]}.{reverseip_array[0]}.in-addr.arpa"
reversezone = findzone(reverserecord)
reversehost = reverserecord.replace(reversezone, '').rstrip(".")
if not zone:
print(f"[ERROR] no suitable zone found for {record}")
exit(1)
if not reversezone:
print(f"[ERROR] no suitable reverse zone found for {reverserecord}")
exit(1)
if zone == record:
host = "@"
request_body_a = {
"rrsets": [
{
"changetype": "DELETE",
"type": "A",
"name": f"{record}."
}
]
}
request_body_ptr = {
"rrsets": [
{
"changetype": "DELETE",
"type": "PTR",
"name": f"{reverserecord}."
}
]
}
print(f"+ curl -X PATCH --data '{json.dumps(request_body_a)}' -H 'X-API-Key: *****' {apischeme}://{apihost}:{apiport}/api/v1/servers/localhost/zones/{zone}. -s")
subprocess.check_output(["curl", "-X", "PATCH", "--data", json.dumps(request_body_a), "-H", f'X-API-Key: {str(os.environ.get("PDNS_APIKEY"))}', f"{apischeme}://{apihost}:{apiport}/api/v1/servers/localhost/zones/{zone}.", "-s" ])
print(f"+ curl -X PATCH --data '{json.dumps(request_body_ptr)}' -H 'X-API-Key: *****' {apischeme}://{apihost}:{apiport}/api/v1/servers/localhost/zones/{reversezone}. -s")
subprocess.check_output(["curl", "-X", "PATCH", "--data", json.dumps(request_body_ptr), "-H", f'X-API-Key: {str(os.environ.get("PDNS_APIKEY"))}', f"{apischeme}://{apihost}:{apiport}/api/v1/servers/localhost/zones/{reversezone}.", "-s" ])