LLI Wiki — My Digital Knowledge Base

Using ncat (netcat): examples

Examples of ncat and nc usage: listening, connecting, chat, relay, file copy, UDP and timeouts.

ncat (shipped with Nmap) and nc can listen on a port, connect to TCP or UDP services, test connectivity and transfer data. This article summarises the common uses described in Netcat, TCP/IP's Swiss Army knife and nc / ncat command examples.

nc or ncat?

Several implementations exist (traditional netcat, OpenBSD netcat, Nmap's ncat). Their options differ slightly; the examples below use ncat.

Listen on a port

ncat -l 8080

Connect to a remote service

ncat 192.0.2.10 80

Once connected, you can type an HTTP request by hand:

GET / HTTP/1.1
Host: 192.0.2.10

HEAD / HTTP/1.1 returns only the headers, including the Server header that identifies the web server software.

Simple chat between two machines

On the server:

ncat -l 8080

On the client:

ncat 192.0.2.10 8080

Messages typed on one side appear on the other.

UDP connections

ncat -l -u 1234
ncat -v -u 192.0.2.11 53

Check that a UDP port is listening:

netstat -tunlp | grep 1234

Simple proxy (relay)

ncat -l 8080 | ncat 192.0.2.12 80

This setup only forwards in one direction. For a relay in both directions, use a named pipe:

mkfifo 2way
ncat -l 8080 0<2way | ncat 192.0.2.12 80 1>2way

Copy a file

On the receiving machine:

ncat -l 8080 > file.txt

On the sending machine:

ncat 192.0.2.10 8080 --send-only < data.txt

The --send-only option closes the connection once the data is sent; without it, press Ctrl+C to stop.

Timeout and persistent listening

ncat -w 10 192.0.2.10 8080
ncat -l -k 8080

Notes

Mastodon LLIMastodon SocialFosstodon