一個統計服務器網絡流量的小程序
因為工作需要,要統計一下服務器的網絡吞吐量,并且出一個圖表。在搜索網絡吞吐量軟件時候,看到這篇文章,于是決定用腳本來實現這個功能。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

公眾號:老翅寒暑
浙公網安備 33010602011771號