Skip to content

Instantly share code, notes, and snippets.

@vasilake-v
Created July 4, 2018 07:27
Show Gist options
  • Save vasilake-v/bf6cfbc72eb2ee02c2010c181a4bdcb7 to your computer and use it in GitHub Desktop.
Save vasilake-v/bf6cfbc72eb2ee02c2010c181a4bdcb7 to your computer and use it in GitHub Desktop.
Bash script that checks if current ip has changed. If it did change, send new ip on email address
#!/bin/sh
cd ~/scripts
LAST_IP_FILENAME="./last_known_ip"
MY_EMAIL_ADDRESS="[email protected]"
LAST_IP=""
#CURRENT_IP=`/sbin/ifconfig | awk '/enp3s0/{getline; split($2, result, ":"); print result[2]; }'`
CURRENT_IP=`/sbin/ifconfig | awk '/enp3s0/{getline; print $2; }'`
if [ -f "$LAST_IP_FILENAME" ]; then
LAST_IP=`cat $LAST_IP_FILENAME`;
else
touch "$LAST_IP_FILENAME"
fi
if [ "$LAST_IP" != "$CURRENT_IP" ]; then
echo "Ip changed, new ip: $CURRENT_IP"
echo "$CURRENT_IP" > "$LAST_IP_FILENAME"
echo "IP changed, new ip is [$CURRENT_IP]" | mail -s "IP changed" $MY_EMAIL_ADDRESS
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment