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

因为工作需要,要统计一下服务器的网络吞吐量,并且出一个图表。在搜索网络吞吐量软件时候,看到这篇文章,于是决定用脚本来实现这个功能。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 
复制代码

posted on   老翅寒暑  阅读(2012)  评论(3编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了

导航

< 2009年6月 >
31 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 1 2 3 4
5 6 7 8 9 10 11
点击右上角即可分享
微信分享提示