Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Scripts to create an IP info summary display UNIX
I created the following scripts to mimic the ipconfig /all functionality found in the Windows NT operating system. In the unix world, all of the info is available, but not in one place which is where this script comes in handy. It concatenates all of the various pieces of IP configuration into one easy-to-read display. The bonus is all of the functions used are contained in a separate file, which can be sourced into your shell environment and used individually.

Download the scripts (4KB download), and install them wherever you like. The first script, functions.sh, contains the functions that I use in the second file, ipinfo.sh. Be sure to alter the second line of the ipinfo.sh file to point to the correct location of functions.sh.

When you run ipinfo.sh from the command line, you will receive output similar to the following:
--------------------------------------------------------
Public:       xxx.xxx.xxx.xxx
Loopback:     127.0.0.1 / 255.0.0.0
Airport:      10.2.0.168 / 255.255.255.0
Gateway:      10.2.0.1
Domain:       yourdomain.com
Nameserver:   10.1.0.50
Nameserver:   192.168.1.16
--------------------------------------------------------
I split the functions out of the main script to I could use them in my bashrc file individually. The functions are as follows: get_public, get_ip, get_mask, get_gateway, get_dns, get_domain The get_ip and get_mask functions take a single argument of an interface name (lo0, en0, en1, fw0, etc.). After these are added to my shell environment, I can do things like ping $(get_gateway) or get_ip en1.

[robg adds: These scripts worked as described in my testing. Remember to make ipinfo.sh executable via chmod 755 ipinfo.sh.]
    •    
  • Currently 3.17 / 5
  You rated: 2 / 5 (6 votes cast)
 
[17,523 views]  

Scripts to create an IP info summary display | 22 comments | Create New Account
Click here to return to the 'Scripts to create an IP info summary display' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Scripts to create an IP info summary display
Authored by: skrawcke on Sep 01, '06 08:50:33AM

if you are on a DHCP network you could also use
> ipconfig getpacket <interface name>

to get info like gateway, MAC address, netmask, DNS server and Domain names.

if you know what you are looking for you can use
> ipconfig getoption <interface name> "option name"

like

> ipconfig getoption en0 domain_name
foo.bar.com.

to get just the ip address you can do
> ipconfig getifaddr en0
192.168.1.2

that way you do not need to do any cutting, greping, or awk, in your script.



