|Last modified: 16-06-2020|
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.
nc smtp.acme.com 25
> 220 smtp.acme.com ESMTP Postfix
> 221 Bye
echo quit | nc smtp.acme.com 25
To scan a range of ports on a server: nc -z -r -v www.acme.com 70-80
On the server:
cat /etc/passwd | nc -l -p 1234 (or
nc -l -p 1234 <
On the client:
nc remotehost.acme.com 1234 > passwd.txt, followed
by CTRL-C to end the connection.
nc -v www.website.com 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.
On the server: nc -L -p 1234 < c:\sometext.txt
On the client, point your browser to http://www.acme.com:1234