forked from johanreinalda/godaddy_dynamic_dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_ip.py
executable file
·66 lines (55 loc) · 2.25 KB
/
update_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/python
import sys, argparse, logging, pif, smtplib
from pygodaddy import GoDaddyClient
# Import the email modules we'll need
from email.mime.text import MIMEText
#this contain the config file
import godaddy
#email function
def email_update(body):
global smtplib
msg = MIMEText(body)
msg['From'] = godaddy.sender
msg['To'] = godaddy.to
msg['Subject'] = 'IP address updater'
s = smtplib.SMTP(smtpserver)
s.sendmail(godaddy.sender, godaddy.to, msg.as_string())
s.quit()
#command line arguments parsing
parser = argparse.ArgumentParser('A Python script to do updates to a GoDaddy DNS host A record')
parser.add_argument('-v', '--verbose', action='store_true', help="send emails on 'no ip update required'")
args = parser.parse_args()
#start log file
logging.basicConfig(filename=godaddy.logfile, format='%(asctime)s %(message)s', level=logging.INFO)
#what is my public ip?
public_ip = pif.get_public_ip()
logging.info("My ip: {0}".format(public_ip))
# login to GoDaddy DNS management
# docs at https://pygodaddy.readthedocs.org/en/latest/
client = GoDaddyClient()
if client.login(godaddy.gduser, godaddy.gdpass):
# find out current dns record value. This can also be done with a quick nslookupA
# with something like socket.gethostbyname()
# however, this can include caching somewhere in the dns layers
# We could also use dnspython libraray, but that adds a lot of complexity
# use the GoDaddy object to find our current IP registered
domaininfo = client.find_dns_records(godaddy.domain)
for record in domaininfo:
if record.hostname == godaddy.host:
if record.value != public_ip:
logging.info("Update required: old {0}, new {1}".format(record.value, public_ip))
updateinfo = "old " + record.value + ", new " + public_ip
# This will fail if you try to set the same IP as already registered!
if client.update_dns_record(godaddy.host+"."+godaddy.domain, public_ip):
logging.info('Update OK')
email_update("Update OK!\n"+updateinfo)
else:
logging.info('Update DNS FAILED!')
email_update("Update failed!\n"+updateinfo)
else:
logging.info('No update required.')
if args.verbose:
email_update('No update required.')
else:
logging.error('CANNOT login to GoDaddy')
email_update('ERROR: Cannot login to GoDaddy!')