Seventh Sense Rambling about life's little things, in 7 ≡ 1 (mod 6) fashion

« | »

BASH – Router’s Public IP Address For Mac

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



Direct connection to the internet

Directly connected to the internet


Configured behind a router

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.

Share
divider

Responses to BASH – Router’s Public IP Address For Mac

  1. [...] Go here to read the rest:  Gowtham » Seventh Sense » BASH – Router's Public IP Address For Mac [...]

  2. :D P1 says:

    Great post really fascinating post. I will certainly get back to this blog later to check some of other blogposts. Thanks!

  3. 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.

  4. Great post, but that doesn’t seem to be compatible with my router ip address, any ideas?

  5. I don’t accept as true with this particular article. However, I had researched in Google and I have found out that you’re right and I had been thinking in the incorrect way. Continue creating good quality material like this.

  6. Eve says:

    Such superb description! I have no clue how you came up with this text..it’d take me days. Well worth it though, I’d assume. Have you considered selling ads on your website?



Leave a Reply

Most of these posts, especially the ones with any hint of technical jargon, are intended to be Note2Self. But if any of them float your boat, then feel free to sail along. If you feel so generous, improve my journey with your comments &/or thoughts!
Looking for MS Thesis or PhD Dissertation Template in LaTeX? Click below!

MTU Create The Future
Twitter



Archives

Planet Kannada


Twitter: @sgowtham Facebook: @sgowtham Linked In: sgowtham