iftop - how to generate text file with its output?

Solution 1:

Since iftop-1.0pre3 (2014-01-01), a text output mode was added. This will be very useful to anyone trying to parse the output of iftop.

The command line option to activate text (batch) mode is:

-t          use text interface without ncurses

When using text mode, the following options are available:

-s num      print one single text output afer num seconds, then quit
-L num      number of lines to print

Use the -h option for help on iftop usage.

Example usage would be:

iftop -t > log.txt
iftop -t -s 180 > log.txt

If you want it to run in the background for 5 hours:

iftop -t -s 18000 > log.txt &

Check on background job with job command.

Solution 2:

iptraf can log this level of detail. The output looks something like this:

Wed Apr 25 23:08:42 2012; UDP; eth0; 65 bytes; from 192.168.1.20:45061 to 192.168.1.254:53
Wed Apr 25 23:08:42 2012; UDP; eth0; 133 bytes; from 192.168.1.254:53 to 192.168.1.20:45061
Wed Apr 25 23:08:43 2012; UDP; eth0; 96 bytes; from 192.168.1.21:137 to 192.168.1.20:137
Wed Apr 25 23:08:44 2012; UDP; eth0; 472 bytes; from 192.168.1.21:1900 to 239.255.255.250:1900
Wed Apr 25 23:08:47 2012; ICMP; eth0; 159 bytes; from 192.168.1.20 to 173.176.222.82; dest unrch (port)