Example dynamic resolver. Maps DNS labels to shell commands and returns result as TXT record (Note: No context is passed to the shell command) Shell commands are passed in a a list in : format - eg: [ 'uptime.abc.com.:uptime', 'ls:ls' ]
| 12 | from dnslib.server import DNSServer,DNSHandler,BaseResolver,DNSLogger |
| 13 | |
| 14 | class ShellResolver(BaseResolver): |
| 15 | """ |
| 16 | Example dynamic resolver. |
| 17 | Maps DNS labels to shell commands and returns result as TXT record |
| 18 | (Note: No context is passed to the shell command) |
| 19 | |
| 20 | Shell commands are passed in a a list in <label>:<cmd> format - eg: |
| 21 | |
| 22 | [ 'uptime.abc.com.:uptime', 'ls:ls' ] |
| 23 | |
| 24 | Would respond to requests to 'uptime.abc.com.' with the output |
| 25 | of the 'uptime' command. |
| 26 | |
| 27 | For non-absolute labels the 'origin' parameter is prepended |
| 28 | |
| 29 | """ |
| 30 | def __init__(self,routes,origin,ttl): |
| 31 | self.origin = DNSLabel(origin) |
| 32 | self.ttl = parse_time(ttl) |
| 33 | self.routes = {} |
| 34 | for r in routes: |
| 35 | route,_,cmd = r.partition(":") |
| 36 | if route.endswith('.'): |
| 37 | route = DNSLabel(route) |
| 38 | else: |
| 39 | route = self.origin.add(route) |
| 40 | self.routes[route] = cmd |
| 41 | |
| 42 | def resolve(self,request,handler): |
| 43 | reply = request.reply() |
| 44 | qname = request.q.qname |
| 45 | cmd = self.routes.get(qname) |
| 46 | if cmd: |
| 47 | output = getoutput(cmd).encode() |
| 48 | reply.add_answer(RR(qname,QTYPE.TXT,ttl=self.ttl, |
| 49 | rdata=TXT(output[:254]))) |
| 50 | else: |
| 51 | reply.header.rcode = RCODE.NXDOMAIN |
| 52 | return reply |
| 53 | |
| 54 | if __name__ == '__main__': |
| 55 |