Checking your computer external IP address using Linux

This script checks what your current external IP address and will let you know if it has changed since the last time you checked. You can easily modify this script to make your Raspberry Pi send you an email of it’s external IP address and send email when it’s email address has changed so this is pretty useful script for many kind of headless Raspberry Pi uses.


#!/bin/sh

ip1=""
ip2=""
ip="/scripts/ip.txt"

[ ! -f $ip ] && touch $ip

read ip1 < $ip
ip2=$(wget -U 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.6) Gecko/20070802 SeaMonkey/1.1.4' -qO- ifconfig.me/ip)

if [ "$ip1" = "$ip2" ]
then
 echo "IP is still the same $ip2"
  exit
else
 echo "$ip2" > $ip
    echo "IP has changed and it's now $ip2"
  exit
fi

    Leave a Reply

    Your email address will not be published. Required fields are marked *