#!/bin/bash # # Author: Georg Voell - georg.voell@oracle.com # Version: @(#)get-ip 3.0.1 11.06.2020 (c)2020 Oracle # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ # #@ Get the external ip of the instance and some more infos. #@ If there is no internet connection, the result would be empty. #@ #@Usage: get-ip [options] [key] #@ Options: #@ -h, --help : Displays helptext. #@ -v, --version : Displays the version of the script. #@ -o, --output : Output format: can be "plain", "keys" or "json". #@ Key: This script can be called with an optional parameter "key" (otherwise it prints out all keys) e.g: #@ ip : Displays the IP e.g. "87.162.84.113". #@ country: Displays the Country e.g. "Germany". #@ city : Displays the City e.g. "Berlin". #@ asn : Displays the ASN ID e.g. "AS3320" for "Deutsche Telekom AG" or "AS31898" for "ORACLE-BMC / OCI" #@ #@Examples: #@ get-ip ip #@ Displays the External IP of the instance (returns empty string if there is no internet connection). #@ get-ip --output json #@ Displays all results in json format. #@ get-ip --output keys #@ Displays all results in key-pair-value format. # # Exit codes: # 01: Unknown or wrong parameter. # 02: **curl** not found. This script needs **curl** to perform. # 03: No **jq** or wrong version of **jq**. This script needs **jq** to format "json". # 04: No internet access. This script calls an external tool to get ip infos. # 99: User interrupt. # # See also: # **check-port**(1), **get-platform**(1), **install-scripts**(1) # # ToDo: # # Known bugs: # # Update history: # # V 3.0.0 24.04.2020 New version # V 3.0.1 11.06.2020 Using library # script=${0} # Name of this script progstr=`basename "$script"` # Basename of the script progdir=`dirname "$script"` # Dirname of the script # Find executable bash library and source it lib=`which lib.bash 2>/dev/null | sed 's|^no 'lib.bash' in .*||'` if [ "$lib" != "" ]; then source "$lib" else if [ -r "${progdir}/lib.bash" ]; then source "${progdir}/lib.bash" else echo "Unexpected error: Unable to locate bash library 'lib.bash'." exit 1 fi fi # Set PATH to something useful if [ "$WORKPATH" != "" ]; then PATH="$WORKPATH" fi # Set the Base URL url="http://ifconfig.co" # ASN lookup info: https://securitytrails.com/blog/asn-lookup # Get ASN: https://stat.ripe.net/data/as-overview/data.json?resource=AS3320 # Preset param="" formatstr="plain" # Check parameters: Loop until all parameters are used up while [ $# -gt 0 ]; do pname=${1} case "$pname" in -o | --output) shift if [ "$1" != "" ]; then formatstr=`echo $1 | tr "[:upper:]" "[:lower:]"` if [ "$formatstr" != "plain" -a "$formatstr" != "keys" -a "$formatstr" != "json" ]; then errstr="Unknown format '$formatstr' after parameter '$pname'. Please choose from 'plain', 'keys' or 'json'." fi shift else errstr="Please specify a format ('plain', 'keys' or 'json') after parameter '$pname'." fi ;; -v | --version) shift showversion=1 ;; -h | --help) shift showhelp=1 ;; *) paramck=`echo "$1" | grep '^-'` # Types don't begin with '-' if [ "$param" = "" -a "$paramck" = "" ]; then param=`echo "$1" | tr "[:upper:]" "[:lower:]"` else errstr="Unknown parameter: '$1'." fi shift esac done # Display help or error message DisplayHelp # Check if he have curl and jq in path curl=`filecheck -x curl` jq=`check-version jq --min 1.5 | cut -d' ' -f3` if [ "$curl" = "" ]; then exitcode=2 errormsg $exitcode "($progstr) No 'curl' in '$PATH'. Please install first e.g. 'sudo yum -y install curl'." exit $exitcode else # Application Error = 3: Redo until we do not get an Application Error stat=3 while [ $stat -eq 3 ]; do if [ "$param" = "ip" -o "$param" = "country" -o "$param" = "city" -o "$param" = "asn" ]; then # Don't use JSON if we have the above parameter myurl="${url}/$param" else if [ "$jq" = "ok" -o "$param" = "" -a "$formatstr" = "json" ]; then # jq is installed and can be used myurl="${url}/json" else # not the right version of jq found exitcode=3 errormsg $exitcode "($progstr) No 'jq' in '$PATH' or 'jq version less than 1.5'." exit $exitcode fi fi # Get result from server via curl transfer --quiet --seconds 15 "$myurl" > $scratchfile stat=$? if [ $stat -eq 0 ]; then # Display the result if [ "$param" = "" ]; then if [ "$formatstr" = "json" ]; then cat $scratchfile else cat $scratchfile | browse-json --quiet --raw --output $formatstr fi else if [ "$myurl" = "${url}/json" ]; then cat $scratchfile | browse-json "$param" --quiet --raw --output $formatstr else # Display the value value=`head -n 1 $scratchfile` if [ "$formatstr" = "json" ]; then printf '{\n "%s": "%s"\n}\n' "$param" "$value" else if [ "$formatstr" = "keys" ]; then printf "%s:" $param fi echo "$value" fi fi fi else if [ $stat -ne 3 ]; then # No internet access? exitcode=4 Cleanup exit $exitcode fi fi done fi # Cleanup and exit Cleanup exit $exitcode