#!/usr/local/bin/python3 # -*- coding: UTF-8 -*- import pycurl from io import BytesIO import json import pprint import urllib.parse apikey = 'sxxxxxxxxxxxxx' buffer = BytesIO() buffer1 = BytesIO() curl = pycurl.Curl() # Requete chez Gandi pour avoir l'ip du DNS curl.setopt(pycurl.HTTPHEADER, [ "X-Api-Key: " + apikey ]); curl.setopt(pycurl.WRITEDATA, buffer) curl.setopt(pycurl.URL, "https://dns.api.gandi.net/api/v5/domains/funambule.org/records/@/A") curl.perform() data = buffer.getvalue().decode('utf-8') datajs = json.loads(data) old_ip = datajs['rrset_values'][0] # Requete chez ifconfig pour avoir l'ip, la vraie curl.setopt(pycurl.URL, "https://ifconfig.me/all.json") curl.setopt(pycurl.HTTPHEADER, [ "X-Api-Key: " + apikey ]); curl.setopt(pycurl.WRITEDATA, buffer1) curl.perform() data1js = json.loads(buffer1.getvalue().decode('utf-8')) new_ip = data1js['ip_addr'] # Les deux sont-elles les memes ? if old_ip != new_ip: # NON - Je supprime l'ancienne adresse buffer2 = BytesIO() curl.setopt(pycurl.HTTPHEADER, [ "Content-Type: application/json", "X-Api-Key: " + apikey ]) curl.setopt(pycurl.CUSTOMREQUEST,'DELETE') curl.setopt(pycurl.WRITEDATA, buffer2) curl.setopt(pycurl.URL, "https://dns.api.gandi.net/api/v5/domains/funambule.org/records/@/A") curl.perform() print("DELETE -> " + buffer2.getvalue().decode('utf-8')) # Je remets new_ip post_data = {} post_data["rrset_ttl"] = 10800 post_data["rrset_values"] = [ new_ip ] postfields = json.dumps(post_data) buffer3 = BytesIO() curl.setopt(pycurl.HTTPHEADER, [ "Content-Type: application/json", "X-Api-Key: " + apikey ]) curl.setopt(pycurl.CUSTOMREQUEST,'PUT') curl.setopt(pycurl.POSTFIELDS, postfields) curl.setopt(pycurl.WRITEDATA, buffer3) curl.setopt(pycurl.URL, "https://dns.api.gandi.net/api/v5/domains/funambule.org/records/@/A") curl.perform() print("PUT -> " + buffer3.getvalue().decode('utf-8'))// # Modification de la configuration du serveur turn old_ip_ech = old_ip.replace('.','\.') cmd = "sed -e 's/^external-ip={}/external-ip={}/' /etc/turnserver.conf >{}".format(old_ip_ech,new_ip,'/etc/turnserver.conf.new') subprocess.run(cmd,shell=True, check=True) cmd = "mv /etc/turnserver.conf /etc/turnserver.conf.bkp" subprocess.run(cmd,shell=True, check=True) cmd = "mv /etc/turnserver.conf.new /etc/turnserver.conf" subprocess.run(cmd,shell=True, check=True) cmd = "systemctl restart coturn" subprocess.run(cmd,shell=True, check=True)