0

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)

abhiomkar
  • 1,713
  • 1
  • 11
  • 7
  • 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 03:42

1 Answers1

3

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

Oneiroi
  • 2,063
  • 1
  • 15
  • 28
  • 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