Last active
January 12, 2020 00:43
-
-
Save elijahc/8abb89f55a49a1abc9b7dd478db89c06 to your computer and use it in GitHub Desktop.
Python script for updating google domains dyndns entries
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 requests | |
import psutil | |
import socket | |
import click | |
import pprint | |
import numpy as np | |
INTERFACES = psutil.net_if_addrs() | |
@click.command() | |
@click.option('--iface', prompt='interface', default='eth1') | |
@click.option('--user', prompt='username', default="") | |
@click.option('--passwd', prompt='pass', default='') | |
@click.option('--domain', prompt='domain', default='symlink.pw') | |
@click.option('--hostname', prompt='hostname', default=socket.gethostname()) | |
def update(iface, user,passwd,domain,hostname): | |
# iface='enp9s0' | |
interfaces = psutil.net_if_addrs() | |
inet_ip = interfaces[iface][0].address | |
auth='{}:{}'.format(user,passwd) | |
host = 'domains.google.com' | |
endpoint = '/nic/update' | |
url='https://{}@{}{}'.format(auth,host,endpoint) | |
print('POST: ',url) | |
print() | |
payload = { | |
'hostname':'{}.{}'.format(hostname,domain), | |
'myip':inet_ip, | |
} | |
print(payload) | |
print() | |
r = requests.post(url, params=payload) | |
print('response:') | |
print() | |
print(r.text) | |
if __name__ == '__main__': | |
pp = pprint.PrettyPrinter(indent=4) | |
exclude = ['docker','lo'] | |
interfaces = psutil.net_if_addrs() | |
ex = [[k.startswith(e) for k in interfaces.keys()] for e in exclude] | |
# keys = np.array(interfaces.keys())[np.logical_or(*ex)] | |
keys = interfaces.keys() | |
interfaces = {k:interfaces[k][0].address for k in keys} | |
print('interfaces:') | |
pp.pprint(interfaces) | |
update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment