Retrieve network info from the command line

Jun 17, '10 07:30:00AM

Contributed by: obearsstate

I found myself wanting to conveniently retrieve useful network information for my wired (en0) and wireless (en1) interfaces without having to open the Network System Preference panel, so I wrote a shell script to extract and format this information.

Just paste the following into your favorite text editor and save it as a shell script (e.g. getnet.sh), preferably somewhere in your $PATH. After you've saved it make sure it is executable by running chmod 0755 /path/to/getnet.sh from the Terminal.

#! /bin/bash

QUERY0="$(ipconfig getpacket en0)";
QUERY1="$(ipconfig getpacket en1)";
MAC0="$(ifconfig en0 | grep ether | awk '{print $2}')";
MAC1="$(ifconfig en1 | grep ether | awk '{print $2}')";

echo $QUERY0 | grep 'BOOTREPLY' > /dev/null;
ET=$?;

echo $QUERY1 | grep 'BOOTREPLY' > /dev/null;
WI=$?;

echo " ";

if [ $ET -eq 0 ] || [ $WI -eq 0 ]
 then
  PUBLIC="$(curl -s http://checkip.dyndns.org | awk '{print $6}' | awk 'BEGIN {FS = "<"} {print $1}')";
  echo "   Public IP: $PUBLIC";
fi;

echo "   Hostname: $HOSTNAME";
echo " ";

echo "Wired Ethernet (en0)";
echo "-----------------------";

if [ $ET -eq 0 ]
 then
  echo $QUERY0 | grep 'yiaddr = 0.0.0.0' > /dev/null;
  AT=$?;
  if [ $AT -eq 0 ]
   then
    IP="$(echo $QUERY0 | sed 's/.*ciaddr = \([[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\).*/\1 (Static)/')";
   else
    IP="$(echo $QUERY0 | sed 's/.*yiaddr = \([[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\).*/\1 (DHCP)/')";
  fi;
  SUBNET="$(echo $QUERY0 | sed 's/.*subnet_mask (ip): \([[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\).*/\1/')";
  ROUTER="$(echo $QUERY0 | sed 's/.*router (ip_mult): {\([^}]*\)}.*/\1/')";
  DNS="$(echo $QUERY0 | sed 's/.*domain_name_server (ip_mult): {\([^}]*\)}.*/\1/')";
  SEARCH="$(echo $QUERY0 | sed 's/.*domain_name (string): \(.*\) [[:alpha:]].*/\1/')";
  SPEED="$(ifconfig en0 | grep media: | sed 's/.*(//' | sed 's/ .*//' | sed 's/baseT/ MBit\/s/')";
  echo "  IP Address: $IP";
  echo "  Subnet Mask: $SUBNET";
  echo "    Router: $ROUTER";
  echo "  DNS Server: $DNS";
  echo "Search Domains: $SEARCH";
  echo "  MAC Address: $MAC0";
  echo "     Speed: $SPEED";
elif ! [ $ET -eq 0 ]
 then
  echo "  IP Address: inactive";
  echo "  MAC Address: $MAC0";
fi;

echo " ";
echo "Wireless Ethernet (en1)";
echo "-----------------------";

if [ $WI -eq 0 ]
 then
  echo $QUERY1 | grep 'yiaddr = 0.0.0.0' > /dev/null;
  AT=$?;
  if [ $AT -eq 0 ]
   then
    IP="$(echo $QUERY1 | sed 's/.*ciaddr = \([[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\).*/\1 (Static)/')";
   else
    IP="$(echo $QUERY1 | sed 's/.*yiaddr = \([[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\).*/\1 (DHCP)/')";
  fi;
  SUBNET="$(echo $QUERY1 | sed 's/.*subnet_mask (ip): \([[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\).*/\1/')";
  ROUTER="$(echo $QUERY1 | sed 's/.*router (ip_mult): {\([^}]*\)}.*/\1/')";
  DNS="$(echo $QUERY1 | sed 's/.*domain_name_server (ip_mult): {\([^}]*\)}.*/\1/')";
  SEARCH="$(echo $QUERY1 | sed 's/.*domain_name (string): \(.*\) [[:alpha:]].*/\1/')";
  SPEED="$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | grep lastTxRate: | sed 's/.*: //' | sed 's/$/ MBit\/s/')";
  echo "  IP Address: $IP";
  echo "  Subnet Mask: $SUBNET";
  echo "    Router: $ROUTER";
  echo "  DNS Server: $DNS";
  echo "Search Domains: $SEARCH";
  echo "  MAC Address: $MAC1";
  echo "     Speed: $SPEED"
elif ! [ $WI -eq 0 ]
 then
  echo "  IP Address: inactive";
  echo "  MAC Address: $MAC1";
fi;

echo " ";


Here is an example of the output from the script:
Public IP: 123.456.78.90
Hostname: CWB-MacBook.local
 
Wired Ethernet (en0)
-----------------------
IP Address: inactive
MAC Address: aa:bb:cc:dd:ee:ff
 
Wireless Ethernet (en1)
-----------------------
IP Address: 192.168.0.196 (DHCP)
Subnet Mask: 255.255.255.0
Router: 192.168.0.1
DNS Server: 192.168.0.1
Search Domains: woh.rr.com
MAC Address: 00:11:22:33:44:55
Speed: 117 MBit/s
[crarko adds: I tested this, and it works as described. You could also generate and parse a report from the system_profiler command if you are fluent with that utility.]

Comments (16)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20100604064451501