BASH – Router’s Public IP Address

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.

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
#! /bin/bash
 
# POSIX SH shell script to display the IP Address - one that the machine has, 
# as well as router's public IP Address, if any.
 
# First written : Gowtham,  Wed Dec 28 22:05:35 EST 2005
# Last modified : Gowtham,  Fri Dec 30 06:57:50 EST 2005
#                 Gowtham,  Tue Jan  3 09:52:47 EST 2006
#                 Jon DeVree, Tue Jan  3 22:28:15 EST 2006 
 
IFACE=`netstat -rn | grep ^0.0.0.0 | awk '{print $8}'`
 
if [ "$IFACE" = "" ];
then
  echo
  echo " You are not connected to the internet"
  echo
  exit 0
fi
 
IP=`/sbin/ifconfig $IFACE | grep Bcast | awk '{print $2}' | awk -F ':' '{print $2}'`
NSIP=`curl -s http://checkip.dyndns.org | awk '{print $6}' | \
            awk -F '<' '{print $1}'`
 
if [ "$IP" = "$NSIP" ];
then
  echo
  echo " Your machine is directly connected to the internet"
  echo " IP Address : $IP"
  echo
else
  echo
  echo " Your machine is configured behind a router"
  echo " Machine's static IP Address : $IP"
  echo " Router's  public IP Address : $NSIP"
  echo
fi

One Reply to “BASH – Router’s Public IP Address”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.