首页 > 代码库 > Shell练习之:分析网卡信息
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"
该配置文件可以让你任意添加删除网卡,而达到你的需求,不需要动脚本的任何内容。。其次该配置文件要和脚本放在同一个目录下,在脚本中已经有所说明。。
下面看看执行效果:
从上面的信息可见,脚本的执行没有任何问题,功能基本达到。。。这个时候可能有人还会问?如果我一次性传递多个参数怎么办?好,这个问题,你自己解决,哈哈!!!
结束!!!
笨蛋的技术------不怕你不会!!!
本文出自 “笨蛋的技术” 博客,请务必保留此出处http://mingyang.blog.51cto.com/2807508/1551153
Shell练习之:分析网卡信息