TCP/IP connection testing with nc (netcat) 

Joined:
04/09/2007
Posts:
776

March 28, 2011 15:54:20    Last update: March 28, 2011 15:54:20
From the nc man page:

The nc (or netcat) utility is used for just about anything under the sun involving TCP or UDP. It can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP ports, do port scanning, and deal with both IPv4 and IPv6.

  1. To bind to port 8080:
    nc -l 8080
    

  2. To connect to the port above
    nc localhost 8080
    

    Type something in either window it will be echoed in the other.
  3. To get the home page of google:
    echo -e -n "GET / HTTP/1.1\r\nHost:www.google.com\r\n\r\n" | nc www.google.com 80
    

  4. To send email via SMTP:
    nc -C localhost 25 << EOF
    HELO host.example.com
    MAIL FROM: <user@host.example.com>
    RCPT TO: <user2@host.example.com>
    DATA
    Enter body of email here. End with a dot . in a line of itself.
    .
    QUIT
    EOF
    

  5. Port scan of localhost:
    $ nc -z localhost 20-8080
    Connection to localhost 22 port [tcp/ssh] succeeded!
    Connection to localhost 25 port [tcp/smtp] succeeded!
    Connection to localhost 631 port [tcp/ipp] succeeded!
    Connection to localhost 3306 port [tcp/mysql] succeeded!
    Connection to localhost 5037 port [tcp/*] succeeded!
    Connection to localhost 5432 port [tcp/postgres] succeeded!
    

Share |
| Comment  | Tags