Skip to content

Instantly share code, notes, and snippets.

@bhaskarkc
Last active April 18, 2024 01:00
Show Gist options
  • Save bhaskarkc/c2f8c2cabd2a3991772f108f68dffcde to your computer and use it in GitHub Desktop.
Save bhaskarkc/c2f8c2cabd2a3991772f108f68dffcde to your computer and use it in GitHub Desktop.
A python (python3) script to update no-ip (https://www.noip.com) dynamic dns ip.
#!/usr/bin/env python
import requests, socket
username = ""
password = ""
hostname = "" # your domain name hosted in no-ip.com
# Gets the current public IP of the host machine.
myip = requests.get('http://api.ipify.org').text
# Gets the existing dns ip pointing to the hostname.
old_ip = socket.gethostbyname(hostname)
# Noip API - dynamic DNS update.
# https://www.noip.com/integrate/request.
def update_dns(config):
r = requests.get("http://{}:{}@dynupdate.no-ip.com/nic/update?hostname={}&myip={}".format(*config))
if r.status_code != requests.codes.ok:
print(r.content)
pass
# Update only when ip is different.
if myip != old_ip:
update_dns( (username, password, hostname, myip) )
pass
@bhaskarkc
Copy link
Author

setting up above script in crontab.

crontab -e

at the end of the crontab file.

0 0 * * FRI source $WORKON_HOME/no-ip-update/bin/activate && ~/noip-updater/app.py

-> Runs updater script every friday at 00:00.
-> Im activating the virtualenv and then running the script. :)
-> $WORKON_HOME is a variable provided by virtualenvwrapper. I've setup it to ~/.virtualenvs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment