#!/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: " $GATEWAY echo "NAMESERVER: " $NAMESERVER echo "INTERNET: " $INTERNET echo "------------------------------" ping -c 3 127.0.0.1 > /dev/null 2>&1 if [ "$?" != "0" ]; then echo "Network card does not work! " else echo "Network card is OK!" fi ping -c 3 $GATEWAY > /dev/null 2>&1 if [ "$?" != "0" ]; then echo "Can't connect to GateWay!" else echo "GateWay is connected!" fi ping -c 3 $NAMESERVER > /dev/null 2>&1 if [ "$?" != "0" ]; then echo "Can't connect to DNS!" else echo "DNS is connected!" fi ping -c 3 $INTERNET > /dev/null 2>&1 if [ "$?" != "0" ]; then echo "Can't connect to Internet!" else echo "Internet is connected!" fi exit 0