#!/bin/bash
###########################################################################################
#
# Author: ccztux
# http://www.linuxinside.at
# 2011-12-28
#
# Last Modification: ccztux
# http://www.linuxinside.at
# 2013-02-11
#
# Description: Find out public IP.
#
# Changelog: 1.0 Initial revision
# 1.1 Bugfix: chkRC
# 1.2 Verbose output added
# 1.3 whatismyip.org-address changed
# 1.4 changed whatismyip.org to ifconfig.me
# 1.5 script reworked, chkInetConn added
#
# License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
# This is free software; you are free to change and redistribute it.
# There is NO WARRANTY, to the extent permitted by law.
#
###########################################################################################
###################
# define some vars
###################
script_name="${0##*/}"
script_version="1.5"
script_copyright="(c) Copyright 2013 linuxinside.at"
script_license="GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\nThis is free software; you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law."
selected_prog=
ping_bin=`which ping`
host_bin=`which host`
disable_chkinetconn=
verbose=
cmd=
rc=
############
# functions
############
function usage() {
echo -e "Usage: $0 OPTIONS
This Script is used to show you your public IP.
OPTIONS:
-h Shows this help.
-c Use curl to get your public IP.
-w Use wget to get your public IP.
-d Disable check if internet connection is established.
-v Verbose output.
-V Show script-version"
}
function chkInetConn() {
if [ "$verbose" == "1" ] ;then
echo "VERBOSE INFOS: START"
if [ "$disable_chkinetconn" != "1" ] ;then
echo -e "VERBOSE INFO: Check if internet connection is established is ENABLED.\nVERBOSE INFO: Checking internet connection:"
fi
$ping_bin -c 1 google.at
rc=$?
echo
chkRC "${ping_bin##*/}" "$rc"
$host_bin ifconfig.me
rc=$?
echo
chkRC "${host_bin##*/}" "$rc"
else
$ping_bin -c 1 google.at 2>&1> /dev/null
rc=$?
chkRC "${ping_bin##*/}" "$rc"
$host_bin ifconfig.me 2>&1> /dev/null
rc=$?
chkRC "${host_bin##*/}" "$rc"
fi
}
function chkRC() {
if [ "$2" != "0" ] ;then
echo "$1-Error-Code: $2"
exit $2
fi
}
##############
# get options
##############
while getopts "h.c.w.d.v.V." OPTION
do
case $OPTION in
h)
usage
exit 251
;;
c)
selected_prog=`which curl`
;;
w)
selected_prog=`which wget`
;;
d)
disable_chkinetconn="1"
;;
v)
verbose="1"
;;
V)
echo -e "Script-Name: $script_name\nScript-Version: $script_version\n$script_copyright\n\n$script_license"
exit 252
;;
?)
usage
exit 253
;;
esac
done
################
# check options
################
if [ -z "$selected_prog" ] || [ "$#" == "0" ] ;then
usage
exit 254
fi
#######
# main
#######
if [ "$disable_chkinetconn" != "1" ] ;then
chkInetConn
fi
if [ "${selected_prog##*/}" == "curl" ] ;then
if [ "$verbose" == "1" ] ;then
if [ "$disable_chkinetconn" == "1" ] ;then
echo -e "VERBOSE INFOS: START\nVERBOSE INFO: Check if internet connection is established is DISABLED."
fi
echo -e "VERBOSE INFO: Getting public IP:"
cmd=`$selected_prog http://ifconfig.me/ip`
rc=$?
echo -e "VERBOSE INFOS: END\n"
else
cmd=`$selected_prog -s http://ifconfig.me/ip`
rc=$?
fi
chkRC "$selected_prog" "$rc"
elif [ "${selected_prog##*/}" == "wget" ] ;then
if [ "$verbose" == "1" ] ;then
if [ "$disable_chkinetconn" == "1" ] ;then
echo -e "VERBOSE INFOS: START\nVERBOSE INFO: Check if internet connection is established is DISABLED."
fi
echo -e "VERBOSE INFO: Getting public IP:"
cmd=`$selected_prog -v -O - http://ifconfig.me/ip`
rc=$?
echo -e "VERBOSE INFOS: END\n"
else
cmd=`$selected_prog -q -O - http://ifconfig.me/ip`
rc=$?
fi
chkRC "$selected_prog" "$rc"
fi
echo $cmd
exit 0
Comments