Shell練習之:分析網卡資訊

來源:互聯網
上載者:User

標籤:linux shell nic analysis scripts shell題目

 


    利用shell指令碼分析網卡狀態資訊

題目:

shell 編程
1.編寫指令碼程式讀取網卡上的流量,收、發包數,丟包數以及丟包率;
2.要求網卡名稱可配置,不僅限於ethX,如pagX(X為數字),網卡數量也可配置;
3.通過配置相應參數,控制各個網卡顯示相應資訊;
4.要求程式的輸出簡潔直觀,輸出結果要便於二次處理;
5.設定檔要獨立與shell程式;
6.請考慮該shell在不同作業系統版本下的統計差異(考慮系統:AS5,AS6,TurboLinux等系統的情況)


    這個需求是某某公司新進員工的一個內測題目,該員工尋求我的協助,我就替他寫了一個版本。這個指令碼,如果你能看懂,那麼恭喜你,你已經不屬於新手的行列了。哈哈。。除了第六點需要自行測試,其他的指令碼內容如下:

指令碼名:nic_information_analysis.sh

#!/bin/bash
#date:2014-09-10
#author:yourname
#function:
#    used to analysis information for nic(network interface card)
#Variables description:
#   scriptDir---the directory path of script
#   nicName---storage the nic name list
#   RX---recive flow,but the unit is byte
#   TX---transfer flow,but the unit is byte
#   reciveFlow---recive flow,but the unit is MByte
#   transferFlow---transfer flow,but the unit is MByte
#   totalFlow----the summary of reciveFlow and transferFlow,the unit is MByte
#   rpkgnum---recive packets number
#   spkgnum---send(transfer) packets number
#   rdpkgnum---the drop packet number based on recive packets
#   sdpkgnum---the drop packet number based on send packets
#   rdratio---the drop packet ratio based on recive packets
#   sdratio---the drop packet ratio based on send packets
#
#Arguments description:
#   rp---recive packets
#   tp---transfer packets
#   dp---drop packets
#   dra---drop ratio
#   all---all information for every netcard
#   

#description:
#   Please set variable "scriptDir" first!!!
#   

#Notice: this script and config.txt must be in the same directory,or you can set right #directory

#
scriptDir=/home/workspace
. $scriptDir/config.txt

for nic in $nicName
do
ifconfig $nic >& /dev/null
[ $? -ne 0 ] && echo -e "\033[31mWrong: NIC $nic is not exist! Please check config file: $scriptDir/config.txt\033[0m" && exit 1
done

function flow_count() {
RX=`ifconfig $1 | sed -n ‘/RX bytes/ s/^[[:space:]]*//p‘ | awk -F ‘[ :]‘ ‘{print $3}‘`
TX=`ifconfig $1 | sed -n ‘/RX bytes/ s/^[[:space:]]*//p‘ | awk -F ‘[ :]‘ ‘{print $(NF-2)}‘`
reciveFlow=$(echo "scale=2;$RX/1024" | bc)M
transferFlow=$(echo "scale=2;$TX/1024" | bc)M
totalFlow=$(echo "scale=2;`expr $RX + $TX`/1024" |  bc)M
echo "RX Flow is $reciveFlow"
echo "TX Flow is $transferFlow"
echo "Total Flow is $totalFlow"
echo
}

function recivePackets_count() {
rpkgnum=`ifconfig $1 | sed -n ‘/RX packets/ s/^[[:space:]]*//p‘ | awk -F ‘[ :]‘ ‘{print $3}‘`
echo "recive packet number is: $rpkgnum"
echo
}
function sendPackets_count() {
spkgnum=`ifconfig $1 | sed -n ‘/TX packets/ s/^[[:space:]]*//p‘ | awk -F ‘[ :]‘ ‘{print $3}‘`
echo "send packet number is: $spkgnum"
echo
}
function dropPackets_count() {
rdpkgnum=`ifconfig $1 | sed -n ‘/RX packets/ s/^[[:space:]]*//p‘ | awk -F ‘[ :]‘ ‘{print $7}‘`
sdpkgnum=`ifconfig $1 | sed -n ‘/TX packets/ s/^[[:space:]]*//p‘ | awk -F ‘[ :]‘ ‘{print $7}‘`
echo "drop packet number of RX is: $rdpkgnum"
echo "drop packet number of TX is: $sdpkgnum"
echo
}
function dropratio_count() {
rdratio=$(echo "scale=2;$rdpkgnum/($rdpkgnum+$rpkgnum)*100" | bc)%
sdratio=$(echo "scale=2;$sdpkgnum/($sdpkgnum+$spkgnum)*100" | bc)%
echo "drop ratio of RX is: $rdratio"
echo "drop ratio of TX is: $rdratio"
echo
}

