getip

obtain Internal/External IP
git clone git://mahdi.pw/getip.git
Log | Files | Refs | README | LICENSE

getip (2481B)


      1 #!/bin/sh
      2 
      3 # getip's version/copyright
      4 GETIP_VERSION="1.0.3"
      5 GETIP_COPYRIGHT="Copyright (C) 2021-2022 Mahdi Mirzade"
      6 
      7 # Handle arguments
      8 filename="$0"
      9 subcommand="$(echo "$1" | sed 's/-//g')"
     10 
     11 # Grab public IP address
     12 publicIP () {
     13 	RESOLVERS='@resolver1.opendns.com @resolver2.opendns.com @resolver3.opendns.com @resolver4.opendns.com'
     14 	for resolver in $RESOLVERS ; do
     15 		PUBLIC_IP="$(dog --short myip.opendns.com "$resolver")"
     16 		[ -n "$PUBLIC_IP" ] && break
     17 	done
     18 }
     19 
     20 # Grab private IP address
     21 privateIP () {
     22 	case "$(uname)" in
     23 		Darwin*|FreeBSD*)
     24 			PRIVATE_IP=$(ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}')
     25 		;;
     26 		Linux*)
     27 			PRIVATE_IP=$(ip address | grep 'inet ' | grep 'scope global' | grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' -o | head -1)
     28 		;;
     29 	esac
     30 }
     31 
     32 # Grab IP's geolocation information
     33 geolocation () {
     34 	publicIP
     35 	curl -s "http://ipwhois.app/json/$PUBLIC_IP"
     36 }
     37 
     38 # Grab usage manual
     39 usage () {
     40 printf "Usage: %s [METHOD]\n
     41 General Methods:
     42     h,  help            Show this manual
     43     v,  version         Show version number\n
     44 IP Information Methods:
     45     pub,    public      Get Your Public IP Address
     46     priv,   private     Get Your Private IP Address
     47     j,      json        Get Your Public IP's Full Information in Json\n
     48 Other methods can be also grabbed from json output,
     49     for example this is your json output:
     50     \033[34m{
     51         ...
     52         \"continent\": \"Europe\",
     53         \"country\": \"Germany\",
     54         \"region\": \"Bavaria\",
     55         \"city\": \"Nuremberg\",
     56         \"latitude\": 49.4254092,
     57         \"longitude\": 11.0796553,
     58         ...
     59     }\033[0m
     60     and if you want to get IP's country:
     61     \033[34m$ %s country
     62     Output: Germany\033[0m\n\n" "$filename" "$filename"
     63 }
     64 
     65 version () {
     66 printf "\n\tgetip (Geolocation, Private/Public IP) v$GETIP_VERSION
     67 \t$GETIP_COPYRIGHT
     68 
     69 \tThis program may be freely redistributed under
     70 \tthe terms of the MIT License.\n\n"
     71 }
     72 
     73 case "$subcommand" in
     74 	[Pp][Rr][Ii][Vv][Aa][Tt][Ee]|[Pp][Rr][Ii][Vv])
     75 		privateIP
     76 		printf "$PRIVATE_IP\n"
     77 	;;
     78 	[Pp][Uu][Bb][Ll][Ii][Cc]|[Pp][Uu][Bb])
     79 		publicIP
     80 		printf "$PUBLIC_IP\n"
     81 	;;
     82 	[Jj][Ss][Oo][Nn]|[Jj])
     83 		geolocation | jq
     84 	;;
     85 	[Vv][Ee][Rr][Ss][Ii][Oo][Nn]|[Vv])
     86 		version
     87 	;;
     88 	[Hh][Ee][Ll][Pp]|[Hh])
     89 		usage
     90 	;;
     91 	*)
     92 		if [ -n "$subcommand" ]; then
     93 			data=$(geolocation)
     94 			var=$(echo $data | jq ".${subcommand}" | sed 's|"||')
     95 		fi
     96 		if [ -n "$var" ] && [ "$var" != "null" ]; then
     97 			printf "$var\n"
     98 		else
     99 			usage
    100 		fi
    101 	;;
    102 esac