Last active
July 16, 2020 00:03
-
-
Save gtrabanco/94e7cc2b1fdbae0d93cf5586f0de6e0c to your computer and use it in GitHub Desktop.
Script to update in ovh the ip address of dynamic DNS and send the IP using Telegram Bot when update the ip.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# CONFIG | |
# | |
#FQDN | |
HOST="example.com" | |
# Telegram | |
BOT_API_KEY="" | |
BOT_CHAT_ID="" | |
BOT_VIM_NAME="" | |
#Login and password | |
LOGIN="" | |
PASSWORD="" | |
PATH_LOG=/var/log/dynhost | |
CURRENT_DATE=`date` | |
# | |
# GET IPs | |
# | |
HOST_IP=$(dig +short a $HOST) | |
CURRENT_IP=$(dig +short myip.opendns.com @resolver1.opendns.com) | |
# | |
# DO THE WORK | |
# | |
if [ -z $CURRENT_IP ] || [ -z $HOST_IP ] | |
then | |
echo "No IP retrieved" >> $PATH_LOG | |
else | |
if [ "$HOST_IP" != "$CURRENT_IP" ] | |
then | |
echo "$CURRENT_DATE"": Current IP:" "$CURRENT_IP" "and" "host IP:" "$HOST_IP" " IP has changed!" >> $PATH_LOG | |
RES=`curl --user "$LOGIN:$PASSWORD" "https://www.ovh.com/nic/update?system=dyndns&hostname=$HOST&myip=$CURRENT_IP"` | |
echo "Result request dynHost:" >> $PATH_LOG | |
echo "$RES" >> $PATH_LOG | |
message="$CURRENT_DATE Current IP: $CURRENT_IP and host IP: $HOST_IP -- ${HOST} has changed!" | |
message=$(echo $message | sed -e 's/ /%20/g') | |
curl "https://api.telegram.org/bot${BOT_API_KEY}/sendMessage" --data-urlencode "chat_id=${BOT_CHAT_ID}" --data-urlencode "text=$message" | |
else | |
echo "$CURRENT_DATE"": Current IP:" "$CURRENT_IP" "and" "Host IP:" "$HOST_IP" " IP has not changed" >> $PATH_LOG | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment