利用shell腳本分析網卡狀態信息
題目:
shell 編程
1.編寫腳本程序讀取網卡上的流量,收、發包數,丟包數以及丟包率;
2.要求網卡名稱可配置,不僅限於ethX,如pagX(X爲數字),網卡數量也可配置;
3.通過配置相應參數,控制各個網卡顯示相應信息;
4.要求程序的輸出簡潔直觀,輸出結果要便於二次處理;
5.配置文件要獨立與shell程序;
6.考慮該shell腳本在不同版本系統下可能出現的差異,舉出一兩例
這個需求是某某公司新進員工的一個內測題目,該員工尋求我的幫助,我就替他寫了一個版本。這個腳本,如果你能看懂,那麼恭喜你,你已經不屬於新手的行列了。哈哈。。除了第六點需要自行測試,其他的腳本內容如下:
腳本名: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"
該配置文件可以讓你任意添加刪除網卡,而達到你的需求,不需要動腳本的任何內容。。其次該配置文件要和腳本放在同一個目錄下,在腳本中已經有所說明。。
下面看看執行效果:
從上面的信息可見,腳本的執行沒有任何問題,功能基本達到。。。這個時候可能有人還會問?如果我一次性傳遞多個參數怎麼辦?好,這個問題,你自己解決,哈哈!!!
結束!!!
笨蛋的技術------不怕你不會!!!