public
Last active

Retrieve network info from the command line

  • Download Gist
getnet.sh
Shell
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#! /bin/bash
 
### Script by obearsstate found on
### http://hints.macworld.com/article.php?story=20100604064451501
 
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 " ";

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.