knowledge/technology/applications/cli/network/netcat.md

56 lines
3.4 KiB
Markdown
Raw Permalink Normal View History

2023-12-04 10:02:23 +00:00
---
obj: application
wiki: https://en.wikipedia.org/wiki/Netcat
---
# netcat
2024-01-17 08:44:04 +00:00
The `nc` (or `netcat`) utility is used for just about anything under the sun involving [TCP](../../../internet/TCP.md), [UDP](../../../internet/UDP.md), or UNIX-domain sockets. It can open [TCP](../../../internet/TCP.md) connections, send [UDP](../../../internet/UDP.md) packets, listen on arbitrary [TCP](../../../internet/TCP.md) and [UDP](../../../internet/UDP.md) ports, do port scanning, and deal with both IPv4 and IPv6.
2023-12-04 10:02:23 +00:00
Common uses include:
2024-01-17 08:00:45 +00:00
- simple [TCP](../../../internet/TCP.md) proxies
- shell-script based [HTTP](../../../internet/HTTP.md) clients and servers
2023-12-04 10:02:23 +00:00
- network daemon testing
2024-01-17 08:00:45 +00:00
- a SOCKS or [HTTP](../../../internet/HTTP.md) ProxyCommand for [ssh](../../network/SSH.md)
2023-12-04 10:02:23 +00:00
## Options
| Option | Description |
| ------------------ | --------------------------------------------------------------------------------------------------- |
| `-4` | Use IPv4 addresses only |
| `-6` | Use IPv6 addresses only |
| `-b` | Allow broadcast |
| `-l` | Listen for an incoming connection rather than initiating a connection to a remote host |
2024-01-17 08:44:04 +00:00
| `-N` | shutdown the network socket after EOF on the input. Some servers require this to finish their work |
| `-p <source_port>` | Specify the source port `nc` should use, subject to privilege restrictions and availability |
2023-12-04 10:02:23 +00:00
## Examples
### Client/Server Model
2024-01-17 08:44:04 +00:00
On one console, start `nc` listening on a specific port for a connection. For example:
2023-12-04 10:02:23 +00:00
```shell
nc -l 1234
```
2024-01-17 08:44:04 +00:00
`nc` is now listening on port 1234 for a connection. On a second console (or a second machine), connect to the machine and port being listened on:
2023-12-04 10:02:23 +00:00
```shell
nc -N 127.0.0.1 1234
```
2024-01-17 08:44:04 +00:00
There should now be a connection between the ports. Anything typed at the second console will be concatenated to the first, and vice-versa. After the connection has been set up, `nc` does not really care which side is being used as a server and which side is being used as a client. The connection may be terminated using an `EOF` (`^D`), as the `-N` flag was given.
2023-12-04 10:02:23 +00:00
### Data Transfer
The example in the previous section can be expanded to build a basic data transfer model. Any information input into one end of the connection will be output to the other end, and input and output can be easily captured in order to emulate file transfer.
2024-01-17 08:44:04 +00:00
Start by using `nc` to listen on a specific port, with output captured into a file:
2023-12-04 10:02:23 +00:00
```shell
nc -l 1234 > filename.out
```
2024-01-17 08:44:04 +00:00
Using a second machine, connect to the listening `nc` process, feeding it the file which is to be transferred:
2023-12-04 10:02:23 +00:00
```shell
nc -N host.example.com 1234 < filename.in
```
### Talking to Servers
It is sometimes useful to talk to servers “by hand” rather than through a user interface. It can aid in troubleshooting, when it might be necessary to verify what data a server is sending in response to commands issued by the client. For example, to retrieve the home page of a web site:
```shell
printf "GET / HTTP/1.0\r\n\r\n" | nc host.example.com 80
```