Skip to content

Instantly share code, notes, and snippets.

@Deuteu
Forked from allegrem/gist:8f1736ed2966d4596779
Last active August 12, 2017 21:24
Show Gist options
  • Save Deuteu/5f24151fe48f6ad74e14 to your computer and use it in GitHub Desktop.
Save Deuteu/5f24151fe48f6ad74e14 to your computer and use it in GitHub Desktop.
#!/bin/bash
#This one mails you the ip if it changes
#Deuteu inspired and guided by Allegrem
#Env variables
path="/home/user/ip_script"
old="$path/ip_old.dat"
mail="$path/ip_mail.txt"
log="$path/ip_log.log"
#IP
T1=`curl -s icanhazip.com`
#curl ipinfo.io/ip #IPv4
#curl ifconfig.me #IPv4
#curl -s icanhazip.com #IPv6
read T2 < $old
#Mail variables
from="sender@gmail.com"
to="example@domain.com"
head="From: $from
To: $to
Subject: IP Adress
"
body="Computer IP is: $T1"
echo "$head" > $mail
date >> $mail
echo "$body" >> $mail
if [ "$T1" = "" ]; then
echo "[`date`] - [ip_update] - IP couldn't be retrieve - doing nothing" >> $log
elif [ "$T1" = "$T2" ]; then
echo "[`date`] - [ip_update] - IP is the same: $T1 - doing nothing " >> $log
else
echo "[`date`] - [ip_update] - IP has changed: $T1 - send mail" >> $log
/usr/sbin/sendmail example@domain.com < $mail
echo "$T1" > $old
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment