2

How can i print the output of vnstat -l to a file? what i've tried:

vnstat -l &> file

but it just hangsup

2 Answers 2

5

Remove the l option. From the vnstat man page:

   -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.

So, the -l makes vnstat display output in "live mode", constantly updating, that's why you can't capture the output. If you want an easily parseable format use

vnstat --dumpdb

To just save the standard output do

vnstat > log.txt

If what you want is the current upload and download rate, vnstat is not the right tool for the job. Try sar from the sysstat package (see man sar for more info):

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

To extract the current* download and upload rate for the interface wlan0 from sar's output do:

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

* What does "current" mean? sar (and any other method) will take some milliseconds to generate output.

4
  • but i actually need the total current network activity. this will print stats of today, yesterday, this month etc
    – teslasimus
    Mar 21, 2013 at 14:14
  • What do you mean? What is "current" network activity? You mean the upload/download rate?
    – terdon
    Mar 21, 2013 at 14:17
  • yes. upload/download...
    – teslasimus
    Mar 21, 2013 at 14:33
  • See updated answer, vnstat is not the right tool for the job.
    – terdon
    Mar 21, 2013 at 14:36
0

I don't know if you want to print just final summary to file from some period you are going to monitor network activity, but you can cheat it this way:

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

The only issue is that vnstat will write output to file every second so it is not the most convenient way. For continuous monitoring with log for every second or so terdon answered your question.

2
  • Ok, but be aware of the problem that vnstat overwrites the status line again and again using backspaces so in the output you will se only the last status. You can replace the sequences of backspaces and expand the statuses to individual lines using for example: sed -re $'s/\b[\b ]+\b/\\n/g' dump.txt Nov 20, 2014 at 14:53
  • Tnak's for a good trick ;) Nov 22, 2014 at 13:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .