1

I want to monitor the number of incoming and outgoing network connections. I can get the total number with netstat and wc -l, but I want to know how many are inbound and how many are outbound.

  • what do you mean by inbound/outbound? please be specific. – Farhan Nov 24 '11 at 17:31
  • 1
    You mean netstat instead of netcat? – quanta Nov 25 '11 at 7:07
3

You can accomplish that by using ip_conntrack.

modprobe ip_conntrack
cat /proc/net/ip_conntrack

The established connections that have the source address equal to your interface's address are the outgoing connections.

The established connections that have the destination address equal to your interface's address, are the incoming connections.

# Number of outgoing connections
cat /proc/net/ip_conntrack | grep "ESTABLISHED" | grep "src=xxx.xxx.xxx.xxx" | wc -l

# Number of incoming connections
cat /proc/net/ip_conntrack | grep "ESTABLISHED" | grep "dst=xxx.xxx.xxx.xxx" | wc -l

(xxx.xxx.xxx.xxx is obviously your interface's ip address)

Keep in mind that there are connections that occur for inter-process communication, between applications running on your system.

You can find information about ip_conntrack entries here.

1

You can monitor all the incoming and outgoing connections with iftop.

http://www.ex-parrot.com/pdw/iftop/

If you want to get some statistics

http://www.ntop.org/products/ntop/

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.