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


Answers:


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.

因此,-lmake vnstat在“实时模式”下显示输出,并不断更新,这就是为什么您无法捕获输出的原因。如果您想要一种易于解析的格式,请使用

vnstat --dumpdb

只保存标准输出

vnstat > log.txt

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

sudo apt-get install sysstat 
sudo sar -n DEV 1 1

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

sudo sar -n DEV 1 1 | grep wlan0 | tail -n 1 | gawk '{print "Down: "$5,"Up: "$6}'

*“当前”是什么意思?sar(以及其他任何方法)将花费几毫秒的时间来生成输出。


但是我实际上需要当前的总网络活动。这将打印今天,昨天,本月等的统计信息
teslasimus

你什么意思?什么是“当前”网络活动?您的意思是上传/下载速率
terdon

是。上传/下载...
teslasimus

请参阅更新的答案,vnstat不是适合此工作的工具。
terdon

0

我不知道您是否要在一段时间内仅将最终摘要打印到文件中,以监视网络活动,但是您可以这样作弊:

vnstat -l -i <device> >> dump.txt

唯一的问题是vnstat每秒都会将输出写入文件,因此这不是最方便的方法。对于每秒进行一次连续日志监视,terdon回答了您的问题。


好的,但是要注意会vnstat使用退格键一次又一次地覆盖状态行的问题,因此在输出中,您只会看到最后一个状态。您可以使用以下命令替换退格序列并将状态扩展为单独的行:sed -re $'s/\b[\b ]+\b/\\n/g' dump.txt
pabouk 2014年

Tnak是个好把戏;)
Michal Gonda
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.