shell学习:TCP活动状况报告的生成

shell学习:TCP活动状况报告的生成

1、实验要求

使用命令netstat可以列出系统当前IP、TCP、UDP、ICMP等与通信相关协议的统计数据。

--statistics 可以列出网络活动相关协议的详细信息。

netstat --statistics

执行效果如下:

image-20200503224707591

可以获取TCP等协议的一些统计信息。

编写shell脚本程序,每隔1分钟生成1行信息:当前时间;这一分钟内TCP发送了多少报文; 接收了多少报文;收发报文总数;行尾给出符号+或-或空格(+表示这分钟收 发报文数比上分钟多10包以上,差别在10包或以内用空格,否则用符号-)。

2 、代码设计

2.1 获取系统当前时间TCP发送报文和接受报文的总量

使用netstat --statistics可以获取到TCP协议的相关信息,其中segements received/sentout表示当前系统TCP报文发送和接受的总量,可以根据该信息的差值获取一段时间内TCP报文发送和接受的量。

image-20200503225712242

使用grep可以进行行筛选,直接筛选出需要的信息。

扫描二维码关注公众号,回复: 11177493 查看本文章
netstat --statistics|grep "segments received"
netstat --statistics|grep "segments sent out"

执行效果如下:

image-20200503230403635

关于出现的bad segments,会在后面进行expr进行模式匹配的时候排除掉。

将上面的查询指令存入到shell变量中,使用expr进行模式匹配,匹配的模式是:数字+segments received/sent out

这样就可以把bad segments这种情况去掉,只获得系统TCP报文的发送和接受总量。

curr_total_recv=`expr "$recv_str" : '^ *\([0-9]*\) segments received'`
curr_total_send=`expr "$send_str" : '^ *\([0-9]*\) segments sent out'`

我把获取TCP信息写成了一个函数,get_send_recv().方便后面的调用。

get_send_recv(){
eval 'recv_str=`netstat --statistics|grep "segments received"`'
eval 'send_str=`netstat --statistics|grep "segments sent out"`'
time=$(date "+%Y-%m-%d-%H:%M")
curr_total_recv=`expr "$recv_str" : '^ *\([0-9]*\) segments received'`
curr_total_send=`expr "$send_str" : '^ *\([0-9]*\) segments sent out'`
}

2.2 获取一分钟内TCP包的发送量和接受量

由于已经可以获得当前时间的总量,每隔一分钟获取一次总量,存储在相应变量中,然后每一分钟做一次差值再将差值输出即可。

在我的代码中,curr_total_send/recv是当前时间的TCP发送和接受总量,prev_total_recv/send是前一分钟的TCP发送和接受总量。thism_send/recv是做差值得到的这一分钟的TCP发送和接受量,而last_send/recv是上一分钟的TCP发送和接受量。再新的一分钟开始时,让prev=curr,thism=last即可。

使用while循环,并且把while循环条件写成sleep 60来让程序休息一分钟,实现每一分钟获取一次的效果。

while(sleep 60)
do
        prev_total_recv=$curr_total_recv
        prev_total_send=$curr_total_send
        last_send=$thism_send
        last_recv=$thism_recv
        get_send_recv
        thism_send=`expr $curr_total_send - $prev_total_send`
        thism_recv=`expr $curr_total_recv - $prev_total_recv`
done

2.3 获取上一分钟和这一分钟TCP包数据的差值

根据2,2中的设计,可以求出thism和last的差值,就是两分钟之间TCP数据包发送接受的差值。然后使用test测试,如果大于10包就输出符号'+',小于10包就输出' ',其他情况输出 '-'

        sub=`expr $thism_send + $thism_recv - $last_send - $last_recv`
        if [ $sub -ge 10 ]
        then symble='+'
        elif [ $sub -ge 0 ]
        then symble=''
        else
             symble='-'
        fi
        printf "%s  %s  %s  %s\n" $time $thism_recv $thism_send $symble

3 、完整代码清单

#!/bin/bash
curr_total_recv=''
curr_total_send=''
prev_total_recv='0'
prev_total_send='0'
thism_send='0'
thism_recv='0'
last_send='0'
last_recv='0'
sub=''
symbol=''
time=$(date "+%Y-%m-%d-%H:%M")
get_send_recv(){
eval 'recv_str=`netstat --statistics|grep "segments received"`'
eval 'send_str=`netstat --statistics|grep "segments sent out"`'
time=$(date "+%Y-%m-%d-%H:%M")
curr_total_recv=`expr "$recv_str" : '^ *\([0-9]*\) segments received'`
curr_total_send=`expr "$send_str" : '^ *\([0-9]*\) segments sent out'`
}
get_send_recv
echo wait a minute for get the status
printf "%s  %s  %s  %s\n" $time $thism_recv $thism_send $symble
while(sleep 60)
do
        prev_total_recv=$curr_total_recv
        prev_total_send=$curr_total_send
        last_send=$thism_send
        last_recv=$thism_recv
        get_send_recv
        thism_send=`expr $curr_total_send - $prev_total_send`
        thism_recv=`expr $curr_total_recv - $prev_total_recv`
        sub=`expr $thism_send + $thism_recv - $last_send - $last_recv`
        if [ $sub -ge 10 ]
        then symble='+'
        elif [ $sub -ge 0 ]
        then symble=''
        else
             symble='-'
        fi
        printf "%s  %s  %s  %s\n" $time $thism_recv $thism_send $symble
done

猜你喜欢

转载自www.cnblogs.com/dochengzz/p/12824715.html