function dropratio_count_single() {
rpkgnum=`ifconfig $1 | sed -n ‘/RX packets/ s/^[[:space:]]*//p‘ | awk -F ‘[ :]‘ ‘{print $3}‘`
spkgnum=`ifconfig $1 | sed -n ‘/TX packets/ s/^[[:space:]]*//p‘ | awk -F ‘[ :]‘ ‘{print $3}‘`
rdpkgnum=`ifconfig $1 | sed -n ‘/RX packets/ s/^[[:space:]]*//p‘ | awk -F ‘[ :]‘ ‘{print $7}‘`
sdpkgnum=`ifconfig $1 | sed -n ‘/TX packets/ s/^[[:space:]]*//p‘ | awk -F ‘[ :]‘ ‘{print $7}‘`
rdratio=$(echo "scale=2;$rdpkgnum/($rdpkgnum+$rpkgnum)*100" | bc)%
sdratio=$(echo "scale=2;$sdpkgnum/($sdpkgnum+$spkgnum)*100" | bc)%
echo "drop ratio of RX is: $rdratio"
echo "drop ratio of TX is: $sdratio"
echo
}

function All_information_summary() {
flow_count $1
recivePackets_count $1
sendPackets_count $1
dropPackets_count $1
dropratio_count $1
}

function print_information(){
    echo "Follow information is about $1 of every netcard:"
}

function analysis_result() {
    for i in $nicName
    do
        echo -e "\033[31mNetcard is $i:\033[0m"
        $1 $i
    done
}

case $1 in
    flow)
        print_information "the flow information"
        analysis_result flow_count
        ;;
    rp)  
        print_information "the recive packet number"
        analysis_result recivePackets_count
        ;;
    tp)
        print_information "the send packet number"
        analysis_result sendPackets_count

        ;;
    dp)
        print_information "the drop packet number"
        analysis_result dropPackets_count
        ;;
    dra)
        print_information "the drop packet ratio"
        analysis_result dropratio_count_single
        ;;
    all)
        print_information "all information"
        analysis_result  All_information_summary
        ;;
    *)
        echo "Usage $0 {flow|rp|tp|dp|dra|all}"
        echo
        ;;
esac



網卡設定檔名:config.txt,內容如下:

#configure your network card
nicName="eth0 lo"


該設定檔可以讓你任意添加刪除網卡,而達到你的需求,不需要動指令碼的任何內容。。其次該設定檔要和指令碼放在同一個目錄下,在指令碼中已經有所說明。。


下面看看執行效果:

650) this.width=650;" style="float:none;" title="3.jpg" alt="wKioL1QRRkeRFmgCAAPSdUa-3_A084.jpg" src="http://s3.51cto.com/wyfs02/M02/49/3D/wKioL1QRRkeRFmgCAAPSdUa-3_A084.jpg" />

650) this.width=650;" style="float:none;" title="4.jpg" alt="wKiom1QRRjiCRRgxAAJi9FpgnBg005.jpg" src="http://s3.51cto.com/wyfs02/M02/49/3B/wKiom1QRRjiCRRgxAAJi9FpgnBg005.jpg" />

650) this.width=650;" style="float:none;" title="5.jpg" alt="wKioL1QRRkijVF2HAAKNEx6kSKw475.jpg" src="http://s3.51cto.com/wyfs02/M00/49/3D/wKioL1QRRkijVF2HAAKNEx6kSKw475.jpg" />

 

    從上面的資訊可見,指令碼的執行沒有任何問題,功能基本達到。。。這個時候可能有人還會問?如果我一次性傳遞多個參數怎麼辦?好,這個問題,你自己解決,哈哈!!!

 

結束!!!

           笨蛋的技術------不怕你不會!!!

 


 



本文出自 “笨蛋的技術” 部落格,請務必保留此出處http://mingyang.blog.51cto.com/2807508/1551153

Shell練習之:分析網卡資訊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.