/
dnsimple_set_ip.py
executable file
·65 lines (52 loc) · 1.75 KB
/
dnsimple_set_ip.py
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
#!/usr/bin/env python3
from requests import get
import click
from dnsimple import DNSimple
def print_records(records):
for rec in records:
print(f"{rec['name']:16s} {rec['type']:8s} {rec['ttl']:6d} {rec['content']}")
def create_subdomain(dns, domain, subdomain):
print(f'Creating {subdomain}.{domain}')
dns.add_record(domain, {
'type': 'A',
'name': subdomain,
'content':get_public_ip(),
'ttl': 60
})
def update_subdomain(dns, domain, subdomain_record):
print(f"Updating IP for {subdomain_record['name']}.{domain}")
dns.update_record(domain, subdomain_record['id'], {
'content':get_public_ip()
})
def get_public_ip():
ip = get('https://api.ipify.org').text
print(f'Public IP: {ip}')
return ip
@click.command()
@click.option('-t', '--api-token', required=True,
help='API token')
@click.option('--list-records', is_flag=True,
help='Instead of setting IP lists the records')
@click.option('-s', '--subdomain', required=True,
help='Subdomain whose IP to set. Will be created if it does not exist')
@click.option('-d', '--domain', required=True,
help='The top level domain to which the subdomain belongs to')
def main(*args, **kwargs):
dnsimple_set_ip(*args, **kwargs)
def dnsimple_set_ip(api_token, list_records, domain, subdomain, **kwargs):
dns = DNSimple(api_token=api_token)
records = [x['record'] for x in dns.records(domain)]
if list_records:
print_records(records)
return
subdomain_record = None
for rec in records:
if subdomain == rec['name']:
subdomain_record = rec
break
if subdomain_record is None:
create_subdomain(dns, domain, subdomain)
else:
update_subdomain(dns, domain, subdomain_record)
if __name__ == '__main__':
main()