Skip to content

Instantly share code, notes, and snippets.

@fabioadrianosoares
Last active December 13, 2015 23:29
Show Gist options
  • Save fabioadrianosoares/4992010 to your computer and use it in GitHub Desktop.
Save fabioadrianosoares/4992010 to your computer and use it in GitHub Desktop.
Atualizar ip dinamico no http://whatismyip.akamai.com/
#! /bin/bash
BASE_DIR=/root/dns
DADOS=$BASE_DIR/tmp/retorno.html
ATUAL=$BASE_DIR/atual.txt
LOG=$BASE_DIR/log/atualizacao-`date +%F`.log
SITES=$BASE_DIR/sites.txt
AFRAID='http://freedns.afraid.org/dynamic/update.php?<%---- hash here ------%>'
function buscar()
{
SITE=$1;
echo `date +%T` - Buscando IP atual em $SITE | tee -a $LOG
wget -q -O $DADOS $SITE
if [ "$?" -ne "0" ]; then
echo `date +%T` - Nao conseguiu chamar site | tee -a $LOG;
return 1;
fi
# fazer parse do valor retornado
IP_DINAMICO=`perl -n -e 'print "$1\n" if (/(\d+\.\d+\.\d+\.\d+)/);' < $DADOS | head -1`
if [ "$IP_DINAMICO" == "" ] ; then
echo `date +%T` - Nao conseguiu encontar endereco IP | tee -a $LOG;
return 1;
fi
# verificar se mudou
if [ "$IP_DINAMICO" == "$IP_ATUAL" ] ; then
echo `date +%T` - IP nao mudou | tee -a $LOG
else
echo `date +%T` - Novo IP dinamico $IP_DINAMICO | tee -a $LOG
# chamar atualizacao
wget -q -O $DADOS $AFRAID
if [ "$?" -ne "0" ]; then
echo `date +%T` - Nao conseguiu chamar AFRAID | tee -a $LOG;
return 1;
fi
echo $IP_DINAMICO > $ATUAL
wget -q -O /dev/null --header='Authorization: Bearer xxxxxxxx' \
--header='Content-Type: application/json' \
https://api.pushbullet.com/v2/pushes \
--post-data='{"device_iden": "yyyyyyy", "type": "note", "title": "Cameras condominio", "body": "O endereco ip das cameras do condominio mudou: '$IP_DINAMICO'"}'
fi
return 0;
}
echo `date +%T` - Iniciando processamento | tee -a $LOG
if [ -f $ATUAL ] ; then
IP_ATUAL=`head -1 < $ATUAL`
else
IP_ATUAL=0.0.0.0
fi
echo `date +%T` - IP atual $IP_ATUAL | tee -a $LOG
# tentar para cada linha do arquivo
while read linha ; do
buscar $linha;
if [ "$?" -eq "0" ]; then
echo `date +%T` - Concluindo processamento | tee -a $LOG
exit 0;
fi
done < $SITES;
echo `date +%T` - Nao foi possivel determinar o IP dinamico | tee -a $LOG
http://whatismyip.akamai.com
http://automation.whatismyip.com/n09230945.asp
http://www.myip.com.br/index2.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment