Skip to content

Instantly share code, notes, and snippets.

@TimoDJatomika
Last active February 13, 2018 14:51
Show Gist options
  • Save TimoDJatomika/80e3f436fb80d3295fea49aac8cfb1a0 to your computer and use it in GitHub Desktop.
Save TimoDJatomika/80e3f436fb80d3295fea49aac8cfb1a0 to your computer and use it in GitHub Desktop.
Everytime your dynamic IP changes you get a slack notification. How cool is that!
#!/bin/bash
# author: Timo Stankowitz <timo.stankowitz@gmail.com>
# create date: 2017-11-17
# last change: 2018-02-13
# version 3
# everytime your dynamic ip changes you get a notification to your slack channel
# with the current dynamic ip of your router
# create a cronjob and let the script execute every 5 minutes
# replace with your data
slackURL='https://hooks.slack.com/services/XXX/YYY/ZZZ1'
DIGITALOCEAN_TOKEN='XXXYYYZZZ'
tempfile="/tmp/current-ip.txt"
# get your current public IP via my PHP script
currentIP=$(curl -s http://ip.brainoftimo.com/cli.php)
function writeIP() {
# write current IP into tempfile
echo "$currentIP" > $tempfile
}
function sendToSlack() {
curl -X POST --data-urlencode 'payload={
"channel": "dynamic_ip",
"username": "dynamic IP Bot",
"icon_emoji": ":nerd_face:",
"text": "'"$1"'"
}' $slackURL
}
# check if tempfile exists
test -f $tempfile
if [ $? != "0" ]; then
# create file and write ip
touch $tempfile
writeIP
sendToSlack "Du führst das Script das erste mal aus.\nDeine aktuelle IP lautet: *$currentIP*"
fi
# read "old" IP from file
oldIP=$(head -n1 $tempfile)
# compare the addresses and send a message if a change took place
if [ "$oldIP" != "$currentIP" ]; then
# your ip has change
sendToSlack "Deine dynamische IP Adresse hat sich geändert.\nZuvor hattest du die Adress ~$oldIP~. \nNun hast du die IP Adresse *$currentIP*"
# write the new IP into the temp file.
writeIP
# replace with your domain
curl -s -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" -X PUT -H "Content-Type: application/json" -d '{"data":"'"$currentIP"'"}' https://api.digitalocean.com/v2/domains/example.com/records/XXYYZZ >> /tmp/curl_DO_Status
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment