linux 網路測試指令碼

來源:互聯網
上載者:User
#!/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
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.