Strumenti Utente

Strumenti Sito


programmazione:python:script_per_aggiornare_dyndns.it

Differenze

Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.

Link a questa pagina di confronto

Entrambe le parti precedenti la revisione Revisione precedente
Prossima revisione
Revisione precedente
programmazione:python:script_per_aggiornare_dyndns.it [17/12/2019 - 15:58]
Fabio Di Matteo
programmazione:python:script_per_aggiornare_dyndns.it [31/03/2020 - 17:42]
Fabio Di Matteo
Linea 1: Linea 1:
 ====== Uno script per aggiornare l'ip del servizio Dyndns.it ====== ====== Uno script per aggiornare l'ip del servizio Dyndns.it ======
-Autore: **//Fabio Di Matteo//** \\ Ultima revisione: ​ **//  ​17/12/2019  ​- ​13:55   //**  // //+Autore: **//Fabio Di Matteo//** \\ Ultima revisione: ​ **//  ​31/03/2020  ​- ​17:37   //**  // //
  
 Aggiorna l'ip pubblico ogni 5 minuti, solo se è cambiato. Aggiorna l'ip pubblico ogni 5 minuti, solo se è cambiato.
Linea 41: Linea 41:
  
 </​code>​ </​code>​
 +
 +====== Versione con file di configurazione ======
 +<code python>
 +
 +#​!/​usr/​bin/​env python3
 +import time,​os,​configparser,​sys
 +import urllib3
 + 
 + 
 +currentIP=''​
 +
 +urlUpdate=''​
 +user=''​
 +password=''​
 +hostname=''​
 + 
 +
 +def getPrefs():
 + global urlUpdate ​
 + global user
 + global password
 + global hostname
 +  
 + if  sys.platform == '​linux':​
 + try:
 + iniFile=os.path.join('/​etc/​pobDNSupdate/​prefs.conf'​)
 + config = configparser.ConfigParser()
 + config.read(iniFile)
 + urlUpdate=config['​DEFAULT'​]['​urlUpdate'​]
 + user=config['​DEFAULT'​]['​user'​]
 + password=config['​DEFAULT'​]['​password'​]
 + hostname=config['​DEFAULT'​]['​hostname'​]
 + return True
 + except Exception as e:
 + print (e.message)
 + return False
 +
 + if sys.platform=="​win32":​
 + try:
 + if getattr(sys,​ '​frozen',​ False):
 + # frozen
 + iniFile= os.path.join(os.path.dirname(sys.executable),"​prefs.conf"​)
 + else:
 + # unfrozen
 + iniFile=os.path.join(os.path.dirname(os.path.realpath(__file__)),"​prefs.conf"​)
 +
 + config = configparser.ConfigParser()
 + config.read(iniFile)
 + urlUpdate=config['​DEFAULT'​]['​urlUpdate'​]
 + user=config['​DEFAULT'​]['​user'​]
 + password=config['​DEFAULT'​]['​password'​]
 + hostname=config['​DEFAULT'​]['​hostname'​]
 + return True
 + except Exception as e:
 + print (e.message)
 + return False
 +
 +
 +
 + if sys.platform=="​darwin":​
 + try:
 + iniFile=os.path.join('/​etc/​pobDNSupdate/​prefs.conf'​)
 + config = configparser.ConfigParser()
 + config.read(iniFile)
 + urlUpdate=config['​DEFAULT'​]['​urlUpdate'​]
 + user=config['​DEFAULT'​]['​user'​]
 + password=config['​DEFAULT'​]['​password'​]
 + hostname=config['​DEFAULT'​]['​hostname'​]
 + return True
 + except Exception as e:
 + print (e.message)
 + return False
 + 
 + 
 +def getPublicIpRaw():​
 + url = "​https://​api.ipify.org/?​format=raw"​
 + http = urllib3.PoolManager()
 + r = http.request('​GET',​ url)
 + return r.data
 + 
 + 
 +def update():
 + global urlUpdate ​
 + global user
 + global password
 + global hostname
 +
 + if (getPrefs() == True):
 +
 + global currentIP
 + fmlIP=getPublicIpRaw()
 + if (currentIP!=fmlIP):​
 + http = urllib3.PoolManager()
 + headers = urllib3.util.make_headers(basic_auth=user+':'​+password)
 + r = http.request('​GET',​ urlUpdate + hostname ,​headers=headers)
 + currentIP=fmlIP
 + print(str(r.data))
 + else:
 + print("​Nothing to update."​)
 + 
 + 
 +while(True):​
 + update()
 + time.sleep(60*5)
 +
 +</​code>​
 +
 +**prefs.conf**
 +<​file>​
 +[DEFAULT]
 +urlUpdate=http://​update.dyndns.it/​nic/​update?​hostname=
 +user=utente
 +password=password
 +hostname=miohost.it
 +
 +</​file>​

programmazione/python/script_per_aggiornare_dyndns.it.txt · Ultima modifica: 31/03/2020 - 17:42 da Fabio Di Matteo