-
Notifications
You must be signed in to change notification settings - Fork 3
/
get-all-dns-nodes.py
executable file
·92 lines (72 loc) · 2.47 KB
/
get-all-dns-nodes.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
#!/usr/bin/python
import re
from datetime import datetime
import yaml # pip install PyYAML
import boto.route53 # pip install boto==2.39.0
from dateutil.parser import parse as parsedate # python-dateutil
aws_access_key_id = ""
aws_secret_access_key = ""
hosted_zone_domain_name = "example.net"
# must have trailing period
hosted_zone_domain_name = hosted_zone_domain_name + "." if hosted_zone_domain_name[-1] != "." else hosted_zone_domain_name
# IAM policy should be something like:
'''
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1461611100000",
"Effect": "Allow",
"Action": [
"route53:ListResourceRecordSets"
],
"Resource": [
"arn:aws:route53:::hostedzone/Zxxxxxxxxxxxxx"
]
},
{
"Effect": "Allow",
"Action": [
"route53:ListHostedZones"
],
"Resource": "*"
}
]
}
'''
#conn = boto.route53.connect_to_region('us-west-2')
#print(records)
def get_port(txt_record):
port_num_matches = re.findall('^P(\d+);.*', txt_record)
port_num = ""
if len(port_num_matches) > 0:
port_num = port_num_matches[0]
return port_num
def get_datetime(txt_record):
datetime_string_matches = re.findall('^.*Last update (.*)\.', txt_record)
date = ""
if len(datetime_string_matches) > 0:
date = datetime_string_matches[0]
return parsedate(date)
def get_nodes():
nodes = []
conn = boto.route53.connection.Route53Connection(aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
zone = conn.get_zone(hosted_zone_domain_name)
records = [r for r in conn.get_all_rrsets(zone.id)]
for rec in records:
if rec.type == "TXT":
txt_rec_value = rec.resource_records[0].decode("utf-8").strip('"')
hostname = rec.name.split(".")[0]
port = get_port( txt_rec_value )
date = get_datetime( txt_rec_value )
secs_since_update = (datetime.utcnow()-date).total_seconds()
node = {}
node["hostname"] = hostname
node["port"] = port
node["date"] = date
node["secs_since_update"] = secs_since_update
if port:
nodes.append(node)
return nodes
if __name__ == "__main__":
print(get_nodes())