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.
Code:
#!/bin/bash
handle_req()
{
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
1. HTTP Client
This example will fetch the latest kernel version from kernel.org (similar to finger(1) ). Note that netcat prints also the HTTP header.
Code:
echo -e "GET /kdist/finger_banner HTTP/1.0\r\n" | \
nc www.kernel.org 80 | grep latest
2. Remote Terminal
This example shows how to connect to a remote shell without using telnet or ssh. The terminal server which runs on <host>:
Code:
nc -l -p 4000 -e /bin/sh
and the client :
3. Port Scanning
netcat can also be used for port scanning (with zero-I/O option).
Code:
nc -v -z www.kernel.org 80 21
# or with port ranges
nc -v -z www.kernel.org 21-23
4. File Transfer
Sending a file "foobar" on port 4000 can be achieved like this:
Code:
cat foobar | nc -l -p 4000
The client would receive the file from <host> with this command:
Code:
nc <host> 4000 > foobar
5. Torifying Netcat
The tor package includes torify, a wrapper for tsocks and tor, which can be used to anonymize network traffic :
Code:
echo -e "GET /iponly/ HTTP/1.0\r\n" | torify nc ipid.shat.net 80
This should print the IP address of the Tor exit-node that is used.
6. Chat
A simple one-to-one chat server can be started like this:
Afterwards a client can connect to the server:
Bookmarks