10 09/2014

Mac 获取网络配置信息

最后更新: Wed Sep 10 2014 12:39:11 GMT+0800

Mac 执行 ifconfig 返回下面信息

  • lo0 = loopback 回调地址 一般是 127.0.0.0
  • gif0 = Software Network Interface 软件网络接口
  • stf0 = 6to4 tunnel interface ipv6->ipv4 通道接口
  • en0 = Ethernet 0 以太网0 有线网卡
  • en1 = Ethernet 1 以太网1 无线网卡
  • fw0 = Firewire 火线
  • vmnet8 = Virtual Interface vmware 虚拟机接口(如果你装了 vmware 的话)
  • vmnet1 = Virtual Interface

我的 Mac 还有个 p2p0 不知道是干什么的!

注意:Mac 下的 en0 在 ubuntu 下叫 eth0

net.sh 脚本

下面的脚本返回 公网ip,主机名,网卡ip、mac地址、掩码、dns 和网络速度。你可以拷贝保存为 net.sh,然后

chmod +x net.sh

以后运行 ./net.sh

当然,如果不 chmod 的话,其实也可以直接 sh net.sh 运行

#! /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 " ";