Skip to content

Instantly share code, notes, and snippets.

@jbarotin
Last active January 9, 2021 10:39
Show Gist options
  • Save jbarotin/da38915aafe4f39d0d8e8333d8ba66e2 to your computer and use it in GitHub Desktop.
Save jbarotin/da38915aafe4f39d0d8e8333d8ba66e2 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# this script aim to renew an ovh dyndns record
# it musts be executed in cron.
# first it check if DNS is up-to-date
# and then if necessary update the IP
#
# it needs two python packages : requests and dnspython
import sys
import dns.resolver
import requests
from requests.auth import HTTPBasicAuth
def main(hostname, login, password):
r = requests.get('https://api.ipify.org')
if r.status_code != 200:
print("Error while querying public ip")
return
ip = r.content.decode("utf-8")
dns_reverse_ips = dns.resolver.resolve(hostname, 'A')
if dns_reverse_ips[0].address != ip:
r = requests.get(f"http://www.ovh.com/nic/update?system=dyndns&hostname={hostname}&myip={ip}",
auth=HTTPBasicAuth(login, password))
if r.status_code != 200:
print("Error while updating hostname")
return
if r.content != f"good {ip}":
print("error : " + r.content.decode("utf-8"))
return
if __name__ == '__main__':
if len(sys.argv) == 4:
main(sys.argv[1], sys.argv[2], sys.argv[3])
else:
print("renew_ovh_dyndns HOSTNAME LOGIN PASSWORD")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment