Skip to content

Instantly share code, notes, and snippets.

@danielflira
Created February 1, 2018 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielflira/85217a4b35195bcc21f268bcaf220570 to your computer and use it in GitHub Desktop.
Save danielflira/85217a4b35195bcc21f268bcaf220570 to your computer and use it in GitHub Desktop.
'''
duckdns - update a https://duckdns.org domain
usage:
duckdns host=hostname token=duckdns-token ip=[ip|local|net]
parameters:
host - name of host created at https://duckdns.org
token - token of your account at https://duckdns.org
ip - ip (valid ip) 'local' (lan address) 'net' (internet address)
examples:
dickdns host=foo token=bar ip=local
dickdns host=foo token=bar ip=net
dickdns host=foo token=bar ip=127.0.0.1
'''
import sys
import socket
try:
from urllib import urlopen
except ImportError:
from urllib.request import urlopen
def print_help():
'''
show help message and finish
'''
print(__doc__)
sys.exit(1)
def parse_arguments():
'''
parse arguments formated as key=value and --key
'''
arguments = {}
for param in sys.argv[1:]:
if '=' in param:
key, value = param.split('=')
arguments[key] = value
elif '--' in param:
arguments[param.replace('--', '')] = True
return arguments
def update_duckdns(domains, token, ipaddr):
'''
send request to https://duckdns.org
'''
url = "https://www.duckdns.org/update?domains={}&token={}&ip={}&verbose=true"
response = urlopen(url.format(domains, token, ipaddr))
return response.status, response.read()
def cmdline():
'''
parse command line parameters
'''
arguments = parse_arguments()
if 'host' not in arguments:
print('host parameter is missing')
print_help()
if 'token' not in arguments:
print('token parameter is missing')
print_help()
if 'ip' not in arguments:
print('ip parameter is missing')
print_help()
if 'help' in arguments:
print_help()
if arguments['ip'] == 'local':
arguments['ip'] = socket.gethostbyname(socket.gethostname())
elif arguments['ip'] == 'net':
arguments['ip'] = ''
print(update_duckdns(arguments['host'], arguments['token'], arguments['ip']))
if __name__ == '__main__':
cmdline()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment