Created
February 14, 2018 02:29
-
-
Save corny/63df2ce8b791a6e236b7e8f87c347ba7 to your computer and use it in GitHub Desktop.
dynv6 python script
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
#!/usr/bin/env python2 | |
import subprocess | |
import re | |
import urllib2 | |
import os | |
hostname = "xxx.dynv6.net" | |
token = "your-secret-token" | |
output = subprocess.check_output(["ip", "addr", "list", "dev", "pppoe0", "scope", "global"]) | |
inet4 = re.search('inet ([\d\.]+)', output).group(1) | |
inet6 = re.search('inet6 ([\da-f:]+)', output).group(1) | |
output = subprocess.check_output(["ip", "addr", "list", "dev", "switch0", "scope", "global"]) | |
prefix = re.search('inet6 ([\da-f:]+/\d+)', output).group(1) | |
with os.fdopen(os.open('/tmp/dynv6.addr', os.O_RDWR | os.O_CREAT), "r+") as fd: | |
# read current address | |
current = fd.read() | |
if current != inet4: | |
url = "http://dynv6.com/api/update?token=%s&hostname=%s&ipv4=%s&ipv6=%s" % (token, hostname, inet4, prefix) | |
print urllib2.urlopen(url).read() | |
# save current address | |
fd.seek(0) | |
fd.truncate(0) | |
fd.write(inet4) | |
fd.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment