Skip to content

Instantly share code, notes, and snippets.

@mrbidon
Created November 14, 2023 20:42
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 mrbidon/2d99b78f0d896258d5fdb15fe39b0194 to your computer and use it in GitHub Desktop.
Save mrbidon/2d99b78f0d896258d5fdb15fe39b0194 to your computer and use it in GitHub Desktop.
Script renouvellement de dyndns
#!/usr/bin/python3
# this script aim renew an ovh dyndns
# it musts be executed in cron,
# first it check if DNS is update
# 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
import time
import socket
import requests.packages.urllib3.util.connection as urllib3_cn
#Force IPv4
def allowed_gai_family():
return socket.AF_INET
def main(hostname, login, password):
i = 0;
while i < 3:
try:
r = requests.get('http://ifconfig.me/ip')
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:
print(f"IP Change new : {ip}, old : " + dns_reverse_ips[0].address)
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.decode("utf-8").strip() != f"good {ip}":
print("error : " + r.content.decode("utf-8"))
return
else:
print("IP well registered")
else:
return
except Exception as e:
if x >= 3:
print(e)
print("catch exception " + i + " time")
else:
time.sleep(30)
i += 1
if __name__ == '__main__':
urllib3_cn.allowed_gai_family = allowed_gai_family
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