5

I would like to use the terminal to monitor the network traffic on a given interface, with a view to providing alerts when certain cumulative totals have been exceeded.

The script needs to gain statistics from the point in which it is started, and there is no requirement to store values for future reference, or retain values over reboots etc. I would like the output to be human readable, so values in Gb or Mb, not packets or bytes.

The intention is to be able to include this script in an Automator application that will run in the background, and open a dialogue box at set thresholds to alert me to when I nearing a download limit, allowing to choose to disconnect the modem etc.

I do not wish to download any tools or applications, and just wish to use the tools a standard installation provides for the terminal. Getting data out of an application via applescript would be acceptable, but only if that application is also a standard tool, like activity monitor.

1
  • 1
    I got as far as netstat -i -I en0 -b which gives you inbound bytes (along with other stats) for en0 (replace your interface here). You'd obviously then have to use other UNIX tools to parse the output, convert to MB/GB etc. but it might be a start if you've not already done better! I'm suprised someone out there hasn't already created a script of this type as it sounds like a useful thing to have!
    – binarybob
    Commented Feb 23, 2012 at 11:21

3 Answers 3

4

Sounds like nettop is what you're looking for. Start it with the help parameter to see the options.

nettop -nc -m route seems to be the best overall traffic gauge but will need additional scripting to parse the output for you

3
  • Not at a mac, so can't test, but if it's anything like 'top', isn't that going to produce a constantly updating screen that I need to quit out of? How would I pull a figure out of the output, perform a calculation on it, then re fetch the figure and repeat etc? Aware of the tool vaguely, but not sure it's a good fit for scripting. Comments welcome tho - thanks.
    – stuffe
    Commented Feb 29, 2012 at 9:42
  • You're right, interactive output will not help for what you need, can't think of the right utility at the moment Commented Mar 1, 2012 at 10:01
  • Having just tried this myself, this command only monitors bytes transferred through the default gateway... traffic to/from LAN (or more specifically, subnet-local) hosts is not displayed.
    – Jeremy
    Commented Feb 16, 2014 at 16:12
2
_delta_t=20
_interface=en1
netstat -i -I ${_interface} -b -w ${_delta_t} |
    awk 'BEGIN {
        printf ("instantaneous (byte/s)\tcumulated (Mbytes)\nin\tout\t\tin\tout\n")
    }{
        if ($0 !~ /(input|packets)/) {
            # get input & output bytes
            inb=$3 ; oub=$6 ;
            # accumulate them
            cuminb+=$3 ; cumoub+=$6 ;
            printf("%8d%8d\t", inb/'${_delta_t}', oub/'${_delta_t}')
            printf("%8.3f%8.3f\n", cuminb/1000000, cumoub/1000000)
        }
    }'
1
  • ® binarybob ☺ for the core engine.
    – athena
    Commented Aug 15, 2013 at 17:35
-2

a useful command is sudo tcpdump -i en0 - v you will have to supply your system password -

You must log in to answer this question.

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