0

I need to monitor the network activities of a process and the bandwidth of the connection. So I decided to use nethog and netperf combine with grep and awk to write the values to a file.

nethog command: nethogs -t eth0 | grep firefox | awk '{ print $3 }' (I want to take the upload/download speed). The 1st problem is that command doesn't print anything.

More detail: command: nethogs -t eth0 | grep firefox output:

usr/lib/firefox/firefox-bi�)/4956/1000  0.338867    0.239063
/usr/lib/firefox/firefox-bi�)/4956/1000 0.543555    0.274219
/usr/lib/firefox/firefox-bi�)/4956/1000 0.794531    0.489844
/usr/lib/firefox/firefox-bi�)/4956/1000 0.794531    0.489844
/usr/lib/firefox/firefox-bi�)/4956/1000 0.749023    0.589844
/usr/lib/firefox/firefox-bi�)/4956/1000 1.30098 3.47617
/usr/lib/firefox/firefox-bi�)/4956/1000 1.90449 8.0127
/usr/lib/firefox/firefox-bi�)/4956/1000 5.31641 25.0033
/usr/lib/firefox/firefox-bi�)/4956/1000 8.60762 42.0176

I want the two values at the bottom right. But as you can see the output ain't consistent (the space), add | awk '{print $3}' most of the time return nothing. And I can't fix this.

netperf command: netperf -H 192.168.1.10 and the output is:

Recv   Send    Send                          
Socket Socket  Message  Elapsed              
Size   Size    Size     Time     Throughput  
bytes  bytes   bytes    secs.    10^6bits/sec  

524288 524288 262144    10.00    **718.62**

I want to print the value on the bottom right but have no idea how to. That's the 2nd problem => solved by fedorqui!

Can you guys help me out? Any ideas will be appreciated!

  • And what about checking the answer you were given? – fedorqui Mar 7 '14 at 15:44
  • Your solution works, but the first one still left... – Tiana987642 Mar 7 '14 at 15:51
  • Do you get any output of nethogs alone? And nethogs -t eth0? – fedorqui Mar 7 '14 at 15:53
  • Yes, yes I do. But it ain't consistent (the space between columns) so I guess that the reason... I have no idea how to fix it :( – Tiana987642 Mar 7 '14 at 15:56
  • 1
    You never need grep+awk since awk can do anything that grep can do. You need to show some sample output of nethogs -t eth0 if you want a script that can parse that output as it's input along with the desired output of the tool you want to write. – Ed Morton Mar 7 '14 at 17:15
1

For the 2nd problem, you can do:

your_command | tail -1 | awk '{print $NF}'

This will print the last field of the last line of the given input.

| improve this answer | |
  • Since Ed Morton help me the first problem, I'm finally over these things! Thank you for helping me :) – Tiana987642 Mar 7 '14 at 17:56
2

Let's start with this:

nethogs -t eth0 | awk '/firefox/{ print $3 }'

netperf -H 192.168.1.10 | awk 'END{ print $NF }'

If those don't work, provide sample input to the awk scripts, expected output, and explanations.

You would need:

netperf -H 192.168.1.10 | awk '{v=$NF} END{ print v }'

with some awks since they're not required by POSIX to retain fields in the END section.

| improve this answer | |
  • Thank you !nethogs command works! But netperf return found? BTW, can you please explain my command use grep and awk most of the time doesn't return anything? – Tiana987642 Mar 7 '14 at 17:53
  • fedorqui suggests me use netperf -H 192.168.1.10 | tail -1 | awk '{print $NF}' and I find this solution works! – Tiana987642 Mar 7 '14 at 17:57
  • If the netperf | awk command prints found then found is the last field on the last line of the netperf output as you said you wanted printed. Having said that, the netperf | tail -1 | awk solution will print exactly the same thing so if you're seeing a a difference in output between the 2 then you are not running the same netperf command or you have spurious control chars in it's output or there's something else you aren't telling us. Finally - yes, there are MANY ways to print the last field of the last line of input and you can employ as many tools and pipes as you like to do so. – Ed Morton Mar 7 '14 at 18:21
  • Hi, maybe you read my question too fast :) I want netperf return the number at the bottom right (see the 2nd problem in my question). So the output should be 718.62 not found. – Tiana987642 Mar 8 '14 at 16:16
  • 1
    No, I didn't read your question too fast. awk does not make up spurious words and print them. The awk command is printing the word found because apparently the word found IS the text at the bottom right of your netperf output, not 718.62 as you think it should be. So far you haven't shown us any output from your nethog command so it's impossible to guess what problem you might be having parsing it. – Ed Morton Mar 8 '14 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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