Due to some requirements I have I've wrote a script to get and eventually update the Wan IP Address.
my provider use not to change the public IP unless the IAD at my home is resetted or remotely upgraded, but since I'm evaluating to remove the ddns and connect just using the externa IP, I was seeking for a way to obtain it.
So I've created a Variable named WanIP_IP and a script that trigger a bash command and do the rest...
here is it
Code: Select all
VARNAME = 'WanIP_IP'
GETIP = 'curl -s https://4.ifcfg.me/'
TMPFILE = '/home/pi/domoticz/scripts/wanip.txt'
PTPPREFIX = '>>> [WanIP checker] >>> '
IDLESECS = 21600 -- 6 hours
function timeDiff(dName)
t1 = os.time()
updTime = uservariables_lastupdate[dName]
year = string.sub(updTime, 1, 4)
month = string.sub(updTime, 6, 7)
day = string.sub(updTime, 9, 10)
hour = string.sub(updTime, 12, 13)
minutes = string.sub(updTime, 15, 16)
seconds = string.sub(updTime, 18, 19)
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
tDiff = os.difftime(t1,t2)
return tDiff
end
CURRWNIP = uservariables[VARNAME]
IDLETIME = tonumber(timeDiff(VARNAME))
commandArray = {}
if IDLETIME > IDLESECS then
os.execute(GETIP..' > '..TMPFILE)
wanip = io.open(TMPFILE):read()
if not wanip then
print (PTPPREFIX..'impossibile reperire Wan IP')
else
if wanip ~= CURRWNIP then
print (PTPPREFIX..'Agiornamento WAN IP: '..wanip)
commandArray['Variable:'..VARNAME] = wanip
os.execute('rm /home/pi/domoticz/scripts/wanip.txt')
end
end
end
return commandArray
than I have a variable script that notify me the change via Telegram and email.
With many thanks to @egregius... I think in bash this can be done in this way:
Code: Select all
#!/bin/bash
LASTWANIP =`cat /tmp/lastwanip.txt`
NEWWANIP = `curl -s https://4.ifcfg.me/`
if [[ $LASTWANIP -ne $NEWWANIP ]];then
echo $NEWWANIP > /tmp/lasttemp.txt
DOMOTICZ=`curl -s "http://127.0.0.1:8080/json.htm?type=command¶m=updateuservariable&vname=WanIP_IP&vtype=string&vvalue=$NEWWANIP
fi
ciao
M