Command line: how to get instantaneous bandwidth on a port

2

I'm trying to get the instantaneous bandwidth usage of eth0, port 10001. I've looked into using lots of different tools, but most of these seem to have an ncurses type output which isn't suitable for piping into my program.

I've seen How can I get an interface's bandwidth at any given second from the Linux command line?, but this only gives the total statistics over an interface and isn't broken down by port.

Ideally, I'd like a command that takes two arguments (eth0 and port 10001), runs for 500ms, calculates the number and size of packets and outputs a simple megabytes/second metric.

Is this too much to ask?

Anyone got any ideas?

Eamorr

Posted 2012-02-29T07:54:51.933

Reputation: 227

1use tcpdump. feed the file into something that can give you a rate, i think ratesniff or tcptrace can do that – Sirex – 2012-02-29T08:45:00.933

Actually, I found the answer here: http://superuser.com/questions/395226/command-line-how-to-get-instantaneous-bandwidth-on-a-port Works perfectly...

– Eamorr – 2012-02-29T08:54:27.787

You linked your own question here ;p – Journeyman Geek – 2012-02-29T10:38:02.277

sorry... Here's the correct link: http://superuser.com/questions/356907/how-to-get-real-time-network-statistics-in-linux-with-kb-mb-bytes-format-and-for Many thanks for your help,

– Eamorr – 2012-02-29T16:36:49.247

Answers

2

You could try to use the ifstat command. With default invocation, it shows bandwidth usage periodically until you hit Control-C But you can invoke it like this:

ifstat <delay> <count>  (i.e. ifstat 1 1)

So that it will give you the bandwidth usage in one second and it's scriptable.

user842313

Posted 2012-02-29T07:54:51.933

Reputation: 159

So how to specify port with ifstat? – holms – 2014-12-10T16:26:57.667

1@holms it's impossible to specify a port with ifstat, you'll need something else. You may give nethogs a try. – Neurotransmitter – 2015-04-21T14:18:46.063