Using NetCat


NetCat (Windows version here) "is the TCP/IP swiss army knife available since 1996. Netcat is a simple Unix utility which reads and writes data across network connections, using TCP or UDP protocol. It is designed to be a reliable "back-end" tool that can be used directly or easily driven by other programs and scripts."


While the -l (lowercase L) tells nc to listen to a port, -L (capital L) will restart Netcat with the same command line when the connection is terminated.  This way you can connect over and over to the same Netcat process, although its behavior may be a bit different (eg. if the server is waiting to send a file to any client that connects, only the first connection will actually send the file.)

If you don't want to see a DOS box remain open while running a server, add the -d switch to tell nc to detach itself, and then close the DOS window.

To work with UDP, use the -u switch.

To keep a copy of the whole conversation between server and client in a hex dump file, use the -o switch, eg. -o mylogfile.bin

If you specify a target host and optional port in listen mode, netcat will accept an inbound connection only from that host and if you specify one, only from that foreign source port.

As a replacement to Telnet

nc 25

> 220 ESMTP Postfix


> 221 Bye

To check ports

echo quit | nc 25

To scan a range of ports on a server: nc -z -r -v 70-80

To open a port on a server, and send a file's content to a client:

On the server: cat /etc/passwd | nc -l -p 1234 (or nc -l -p 1234 < /etc/passwd)

On the client: nc 1234 > passwd.txt, followed by CTRL-C to end the connection.

To retrieve the home page of a web site:

nc -v 80 < get.txt

where get.txt contains GET / HTTP/1.0 followed by a CRLF since this is what a web server expects to send a web page to a browser.

Poor man's web server

On the server: nc -L -p 1234 < c:\sometext.txt

On the client, point your browser to