-
Notifications
You must be signed in to change notification settings - Fork 0
/
dyndns.py
executable file
·71 lines (54 loc) · 1.7 KB
/
dyndns.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
#!/usr/bin/python
from boto.route53.connection import Route53Connection
from boto.route53.record import ResourceRecordSets
import getopt
import sys
import urllib2
def getZone(zoneName, connection):
for zone in connection.get_zones():
if zone.name == zoneName:
return zone
else:
continue
return
def updateARecordOnZone(dns_name, zone, ip):
record = zone.find_records(dns_name, "A")
if record:
if record.resource_records[0] != ip:
print("Zone IP is: " + record.resource_records[0])
print("Updating zone record")
zone.update_record(record, ip)
else:
print("Zone IP is: " + record.resource_records[0])
print("Not updating, already up to date")
else:
print("Creating zone record")
zone.add_record("A", dns_name, ip)
def main(argv):
zone = ''
dns_name = ''
opts, args = getopt.getopt(argv, "z:d:",["zone=","dns_name="])
if len(argv) != 4:
print("You must provide arguments in the form...")
print('dyndns.py -z zone.com. -d host.zone.com.')
sys.exit(2)
for opt, arg in opts:
if opt in ("-z", "--zone"):
zone = arg
elif opt in ("-d", "--dns_name"):
dns_name = arg
if zone == '' or dns_name == '':
print("You must provide arguments in the form...")
print('dyndns.py -z zone.com. -d host.zone.com.')
sys.exit(2)
getIpStream = urllib2.urlopen('http://ipinfo.io/ip')
ip = getIpStream.read().rstrip();
print("Local IP is: " + ip)
r53 = Route53Connection()
zoneTarget = getZone(zone, r53)
if zoneTarget:
updateARecordOnZone(dns_name, zoneTarget, ip)
else:
print('Zone not found.')
if __name__ == "__main__":
main(sys.argv[1:])