12 def get_argument_parser():
13 parser
= argparse
.ArgumentParser(
14 prog
='update_dns_entry.py',
15 description
='Generate fixed-address DHCP configuration based for hosts based on DNS entries'
18 '-s', '--server', default
=None, required
=True,
19 help='Server for DNS updates'
22 '-u', '--user', default
=None,
23 help='The user to use with samba-tool'
26 '-z', '--zone', required
=True,
27 help='The zone in which to update the entry'
30 '-n', '--name', required
=True,
34 '-v', '--value', required
=True,
35 help='DNS entry value'
38 '-t', '--type', default
='A',
44 def update_dns_entry(server
, zone
, name
, entry_type
, value
, user
=None, with_reverse
= True):
46 assert(ipaddress
.ip_address(value
))
48 server_ip
= str(ipaddress
.ip_address(server
))
50 server_ip
= dns
.resolver
.resolve(server
)[0].to_text()
53 # Verify existing entry
54 query
= dns
.message
.make_query('.'.join([name
, zone
]), entry_type
)
55 record
= dns
.query
.udp(query
, server_ip
);
56 if len(record
.answer
) == 0:
58 argv
= ['samba-tool', 'dns', 'add', server
, zone
, name
, entry_type
, value
]
63 assert(len(record
.answer
) == 1)
65 existing
= (record
.answer
)[0][0].to_text()
68 argv
= ['samba-tool', 'dns', 'update', server
, zone
, name
, entry_type
, existing
, value
]
74 if with_reverse
and entry_type
== 'A':
75 rname
, rzone
= ipaddress
.ip_address(value
).reverse_pointer
.split('.', 1)
76 rvalue
= '.'.join([name
, zone
]) + '.'
78 query
= dns
.message
.make_query(ipaddress
.ip_address(value
).reverse_pointer
, rtype
)
79 record
= dns
.query
.udp(query
, server_ip
)
80 if len(record
.answer
) == 0:
81 argv
= ['samba-tool', 'dns', 'add', server
, rzone
, rname
, rtype
, rvalue
]
86 assert(len(record
.answer
) == 1)
87 existing
= (record
.answer
)[0][0].to_text()
89 argv
= ['samba-tool', 'dns', 'update', server
, rzone
, rname
, rtype
, existing
, rvalue
]
95 for command
in commands
:
96 subprocess
.run(command
, check
=True)
99 if __name__
== '__main__':
100 parser
= get_argument_parser()
101 args
= parser
.parse_args()
102 update_dns_entry(args
.server
, args
.zone
, args
.name
, args
.type, args
.value
, user
=args
.user
)
This page took 0.030937 seconds and 4 git commands to generate.