因为工作需要,要统计一下服务器的网络吞吐量,并且出一个图表。在搜索网络吞吐量软件时候,看到这篇文章,于是决定用脚本来实现这个功能。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 /q /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 /f "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 /f "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 //b //nologo sleep.js
50 goto begin
51 :mainexit
52 if exist %stopfile% del /q /f %stopfile%
53 if exist sleep.js del /q /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
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 /q /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 /f "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 /f "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 //b //nologo sleep.js
50 goto begin
51 :mainexit
52 if exist %stopfile% del /q /f %stopfile%
53 if exist sleep.js del /q /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