Lan & Global IP

Heyas

Wasnt sure where to post, but thought this is most matching.
Thanks to Neo we can now see our IP and much more on: What is My IP Address?

I've always wanted a script to get my IP's, and thanks to Neo's effort, i now could do.
Outputs as follows:

$ myip.sh 
Internal	192.168.10.15
External	215.163.234.18
#!/usr/bin/env bash
# File: 	myip.sh
# Description:	Simply prints internal and external IP using http://www.unix.com/what-is-my-ip.php
# ------------------------------------------------------
#
#	Variables
#
	URL=http://www.unix.com/what-is-my-ip.php
	DATA=$(curl -s $URL) > /dev/zero
	str="DNS Lookup For"
#
#	Action & Display
#
	printf "%s\t%s\n" "Internal" "$(ifconfig | grep -i broadcast | grep ^[[:space:]] | awk '{ print $2}')"
	printf "%s\t%s\n" "External" "$(echo "$DATA" | sed s,"$str","\n\n$str",g  | sed s,"<"," ",g|grep "$str" | awk '{print $4}')"

Hope you like :slight_smile:
Happy easter!

1 Like

This will work for some common configurations, but it's possible for an adaptor to have more than one IP address.

Nice! Some opportunities to simplify:

lynx -dump http://www.unix.com/what-is-my-ip.php | awk '/DNS Lookup For/ {print "External: ", $NF}'
External:  215.163.234.18
ifconfig | awk -F"[ :]*" '/inet addr/ {print "Internal: ", $4}'
Internal:  10.1.1.1
Internal:  127.0.0.1
1 Like

Had to change your awk a bit for my system:

#
#	Variables
#
	URL=http://www.unix.com/what-is-my-ip.php
	T="T=\\t"
#
#	Action & Display
#
	lynx -dump "$URL" | awk '/DNS Lookup For/ {print "External: "T, $NF}' $T
	ifconfig | awk -F" " '/netmask / {print "Internal:"T, $2}' $T