Barra laterale

Command disabled: revisions
programmazione:python:script_per_aggiornare_dyndns.it

Uno script per aggiornare l'ip del servizio Dyndns.it

Autore: Fabio Di Matteo
Ultima revisione: 31/03/2020 - 17:37

Aggiorna l'ip pubblico ogni 5 minuti, solo se è cambiato. Modificare : “nomeutente:password”, miohost.it (eventualmente anche https://api.ipify.org/?format=raw ) secondo le nostre esigenze.

#!/usr/bin/env python3
import time
import urllib3
 
 
currentIP=''
 
 
 
def getPublicIpRaw():
	url = "https://api.ipify.org/?format=raw"
	http = urllib3.PoolManager()
	r = http.request('GET', url)
	return r.data
 
 
def update():
	global currentIP
	fmlIP=getPublicIpRaw()
	if (currentIP!=fmlIP):
		http = urllib3.PoolManager()
		headers = urllib3.util.make_headers(basic_auth='nomeutente:password')
		r = http.request('GET', 'http://update.dyndns.it/nic/update?hostname=miohost.it',headers=headers)
		currentIP=fmlIP
		print(str(r.data))
	else:
		print("Stesso ip pubblico. Non aggiorno.")
 
 
while(True):
	update()
	time.sleep(60*5)

Versione con file di configurazione

#!/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)

prefs.conf

[DEFAULT]
urlUpdate=http://update.dyndns.it/nic/update?hostname=
user=utente
password=password
hostname=miohost.it

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