#!/bin/sh#*************************************************************# 網路測試指令碼# written by braincol 2011.04.06#*************************************************************#****************************************#被注釋掉的方法擷取網關所需的時間太長!#GATEWAY=`route | grep 'default.*' | awk '1{split($0,parts," ");print parts[2]}'` #**************************************** GATEWAY=`cat /proc/net/route | awk 'function char2num(char) { dec = "0123456789"; # decimal cap = "ABCDEF"; # capital min = "abcdef"; # minuscule if( length(char) > 1 ){ print "the length of the input char is erro!"; return; } if( index(dec,char) > 0 ) { return (index(dec,char) - 1); } else if( index(cap,char) > 0 ) { return (index(cap,char) + 9); } else if( index(min,char) > 0 ) { return (index(min,char) + 9); }else{ #print "input char error!"return 0}} function str2ip(str) { split(str,out,""); strout= sprintf("%d.%d.%d.%d", char2num(out[7])*16 + char2num(out[8]),char2num(out[5])*16 + char2num(out[6]), char2num(out[3])*16 + char2num(out[4]),char2num(out[1])*16 + char2num(out[2])); return strout;}(FNR>1) {split($0,parts," ")if(parts[3] != "00000000"){print str2ip(parts[3])}}'`#*******************************************#被注釋掉的方法擷取的dns太多,實際只需一個就夠了#NAMESERVER=`cat /etc/resolv.conf | grep 'nameserver.*' | sed 's/^.*nameserver *//g'`#******************************************* NAMESERVER=`cat /etc/resolv.conf |awk '(FNR<2) && /nameserver.*/' |sed 's/^.*nameserver *//g'`INTERNET="www.baidu.com"echo "GATEWAY: " $GATEWAYecho "NAMESERVER: " $NAMESERVERecho "INTERNET: " $INTERNETecho "------------------------------"ping -c 3 127.0.0.1 > /dev/null 2>&1if [ "$?" != "0" ]; thenecho "Network card does not work! "elseecho "Network card is OK!"fi ping -c 3 $GATEWAY > /dev/null 2>&1if [ "$?" != "0" ]; thenecho "Can't connect to GateWay!"elseecho "GateWay is connected!"fiping -c 3 $NAMESERVER > /dev/null 2>&1if [ "$?" != "0" ]; thenecho "Can't connect to DNS!"elseecho "DNS is connected!"fiping -c 3 $INTERNET > /dev/null 2>&1if [ "$?" != "0" ]; thenecho "Can't connect to Internet!"elseecho "Internet is connected!"fiexit 0