-
Notifications
You must be signed in to change notification settings - Fork 2
/
rr.py
111 lines (81 loc) · 2.81 KB
/
rr.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""
rr.py
UDNS's DDNS cli
License: MIT
Copyright: 2017, Yehuda Deutsch <[email protected]>
"""
from ConfigParser import ConfigParser
from argparse import ArgumentParser
import six
import txredisapi as redis
from twisted.internet import defer, reactor
parser = ArgumentParser('rr.py', 'Manage DDNS records')
parser.add_argument('--config', '-c', help='A config file for DDNS settings, default: ddns.ini', default='ddns.ini')
parser.add_argument('--zone', '-z', help='The zone (domain name) to add this record', required=True)
parser.add_argument('--host', '-o', help='The hostname to set the RR')
parser.add_argument('--type', '-t', help='The RR type, currently supports only A', default='A', choices=['A'])
parser.add_argument('--data', '-d', help='The data for the RR')
parser.add_argument('--list', '-l', help='Just list all records for zone', action='store_true')
args = parser.parse_args()
config = ConfigParser()
config.read(args.config)
@defer.inlineCallbacks
def rr_set():
domain = args.zone.lower().strip()
host = args.host.lower().strip()
rr_type = args.type.upper().strip()
data = args.data.strip()
conn = yield redis.Connection(
host=config.get('redis', 'host'),
port=int(config.get('redis', 'port')),
dbid=int(config.get('redis', 'db'))
)
hset = yield conn.hset(
'DDNS:ZONE:{}'.format(domain),
'{}:{}'.format(host, rr_type),
data
)
if hset:
print 'Set successful'
else:
print 'Update successful'
yield conn.disconnect()
@defer.inlineCallbacks
def rr_get():
domain = args.zone.lower().strip()
host = args.host.lower().strip()
rr_type = args.type.upper().strip()
conn = yield redis.Connection(
host=config.get('redis', 'host'),
port=int(config.get('redis', 'port')),
dbid=int(config.get('redis', 'db'))
)
data = yield conn.hget(
'DDNS:ZONE:{}'.format(domain),
'{}:{}'.format(host, rr_type)
)
print '{}.{}.\t{}\t{}'.format(host, domain, rr_type, data)
yield conn.disconnect()
@defer.inlineCallbacks
def rr_list():
domain = args.zone.lower().strip()
conn = yield redis.Connection(
host=config.get('redis', 'host'),
port=int(config.get('redis', 'port')),
dbid=int(config.get('redis', 'db'))
)
zone_rrs = yield conn.hgetall('DDNS:ZONE:{}'.format(domain))
zone_hosts = ['{}\t{}\t{}'.format(*(key.split(':') + [value])) for key, value in six.iteritems(zone_rrs)]
zone_hosts.sort()
print '\n'.join(zone_hosts)
yield conn.disconnect()
def main():
cmd_method = rr_set
if args.list:
cmd_method = rr_list
elif not args.data:
cmd_method = rr_get
cmd_method().addCallback(lambda ignore: reactor.stop())
if __name__ == '__main__':
reactor.callWhenRunning(main)
reactor.run()