Get external IP via bash

To get your external IP via a small bash script

#!/bin/bash
# Define parameters
LOGFILE="/tmp/extip.log"
DATE=$(date "+%Y%m%d-%H%M")
GETIP=$(wget -q -O - checkip.dyndns.org | sed -e 's/[^[:digit:]|.]//g')
test -f $LOGFILE  || echo "no LOGFILE"
touch $LOGFILE
GETLOGIP=$(tail -n 1 $LOGFILE | awk -F"-" '{ print $3 }')
# check IP vs last logged IP
if [ "$GETIP" != "$GETLOGIP" ];
 then
#	echo "no match"
	echo $DATE-$GETIP >> $LOGFILE
# else
#	echo "match"
fi

The Output is send into a logfile.

20110724-1522-213.165.40.195

 

 

3 comments for “Get external IP via bash

  1. Bamm
    February 5, 2013 at 18:21

    This might help:

    #/bin/sh
    wget -qO- icanhazip.com

    • phil
      February 6, 2013 at 11:12

      Another very easy way to obtain your IP address. Thanks !

Leave a Reply

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