vnstat:将当前网络活动打印到文件

tes*_*mus 2 networking linux bash ubuntu

如何将输出打印vnstat -l到文件?我试过的:

vnstat -l &> file
Run Code Online (Sandbox Code Playgroud)

但它只是挂断了

ter*_*don 5

删除该l选项。从vnstat手册页:

   -l, --live mode
          Display current transfer rate for the selected interface in real time until interrupted. Statis?
          tics  will be shown after interruption if the runtime was more than 10 seconds. An optional mode
          parameter can be used to select between the displaying of packets per second (mode 0) and trans?
          fer  counters  (mode  1) during execution.  --style can also be used to affect the layout of the
          output.
Run Code Online (Sandbox Code Playgroud)

因此,-l使得vnstat在“直播模式”显示输出,不断地更新,这就是为什么你不能捕获输出。如果您想要易于解析的格式,请使用

vnstat --dumpdb
Run Code Online (Sandbox Code Playgroud)

只保存标准输出做

vnstat > log.txt
Run Code Online (Sandbox Code Playgroud)

如果您想要的是当前的上传和下载速率,vnstat则不是适合该工作的工具。sarsysstat包中尝试(sar有关更多信息,请参见 man ):

sudo apt-get install sysstat 
sudo sar -n DEV 1 1
Run Code Online (Sandbox Code Playgroud)

要提取该接口的电流*下载和上传速率wlan0sar的输出做:

sudo sar -n DEV 1 1 | grep wlan0 | tail -n 1 | gawk '{print "Down: "$5,"Up: "$6}'
Run Code Online (Sandbox Code Playgroud)

* “当前”是什么意思?sar(和任何其他方法)将需要几毫秒来生成输出。