Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to store the network data received and data sent information on a variable or in a file.

Using the below command I can grep it and print the line on screen. But I'm not able to store the info in a bash variable.

top -l 0 -n 1 -c d | grep -e '^Networks:'

Is there any better approach?

(on Mac OS X)

share|improve this question
    
your question is a little ambiguous, are you wanting the actual packet data or a summary of the number of bytes sent/received? –  Oneiroi Jun 24 '12 at 21:24
    
@Oneiroi sorry that I didn't mention, I wanted the summary of the number of bytes sent/received. Thanks –  abhiomkar Jun 25 '12 at 3:42

1 Answer 1

up vote 2 down vote accepted

This should work for you:

netstat -bi | grep -v Ibytes | awk '{ x += $7 } END { print x }' <- This will give you a total "IN" byte count

netstat -bi | grep -v Obytes | awk '{ x += $10 } END { print x }' <- This will give you a total "OUT" byte count

share|improve this answer
    
Thanks :) that worked! I'm using netstat -bi | grep -v Ibytes | grep 'en1' | head -n 1 | awk '{ x = $7 / (1024 * 1024 * 1024) } END { print x }' to get the data received in GB. But, the number is not matching with the one that I'm seeing at System Activity Monitor Network tab (on Mac). –  abhiomkar Jun 26 '12 at 13:08
1  
I figured it out: here is the one liner to get the Total Data Recieved: echo "`netstat -bi | grep -v Ibytes | grep -v "-" | grep "^en" | awk '{ x += $7 } END { print x / (1024 * 1024 * 1024) }'` + `netstat -bi | grep -v Ibytes | grep -v "-" | grep "^lo" | awk '{ x += $6 } END { print x / (1024 * 1024 * 1024) }'`" | bc –  abhiomkar Jun 26 '12 at 13:31
    
@abhiomkar great! and thanks for posting a follow up :) –  Oneiroi Jun 26 '12 at 14:44

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.