Last active
August 28, 2021 17:39
-
-
Save Palakis/7b4f4f3b6708c21519e9 to your computer and use it in GitHub Desktop.
Behind-NAT-style Dynamic DNS script, designed to run on Ubiquiti EdgeRouter hardware
This file contains 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/python | |
import socket | |
import fcntl | |
import struct | |
import httplib | |
import json | |
import sys | |
from base64 import b64encode | |
from urllib import urlencode | |
def get_ip_address(ifname): | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))[20:24]) | |
config = json.load(open('/config/scripts/ddns.settings.json')) | |
for key in config: | |
key = key.encode('ascii') | |
try: | |
params = config[key] | |
try: | |
lan_addr = get_ip_address(key) | |
except: | |
print 'No address defined on ' + key | |
continue | |
req = httplib.HTTPConnection('icanhazip.com', source_address=(lan_addr, 0)) | |
req.request('GET', '/') | |
resp = req.getresponse() | |
wan_addr = resp.read() | |
wan_addr = wan_addr.rstrip('\n') | |
print key + ': ' + lan_addr + ' (IF address); ' + wan_addr + ' (Public address)' | |
sys.stdout.write('Sending IP update for ' + key + ' to ' + params['server'] + '... ') | |
req = httplib.HTTPConnection(params['server'], source_address=(lan_addr, 0)) | |
authData = b64encode(params['username'] + ':' + params['password']) | |
headers = {"Authorization": "Basic " + authData} | |
params = urlencode({'hostname': params['hostname'], 'myip': wan_addr, 'system': 'dyndns'}) | |
req.request('GET', '/nic/update?' + params, "", headers) | |
print req.getresponse().read(); | |
except: | |
raise |
This file contains 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
{ | |
"eth0": { | |
"hostname": "a.domain.com", | |
"username": "johndoe", | |
"password": "1234", | |
"server": "members.dyndns.com" | |
}, | |
"eth1": { | |
"hostname": "b.domain.com", | |
"username": "johndoe", | |
"password": "1234", | |
"server": "members.dyndns.com" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment