This article describes some netcat applications and demonstrates how to use netcat which is one of the most useful network tools.
0. HTTP Server
This is a simple HTTP server which listens on port 8080. The -l option puts netcat into listen mode and -c specifies the command to execute after a connection is established. The server respond can be viewed with a browser at http://localhost:8080.
1. HTTP Client
read req file proto
echo -e "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"
echo -e "<html><h1>Hello World</h1></html>"
typeset -fx handle_req
nc -l -p 8080 -c handle_req
This example will fetch the latest kernel version from kernel.org (similar to finger(1) ). Note that netcat prints also the HTTP header.
2. Remote Terminal
echo -e "GET /kdist/finger_banner HTTP/1.0\r\n" | \
nc www.kernel.org 80 | grep latest
This example shows how to connect to a remote shell without using telnet or ssh. The terminal server which runs on <host>:
and the client :
nc -l -p 4000 -e /bin/sh
3. Port Scanning
netcat can also be used for port scanning (with zero-I/O option).
4. File Transfer
nc -v -z www.kernel.org 80 21
# or with port ranges
nc -v -z www.kernel.org 21-23
Sending a file "foobar" on port 4000 can be achieved like this:
The client would receive the file from <host> with this command:
cat foobar | nc -l -p 4000
5. Torifying Netcat
nc <host> 4000 > foobar
The tor package includes torify, a wrapper for tsocks and tor, which can be used to anonymize network traffic :
This should print the IP address of the Tor exit-node that is used.
echo -e "GET /iponly/ HTTP/1.0\r\n" | torify nc ipid.shat.net 80
A simple one-to-one chat server can be started like this:
Afterwards a client can connect to the server: