一个统计服务器网络流量的小程序

因为工作需要,要统计一下服务器的网络吞吐量,并且出一个图表。在搜索网络吞吐量软件时候,看到这篇文章,于是决定用脚本来实现这个功能。bigtall在原文的启发下,做了一些修改,去掉了对unix工具的依赖,把它变成了一个纯的批处理文件。希望能给有需要的人帮助。

 1  @echo   off
 2  :args
 3  rem  处理参数
 4  if   " %1 "   ==   ""   goto  help
 5 
 6  set  stopfile = " %temp%\netflow.stop "
 7  if   " %1 "   ==   " stop "   goto  stop
 8  if   " %1 "   ==   " start "   goto   start
 9  goto  help
10 
11  :start
12  rem  继续分析start参数
13  set  outfile =
14  set  outcon = 1
15  if   " %2 "   ==   ""   goto  main
16  set  outcon =
17  if   " %2 "   ==   " console "   set  outcon = 1
18  if   not   " %2 "   ==   " console "   set  outfile = %2
19  if   " %3 "   ==   " console "   set  outcon = 1
20  if   " %outfile% "   ==   ""   if   not   " %3 "   ==   " console "   set  outfile = %3
21 
22  :main
23  echo  本程序每5秒统计一下网卡的流量 ,  ctrl + c退出
24 
25  rem  删除stop记录
26  if   exist  %stopfile%  del   / / f %stopfile%
27  rem  首次流量记录,初始化
28  echo  WScript . Sleep ( 5000 );   >  sleep . js
29  set  curdate1 = % date %
30  set  curdate = %curdate1:~ 0 , 10 %
31  set  curdate1 =
32 
33  if   " %outfile% "   ==   ""   goto  label2
34  if   not   exist  %outfile%  echo  日期 , 时间 , 接收总字节 , 发送总字节 , 本次接收字节 , 本次发送字节 >> %outfile%
35  :label2
36  rem  其中字段内容为:日期,时间,接受总字节,发送总字节,本次接受字节,本次发送字节
37  for   / " tokens=1,2,3 "  %%i in  ( 'netstat -e ^| findstr 字节' )   do   set  prevrecv = %%j&& set  prevsend = %%k
38  :begin
39  if   exist  %stopfile%  goto  mainexit
40  for   / " tokens=1,2,3 "  %%i in  ( 'netstat -e ^| findstr 字节' )   do   set  recv = %%j&& set  send = %%k
41  set   / a nrecv = %recv:~-9% - %prevrecv :~-9 %
42  set   / a nsend = %send :~-9 % - %prevsend :~-9 %
43  set  prevrecv = %recv%
44  set  prevsend = %send%
45  if   " %outfile% "   ==   ""   goto  label3
46  echo  %curdate% , % time % , %recv% , %send% , %nrecv% , %nsend%  >>  %outfile%
47  :label3
48  if   " %outcon% "   ==   " 1 "   echo  %curdate% , % time % , %recv% , %send% , %nrecv% , %nsend%
49  cscript  // // nologo sleep . js
50  goto  begin
51  :mainexit
52  if   exist  %stopfile%  del   / / f %stopfile%
53  if   exist  sleep . js  del   / / f sleep . js
54  goto   end
55 
56  :stop
57  echo  通知发送完成
58  echo   .   >  %stopfile%
59  goto   end
60 
61  :help
62  echo  网络流量统计
63  echo  usage: netflow [ start ^|stop] [console] [logfile]
64  echo          start    开始运行,并输出到指定文件
65  echo         stop    停止统计
66  echo         console 运行输出时,输出一份到控制台
67  echo         logfile 运行输出时,输出一份到指定文件
68  goto   end
69 
70  :end
71 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值