Skip to content

Instantly share code, notes, and snippets.

@ax003d
Created September 9, 2015 08:53
Show Gist options
  • Save ax003d/a2ebac55c1a72189a73f to your computer and use it in GitHub Desktop.
Save ax003d/a2ebac55c1a72189a73f to your computer and use it in GitHub Desktop.
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