- Richard Feynman -
nc
Description
Description: nc — arbitrary TCP and UDP connections and listens
Sometimes installed as nc
, netc
, netcat
, ncat
.
From the man file:
The nc (or netcat) utility is used for just about anything under the sun involving TCP, UDP, or UNIX-domain sockets. It can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP ports, do port scan‐ ning, and deal with both IPv4 and IPv6. Unlike telnet(1), nc scripts nicely, and separates error messages onto standard error instead of send‐
ing them to standard output, as telnet(1) does with some.
Example usage
Example on how to use:
Connect to www.apache.org web server and get the top html page.
Using -C flag to get the correct line-endings:
$ nc -C www.apache.org 80
GET / HTTP/1.0
Host: www
HTTP/1.1 200 OK
Date: Wed, 03 Jul 2019 07:08:16 GMT
Server: Apache/2.4.18 (Ubuntu)
Last-Modified: Wed, 03 Jul 2019 06:10:21 GMT
ETag: "136f2-58cc0b759e22b"
Accept-Ranges: bytes
Content-Length: 79602
Vary: Accept-Encoding
Cache-Control: max-age=3600
Expires: Wed, 03 Jul 2019 08:08:16 GMT
Connection: close
Content-Type: text/html
<!DOCTYPE html>
<html lang="en">
<head>
...etc, etc...
Your input is hightlighted. The rest is the response from the web server. Please note that you need two line-breaks after the Host: header.
Using echo and pipes (to get the correct line-endings):
$ echo -e 'GET / HTTP/1.0\r\nHost: www\r\nConnection: close\r\n\r\n'|nc www.apache.org 80
HTTP/1.1 200 OK
Date: Tue, 07 Feb 2017 08:34:34 GMT
Server: Apache/2.4.7 (Ubuntu)
Last-Modified: Tue, 07 Feb 2017 08:10:32 GMT
ETag: "d716-547ec482a0c1c"
Accept-Ranges: bytes
Content-Length: 55062
Vary: Accept-Encoding
Cache-Control: max-age=3600
Expires: Tue, 07 Feb 2017 09:34:34 GMT
Connection: close
Content-Type: text/html
<!DOCTYPE html>
<html lang="en">
<head>
......
Start up a listening server on port 9090.
$ nc -l -p 9090
... and connect to that server using netcat (in another terminal)
$ nc localhost 9090
You can now transfer text between the two.