« Mystic River – One of National Geographic’s Best Wallpapers In 2009 | BASH – Extracting Subversion Log »
BASH – Router’s Public IP Address For Mac
January 19th, 2010 @ 11:47:09 | AWK, BASH, Mac, SED, Scripts
BASH, AWK and SED
BASH is a free software Unix shell written for the GNU Project. Its name is an acronym which stands for Bourne-again shell. The name is a pun on the name of the Bourne shell (sh), an early and important UNIX shell written by Stephen Bourne and distributed with Version 7 UNIX circa 1978, and the concept of being born again. BASH was created in 1987 by Brian Fox. In 1990 Chet Ramey became the primary maintainer. BASH is the default shell on most GNU/Linux systems as well as on Mac OS X and it can be run on most UNIX-like operating systems. It has also been ported to Microsoft Windows using the POSIX emulation provided by Cygwin, to MS-DOS by the DJGPP project and to Novell NetWare.
AWK is a general purpose programming language that is designed for processing text-based data, either in files or data streams, and was created at Bell Labs in the 1970s. The name AWK is derived from the family names of its authors — Alfred Aho, Peter Weinberger, and Brian Kernighan; however, it is not commonly pronounced as a string of separate letters but rather to sound the same as the name of the bird, auk. awk, when written in all lowercase letters, refers to the UNIX or Plan 9 program that runs other programs written in the AWK programming language. AWK is an example of a programming language that extensively uses the string data type, associative arrays (that is, arrays indexed by key strings), and regular expressions. The power, terseness, and limitations of AWK programs and sed scripts inspired Larry Wall to write PERL. Because of their dense notation, all these languages are often used for writing one-liner programs. AWK is one of the early tools to appear in Version 7 UNIX and gained popularity as a way to add computational features to a UNIX pipeline. A version of the AWK language is a standard feature of nearly every modern UNIX-like operating system.
sed (stream editor) is a Unix utility that parses text files and implements a programming language which can apply textual transformations to such files. It reads input files line by line, applying the operation which has been specified via the command line (or a sed script), and then outputs the line. It was developed in 1973-74 as a Unix utility by Lee McMahon of Bell Labs, and is available for UNIX and most flavors of Linux operating system.
Why This Script?
While in its simplest form of usage, this script will only print out the public (& private) IP address along with the SSID to which the machine is connected to, the results therefrom can be used for a variety of other purposes – I will let your imagination fly you to your dream destination!
The Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #! /bin/bash # BASH script to display the IP Address - one that the machine has, # as well as router's public IP Address, if any. # Tested on Apple Mac Snow Leopard 10.6.2 # First written : Gowtham, Thu Dec 31 12:05:35 EST 2005 # Last modified : Gowtham, Thu Dec 31 12:05:35 EST 2005 # en1 is the wireless interface. # Change appropriately, if different export WIFI_INTERFACE="en1" # Details about the ${WIFI_INTERFACE} ifconfig ${WIFI_INTERFACE} > public_ip.txt export STATUS=`cat public_ip.txt | grep "status" | awk '{print $4}'` # Analyze the details and display appropriate information if [ "$STATUS" = "active" ]; then export IPADDRESS=`cat public_ip.txt | grep broadcast | awk '{print $2}'` export SSID=`system_profiler SPAirPortDataType | \ grep -A1 "Current Network Information:" | \ tail -1 | awk -F ':' '{print $1}' | \ sed 's/^[ \t]*//;s/[ \t]*$//'` else echo echo " `date`" echo " You are not connected to the internet" echo exit 0 fi # Get the public IP address, $NSIP export NSIP=`curl -s http://checkip.dyndns.org | awk '{print $6}' | \ awk -F '<' '{print $1}'` # Compare $IPADDRESS & $NSIP # Display appropriate information along with SSID if [ "$IPADDRESS" = "$NSIP" ]; then echo echo " `date`" echo " SSID : $SSID" echo " IP Address : $IPADDRESS" echo else echo echo " `date`" echo " SSID : $SSID" echo " Machine's static IP Address : $IPADDRESS" echo " Router's public IP Address : $NSIP" echo fi # Delete the scratch file rm -f public_ip.txt |
Screenshots

Directly connected to the internet

Configured behind a router
I must thank Jon DeVree [@nuxi] for help with a previous version of this script [for linux] as well as Sarah for graciously letting me use her WiFi network.



[...] Go here to read the rest: Gowtham » Seventh Sense » BASH – Router's Public IP Address For Mac [...]
Great post really fascinating post. I will certainly get back to this blog later to check some of other blogposts. Thanks!
When you open net, you are bombarded with millions of article. However, unfortunately most of them are just a piece of junk. It is difficult to even read first paragraph of such articles. However, this article is not like them. It contains quality content, which can bind you to finish till the last words.