Created
September 9, 2015 08:53
-
-
Save ax003d/a2ebac55c1a72189a73f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import socket | |
import requests | |
SERVER = '' | |
ZONE = '' | |
def public_ip(): | |
try: | |
resp = requests.get('http://httpbin.org/ip') | |
return resp.json()['origin'] | |
except Exception, e: | |
pass | |
return "cannot get public ip." | |
def update(key, domain, ip): | |
p = os.popen('nsupdate -k %s' % key, 'w') | |
p.write('server %s\n' % SERVER) | |
p.write('zone %s\n' % ZONE) | |
p.write('update del %s A\n' % domain) | |
p.write('update add %s 600 A %s\n' % (domain, ip)) | |
p.write('send\n') | |
p.close() | |
if __name__=='__main__': | |
cur_ip = socket.gethostbyname('') | |
pub_ip = public_ip() | |
if cur_ip != pub_ip: | |
update('', '', pub_ip) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment