Created
August 13, 2017 06:35
-
-
Save logan2211/2855093b5919e8d2f17c23a24fb4394f to your computer and use it in GitHub Desktop.
PowerDNS Pipe Backend for automatic IP -> Hostname
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import socket | |
import struct | |
from sys import stdin, stdout | |
from time import time | |
data = stdin.readline() | |
stdout.write("OK\tDynhost Backend\n") | |
stdout.flush() | |
def hex2ip(hexip): | |
# Return an IP address from a hex encoded decimal IP | |
try: | |
ip = socket.inet_ntoa(struct.pack('!L', int(hexip, 16))) | |
except (ValueError, struct.error): | |
ip = '127.0.0.1' | |
return ip | |
while True: | |
line = stdin.readline().strip() | |
kind, qname, qclass, qtype, id, remoteip = line.split("\t") | |
if qtype == 'SOA': | |
r = "DATA\t{}\t{}\t{}\t{}\t{}\t{} {} {} {} {} {} {}\n".format( | |
qname, qclass, qtype, 86400, id, 'ns.none', 'email.none', | |
int(time()), 172800, 900, 1209600, 3600 | |
) | |
else: | |
resp_ip = hex2ip(qname.split('.')[0]) | |
r = "DATA\t{}\t{}\t{}\t{}\t{}\t{}\n".format( | |
qname, qclass, 'A', 86400, id, resp_ip | |
) | |
stdout.write(r) | |
stdout.write("END\n") | |
stdout.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment