Pages

05 August 2015

Use netcat to test ports comunications

Recently I had to test that the ports among many servers were open and that connection succeeded.
I didn't have any service to install only had to test the comunication.

A list of ports were given to me (more than the 5 shown below).

I wrote a simple bash script to run on the servers. 
I used a bash for to start all the ports with nc:

for PORT in 80 443 8443 8000 8443 2303
do
   sudo nc -l $PORT > $PORT.log &
done

So now we have a bunch of LISTEN sockets:

netstat -an

Active Internet connections (including servers)
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)   
tcp4       0      0  *.2303                 *.*                    LISTEN   
tcp4       0      0  *.8000                 *.*                    LISTEN    
tcp4       0      0  *.8443                 *.*                    LISTEN    
tcp4       0      0  *.8443                 *.*                    LISTEN    
tcp4       0      0  *.443                  *.*                    LISTEN    
tcp4       0      0  *.80                   *.*                    LISTEN    
[...]


Now we can test the connectivity with netcat from another machine.

I wrote another script to do this that uses the first parameter given as target host:

for PORT in 80 443 8443 8000 8443 2303
do
   export RESULT=`nc -G 1 -v $1 $PORT`
   echo $1":"$PORT " RESULT: "$RESULT >> $1.log
done


This script outputs some lines like these:

10.10.200.10:80  RESULT: found 0 associations found 1 connections: 1: flags=82 outif en3 src 10.17.180.81 port 52879 dst 10.10.200.10 port 80 rank info not available TCP aux info available Connection to 10.10.200.10 port 80 [tcp/http] succeeded!

10.10.200.10:443  RESULT: found 0 associations found 1 connections: 1: flags=82 outif en3 src 10.17.180.81 port 52880 dst 10.10.200.10 port 443 rank info not available TCP aux info available Connection to 10.10.200.10 port 443 [tcp/https] succeeded!

So If you found an output in the [IP_ADDRESS].log after:

[IP_ADDRESS]:[PORT] RESULT: yadda yadda yadda


Means the connection works.

The scripts are really raw, but are a good starting point.