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 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.

share|improve this question
    
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
add comment

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
add comment

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/

share|improve this answer
add comment

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.