[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: jmdevaney on Sep 01, '06 10:55:22AM

True but that is not as much fun as converting hex to decimal... lol


---
Jdevaney



[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: rameeti on Sep 01, '06 09:27:58AM

I find the MAC address, Host Name, and whethere DHCP is enabled to also be handy to know when I am looking for 'all' info.



[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: gghose on Sep 01, '06 10:06:06AM
I've written a script that also reports the current hardware interface, the current location setting, checks proxy connections for being valid, and guesses at your geographic location. It's called locationinfo and it's included in my previously described LocationChanger package
http://www.ghoselab.cmrr.umn.edu/Software/LocationChanger.zip

It's not all that big, so if you want to copy and paste the following into a file and set the file executable (chmod 755), be my guest:


#!/bin/sh

# display location info
# Geoffrey M. Ghose

ARD=/System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Support/networksetup-panther
GATEWAY=`route get default 2> /dev/null | grep gateway | awk '{print $2}'`
SSID=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I\
| grep ' SSID:' | cut -d ':' -f 2 | tr -d ' '`
BSSID=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I\
| grep ' BSSID:' | cut -d ':' -f 2- | tr -d ' '`
INTF=`route get default | grep interface | awk '{print $2}'`
IP=`ifconfig $INTF | grep 'inet ' | cut -d ' ' -f 2`
if [ $GATEWAY ]; then
GATEWAYMAC=`arp $GATEWAY | awk '{print $4}'`
fi
LOCATION=`scselect 2>&1 | grep '*' - | tail -n 1 | awk '{print $3}' | tr -d "()"`
HOSTNAME=`hostname`
SERVICE_GUID=`echo "open|||get State:/Network/Global/IPv4|||d.show" | \
tr '|||' '\n' | scutil | grep "PrimaryService" | awk '{print $3}'`
INTERF=`echo "open|||get Setup:/Network/Service/$SERVICE_GUID|||d.show" |\
tr '|||' '\n' | scutil | grep "UserDefinedName" | awk -F': ' '{print $2}'`

echo Location= $LOCATION
if [ $GATEWAYMAC ]; then
echo Gateway MAC= $GATEWAYMAC
fi
if [ $BSSID ]; then
echo BSSID= $BSSID
fi
if [ $SSID ]; then
echo SSID= $SSID
fi
echo IP= $IP
echo Hostname= $HOSTNAME
echo Interface= $INTERF
function getproxy {
GETPROXY="-get${1}proxy"
HOST=`$ARD $GETPROXY "$INTERF" 2> /dev/null | grep Server | awk '{print $2}'`
if [ $HOST ]; then
PORT=`$ARD $GETPROXY "$INTERF" 2> /dev/null | grep Port | awk '{print $2}'`
STATUS=`echo " " | telnet $HOST $PORT 2> /dev/null`
MATCH=`echo $STATUS | grep Connected - | tr -d '\n' | tr -d ' '`
if [ $MATCH ]; then
echo " " $1: $HOST $PORT Reachable
if [ $1 == "web" ]; then
WEBPROXY="-x $HOST:$PORT"
fi
else
echo " " $1: $HOST $PORT Not Reachable
fi
fi
}
echo Set Proxies:
getproxy ftp
getproxy web
getproxy secureweb
getproxy streaming
getproxy gopher
getproxy socksfirewall

GEO=`curl -m 3 $WEBPROXY -s http://api.hostip.info/rough.php | grep City - | sed -e 's/^City: //'`
echo Where: $GEO

[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: father2a-f on Sep 01, '06 10:46:24AM

I copied and pasted this script into a new GeekTool console and it worked and displayed like a charm. Thanks.



[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: mikemccallum on Sep 01, '06 10:45:23AM

Hey, these seem to be begging to be used with GeekTool!



[ Reply to This | # ]
A suggested code modification
Authored by: bomolub on Sep 01, '06 01:43:55PM
I modified the function get_ip in "functions.sh":


get_ip ()
{
ifconfig $1 2>&1 | grep inet | grep -v inet6 | awk '{print $2}'
}

This hides stderr messages; before doing this I was getting an output from "ipinfo.sh" like the following:

Public: XXX.XXX.XXX.XXX
Loopback: 127.0.0.1 / 255.0.0.0
Wired: 10.0.0.101 / 255.255.255.0
ifconfig: interface en1 does not exist
Gateway: 10.0.0.1
Nameserver: 10.0.0.1

The error message occurred when the script was looking for an Airport (en1), which I don't have.

[ Reply to This | # ]

A suggested code modification
Authored by: jmdevaney on Sep 01, '06 01:58:34PM

Very nice, both of my machines have airport cards so I never hit that bug... thanks.

---
Jdevaney



[ Reply to This | # ]
I took it a *tiny* step further and added
Authored by: st3phen on Sep 01, '06 02:16:14PM

an alias (in ~/.bash_profile) as follows:

alias ipinfo='/path/to/ipinfo.sh'

This way, one only has to type 'ipinfo' instead of 'ipinfo.sh'. Small change, but it makes it look a slight bit slicker...



[ Reply to This | # ]
Alternatively...
Authored by: rhowell on Sep 01, '06 06:09:44PM

...rename "ipinfo.sh" to "ipinfo".



[ Reply to This | # ]
Well...
Authored by: st3phen on Sep 01, '06 10:59:08PM

there you go!



[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: S Barman on Sep 01, '06 09:49:02PM
Ok... I couldn’t help myself. As a former hardcore UNIX programmer, I had to modify the functions.sh to speed them up. First, rather than use multiple grep’s, I used the full capabilities of awk along with sed and added some perl code.

When writing shell script, there are a few things to consider:

  • Every time you use a pipe, the system sets up a file in the kernel. While a pipe is not written to the disk, pipes use in-memory buffers that have to be managed
  • For each pipe, a new process is started concurrently. If you have a command like cmd | grep x | awk '{print $2}', you start three processes.
  • Understanding shell script nuances does help. For example, a construct like [ -e filename ] && command will check whether filename exists. If it does then command is executed. Using this construct rather than an if-then statement means that there are fewer lines to process. Ok, on today’s system that only reduces the execution time by miliseconds, but (IMHO) every little bit counts!
Without further ado, here are my modifications:
#!/bin/bash
get_mask ()
{
        # one call to perl removes all that conversion stuff and makes this faster
        [ "$1" == "" ] && return
        ifconfig $1 | perl -na -e 'print join(".",unpack("CCCC",pack("L*",oct $F[3]))) if (m/mask/);'
}

get_public ()
{
        # removed two calls to awk with one call to sed
        curl -s http://checkip.dyndns.org | sed 's/.*: ([^<]*)<.*/1/'
}

get_gateway ()
{
        # removed a call to grep and made one call to awk
        netstat -rn | awk '/default/{print $2}'
        # as an alternative, sed would be faster:
        # netstat -rn | sed -n '/default/s/default *([^ ]*).*/1/p'
}

get_dns ()
{
        # instead of a call to cat AND grep, try:
        [ -e /etc/resolv.conf ] && awk '/nameserver/{print $2}' /etc/resolv.conf
}

get_domain ()
{
        # same issue as above
        [ -e /etc/resolv.conf ] && awk '/domain/{print $2}' /etc/resolv.conf
}

get_ip ()
{
        # one call to awk and removed two calls to grep. There's a space after 'inet '
        ifconfig $1 | awk '/inet /{print $2}'
}


[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: jmdevaney on Sep 02, '06 04:48:32AM

Great info, I knew my script was a kludge but to me that is the great thing about unix. I have a few scripts that some of your suggestions could do wonders in speeding up.

---
Jdevaney



[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: eagle on Sep 02, '06 05:38:46AM

Doing that outputs the netmask in reverse order. Also, the public IP address area prints the complete HTML returned instead of just the IP address. If I knew enough about sed and unpack/pack, I'd fix it myself...

--------------------------------------------------------------------------------
Public: <html><head><title>Current IP Check</title></head><body>Current IP Address: 24.225.90.70</body></html>
Loopback: 127.0.0.1 / 0.0.0.255
Airport: 10.1.1.2 / 224.255.255.255
Gateway: 10.1.1.1
Domain: earthlink.net
Nameserver: 10.1.1.1
--------------------------------------------------------------------------------



[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: knouse on Sep 02, '06 12:50:13PM
the public IP address area prints the complete HTML returned instead of just the IP address

The sed command needs the -E option

get_public ()
{
        curl -s http://checkip.dyndns.org | sed -E 's/.*: ([^<]*)<.*/\1/'
}


[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: knouse on Sep 02, '06 01:40:41PM

And the replacement string must be escaped \1 instead of 1



[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: ScottTFrazer on Sep 06, '06 06:09:33AM

I get an error when I try to use -E with sed, changing it to -e doesn't really seem to work either.

Also, I always seem to reply to wrong thread. but that's a user issue :-)



[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: knouse on Sep 02, '06 02:32:06PM
Doing that outputs the netmask in reverse order

Since I don't know Perl I piped the output to sed to reverse the order

get_mask ()
{
        [ "$1" == "" ] && return
        ifconfig $1 | perl -nase 'print join(".",unpack("CCCC",pack("L*",oct $F[3]))) if (m/mask/);' \
            | sed -E 's/([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/\4.\3.\2.\1/'
}



[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: eagle on Sep 02, '06 05:29:30AM

Another change: I have no need to have functions.sh separate from ipinfo.sh (perhaps you do), so I replaced the include in ipinfo.sh with the code from functions.sh.



[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: unforeseen:X11 on Sep 03, '06 01:33:14AM
Just want to point out that there is a widget which displays all this info for every active interface. Additionally, it has the location, can ping, send email-notifications and other funny stuff, too.
http://www.widgetschmie.de/widgets/NetworkStat/

---
this is not the sig you`re looking for.

[ Reply to This | # ]

Scripts to create an IP info summary display
Authored by: ScottTFrazer on Sep 06, '06 05:59:42AM

I get an error when I try to use -E with sed, changing it to -e doesn't really seem to work either.



[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: knouse on Sep 06, '06 12:18:36PM
I get an error when I try to use -E with sed

I posted the -E mod. I'm using Tiger. You?

[ Reply to This | # ]