Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.

share|improve this question
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 Feb 23 '12 at 11:21
add comment

2 Answers

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

share|improve this answer
    
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 Feb 29 '12 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 –  demianturner Mar 1 '12 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 Feb 16 at 16:12
add comment
_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)
        }
    }'
share|improve this answer
    
® binarybob ☺ for the core engine. –  daniel Azuelos Aug 15 '13 at 17:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.