Linux Network Tools - Netcat
Netcat (nc)
Sometimes it is useful to listen to a port a monitor what requests/data are being sent to it.
TCP
Open a client to port 7000 on localhost. Type something and hit enter to send a message. If the client is unable to make a connection then it will close.
$ nc localhost 7000
Hello from the client.Add the -l optiont to listen to port 7000 on
localhost.
$ nc -l 7000UDP
Use the -u option to send to UDP packets.
$ netcat -u localhost 7000Use the UDP and listen options together to create a simple UDP server.
$ netcat -lu 7000Port scan with Netcat
The -z option performs a scan. The -v
option give a verbose output.
netcat -zv localhost 80
Connection to 127.0.0.1 80 port [tcp/*] succeeded!We can also use a range of ports. Another useful option is
-n which tells nc to not resolve the IP
address using DNS. You have to use the IP address. This improves
performance.
$ netcat -zvn 127.0.0.1 1-1000nc sends messages to standard error. In order to pipe
the results to another program, we need to redirect standard error to
standard output via 2>&1. We can then filter the
results with grep.
$ netcat -zvn 127.0.0.1 1-1000 2>&1 | grep succeeded
Connection to 127.0.0.1 80 port [tcp/*] succeeded!Using files with Netcat
Put received messages into a file.
$ nc -l 7000 > output.txtSend file from client to server.
$ nc localhost 7000 < message.txtYou can even serve html files with netcat and access it with your browser.
$ nc -l 7000 < index.htmlTimeout
-w keep connection open for x seconds.
nc -w 10 localhost 7000-k flag forces it to stay open forever, even after the
client logs off.
nc -lk 7000IPV4 and IPV6
-4 IPV4 address
$ nc -4 -l 7000-6 IPV6 address
$ nc -6 -l 7000Proxying
mkfifo make first in, first out named pipe. Pipes are
generally unidirectional. This allows the proxy to send and receive data
from the target server. We can access www.mchaver.com:80
via localhost:7000.
$ mkfifo bidirectional
$ nc -l 7000 0<bidirectional | nc www.mchaver.com 80 1>bidirectionalAlternatives
Python
python -m SimpleHTTPServer 7000