How do I use Netstat to see connections

How do I use Netstat to see connections

Netstat has some very useful command line switches that help show the connections into a server.  If you are seeing many TCP TIME_WAITs that are not going away then that tends to indicate an issue, many times a box low on resources tends to build up TIME_WAITs because the machine does not have enough resources to handle the incoming connections along with clean up old connections.

Some examples.  Please note that there are slight differences between the output on linux and windows.


netstat -a  (this list can be very long on busy machines, this is just a small snip it)

Linux:
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 localhost:smtp          *:*                     LISTEN      
tcp        0      0 *:microsoft-ds          *:*                     LISTEN      
tcp        0      0 *:netbios-ssn           *:*                     LISTEN      
tcp        0      0 *:ssh                   *:*                     LISTEN      
tcp        0      0 localhost:ipp           *:*                     LISTEN      
tcp        0    224 webnet3:ssh             172.16.5.69:44122       ESTABLISHED 

Windows:
Active Connections

  Proto  Local Address          Foreign Address        State
  TCP    0.0.0.0:135            webnet4:0              LISTENING
  TCP    0.0.0.0:445            webnet4:0              LISTENING
  TCP    0.0.0.0:1099           webnet4:0              LISTENING
  TCP    0.0.0.0:1494           webnet4:0              LISTENING
  TCP    0.0.0.0:3306           webnet4:0              LISTENING
  TCP    0.0.0.0:3389           webnet4:0              LISTENING
  TCP    0.0.0.0:3390           webnet4:0              LISTENING
  TCP    0.0.0.0:4500           webnet4:0              LISTENING
  TCP    0.0.0.0:8090           webnet4:0              LISTENING


To count all connections that are connected to the machine.
netstat -an | wc -l   (linux only command)

To count all connections that are on port 80
netstat -an | grep :80 | wc -l  (linux only command)


A nice feature do to the extra utilities on linux can create a summary of connections.
netstat -ant | awk '{print $6}' | sort | uniq -c | sort -n
      1 Foreign
      1 TIME_WAIT
      1 established)
      3 ESTABLISHED
     22 LISTEN

To show all listening ports
netstat -l

To get a list of statistics 
netstat -s

To show the process ID of the connection.
netstat -p   (linux)
netstat -b  (windows)



Additional URLs 
https://www.exchangecore.com/blog/find-number-active-connections-linux-using-netstat/
http://www.binarytides.com/linux-netstat-command-examples/