1 .\" $OpenBSD: nc.1,v 1.68 2015/03/26 10:35:04 tobias Exp $
3 .\" Copyright (c) 1996 David Sacerdote
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. The name of the author may not be used to endorse or promote products
15 .\" derived from this software without specific prior written permission
17 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 .Nd arbitrary TCP and UDP connections and listens
39 .Op Fl 46DdEFhklMNnrStUuvz
40 .Op Fl e Ar IPsec_policy
46 .Op Fl P Ar proxy_username
47 .Op Fl p Ar source_port
49 .Op Fl T Ar toskeyword
53 .Op Fl X Ar proxy_protocol
55 .Fl x Ar proxy_address Ns Oo : Ns
66 utility is used for just about anything under the sun involving TCP,
70 It can open TCP connections, send UDP packets, listen on arbitrary
71 TCP and UDP ports, do port scanning, and deal with both IPv4 and
76 scripts nicely, and separates error messages onto standard error instead
77 of sending them to standard output, as
83 .Bl -bullet -offset indent -compact
87 shell-script based HTTP clients and servers
89 network daemon testing
91 a SOCKS or HTTP ProxyCommand for
97 The options are as follows:
102 to use IPv4 addresses only.
106 to use IPv6 addresses only.
108 Enable debugging on the socket.
110 Do not attempt to read from stdin.
114 .Li "-e 'in ipsec esp/transport//require'"
115 .Li "-e 'out ipsec esp/transport//require'"
117 which enables IPsec ESP transport mode in both
120 If IPsec support is available, then one can specify the IPsec policies
121 to be used using the syntax described in
122 .Xr ipsec_set_policy 3 .
123 This flag can be specified up to two times, as typically one policy for
124 each direction is needed.
126 Pass the first connected socket using
129 This is useful in conjunction with
133 perform connection setup with a proxy but then leave the rest of the
134 connection to another program (e.g.\&
145 Specifies the size of the TCP receive buffer.
147 Specifies a delay time interval between lines of text sent and received.
148 Also causes a delay time between connections to multiple ports.
152 to stay listening for another connection after its current connection
154 It is an error to use this option without the
157 When used together with the
159 option, the server socket is not connected and it can receive UDP datagrams from
164 should listen for an incoming connection rather than initiate a
165 connection to a remote host.
166 It is an error to use this option in conjunction with the
172 Additionally, any timeouts specified with the
176 Collect per-connection TCP statistics using the
178 framework and print them in JSON format to
180 after the connection is closed.
183 the network socket after EOF on the input.
184 Some servers require this to finish their work.
186 Do not do any DNS or service lookups on any specified addresses,
189 Disables the use of TCP options on the socket, by setting the boolean
193 Use SCTP instead of the default option of TCP.
195 Specifies the size of the TCP send buffer.
196 .It Fl P Ar proxy_username
197 Specifies a username to present to a proxy server that requires authentication.
198 If no username is specified then authentication will not be attempted.
199 Proxy authentication is only supported for HTTP CONNECT proxies at present.
200 .It Fl p Ar source_port
201 Specifies the source port
203 should use, subject to privilege restrictions and availability.
204 It is an error to use this option in conjunction with the
208 Specifies that source and/or destination ports should be chosen randomly
209 instead of sequentially within a range or in the order that the system
212 Enables the RFC 2385 TCP MD5 signature option.
214 Specifies the IP of the interface which is used to send the packets.
217 datagram sockets, specifies the local temporary socket file
218 to create and use so that datagrams can be received.
219 It is an error to use this option in conjunction with the
222 .It Fl T Ar toskeyword
223 Change IPv4 TOS value.
232 or one of the DiffServ Code Points:
236 or a number in either hex or decimal.
240 to send RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL requests.
241 This makes it possible to use
243 to script telnet sessions.
244 .It Fl -tun Ar tundev
249 for input and output rather than the default of stdin and stdout.
255 Use UDP instead of the default option of TCP.
258 sockets, use a datagram socket instead of a stream socket.
261 socket is used, a temporary receiving socket is created in
267 Set the routing table
273 give more verbose output.
275 Connections which cannot be established or are idle timeout after
280 flag has no effect on the
284 will listen forever for a connection, with or without the
287 The default is no timeout.
288 .It Fl X Ar proxy_protocol
291 should use the specified protocol when talking to the proxy server.
292 Supported protocols are
300 If the protocol is not specified, SOCKS version 5 is used.
302 .Fl x Ar proxy_address Ns Oo : Ns
315 is not specified, the well-known port for the proxy protocol is used (1080
316 for SOCKS, 3128 for HTTPS).
320 should just scan for listening daemons, without sending any data to them.
321 It is an error to use this option in conjunction with the
327 can be a numerical IP address or a symbolic hostname
331 In general, a destination must be specified,
335 (in which case the local host is used).
338 sockets, a destination is required and is the socket path to connect to
344 can be a single integer or a range of ports.
345 Ranges are in the form nn-mm.
347 a destination port must be specified,
351 .Sh CLIENT/SERVER MODEL
352 It is quite simple to build a very basic client/server model using
354 On one console, start
356 listening on a specific port for a connection.
362 is now listening on port 1234 for a connection.
364 .Pq or a second machine ,
365 connect to the machine and port being listened on:
367 .Dl $ nc 127.0.0.1 1234
369 There should now be a connection between the ports.
370 Anything typed at the second console will be concatenated to the first,
372 After the connection has been set up,
374 does not really care which side is being used as a
376 and which side is being used as a
378 The connection may be terminated using an
382 The example in the previous section can be expanded to build a
383 basic data transfer model.
384 Any information input into one end of the connection will be output
385 to the other end, and input and output can be easily captured in order to
386 emulate file transfer.
390 to listen on a specific port, with output captured into a file:
392 .Dl $ nc -l 1234 \*(Gt filename.out
394 Using a second machine, connect to the listening
396 process, feeding it the file which is to be transferred:
398 .Dl $ nc -N host.example.com 1234 \*(Lt filename.in
400 After the file has been transferred, the connection will close automatically.
401 .Sh TALKING TO SERVERS
402 It is sometimes useful to talk to servers
404 rather than through a user interface.
405 It can aid in troubleshooting,
406 when it might be necessary to verify what data a server is sending
407 in response to commands issued by the client.
408 For example, to retrieve the home page of a web site:
409 .Bd -literal -offset indent
410 $ printf "GET / HTTP/1.0\er\en\er\en" | nc host.example.com 80
413 Note that this also displays the headers sent by the web server.
414 They can be filtered, using a tool such as
418 More complicated examples can be built up when the user knows the format
419 of requests required by the server.
420 As another example, an email may be submitted to an SMTP server using:
421 .Bd -literal -offset indent
422 $ nc localhost 25 \*(Lt\*(Lt EOF
423 HELO host.example.com
424 MAIL FROM:\*(Ltuser@host.example.com\*(Gt
425 RCPT TO:\*(Ltuser2@host.example.com\*(Gt
433 It may be useful to know which ports are open and running services on
437 flag can be used to tell
439 to report open ports,
440 rather than initiate a connection.
442 .Bd -literal -offset indent
443 $ nc -z host.example.com 20-30
444 Connection to host.example.com 22 port [tcp/ssh] succeeded!
445 Connection to host.example.com 25 port [tcp/smtp] succeeded!
448 The port range was specified to limit the search to ports 20 \- 30.
450 Alternatively, it might be useful to know which server software
451 is running, and which versions.
452 This information is often contained within the greeting banners.
453 In order to retrieve these, it is necessary to first make a connection,
454 and then break the connection when the banner has been retrieved.
455 This can be accomplished by specifying a small timeout with the
457 flag, or perhaps by issuing a
459 command to the server:
460 .Bd -literal -offset indent
461 $ echo "QUIT" | nc host.example.com 20-30
462 SSH-1.99-OpenSSH_3.6.1p2
464 220 host.example.com IMS SMTP Receiver Version 0.84 Ready
467 Open a TCP connection to port 42 of host.example.com, using port 31337 as
468 the source port, with a timeout of 5 seconds:
470 .Dl $ nc -p 31337 -w 5 host.example.com 42
472 Open a UDP connection to port 53 of host.example.com:
474 .Dl $ nc -u host.example.com 53
476 Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as the
477 IP for the local end of the connection:
479 .Dl $ nc -s 10.1.2.3 host.example.com 42
481 Open a TCP connection to port 42 of host.example.com using IPsec ESP for
482 incoming and outgoing traffic.
484 .Dl $ nc -E host.example.com 42
486 Open a TCP connection to port 42 of host.example.com using IPsec ESP for
487 outgoing traffic only.
489 .Dl $ nc -e 'out ipsec esp/transport//require' host.example.com 42
491 Create and listen on a
495 .Dl $ nc -lU /var/tmp/dsocket
497 Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4,
499 This example could also be used by
505 for more information.
507 .Dl $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42
509 The same example again, this time enabling proxy authentication with username
511 if the proxy requires it:
513 .Dl $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42
522 Original implementation by *Hobbit*
523 .Aq Mt hobbit@avian.org .
525 Rewritten with IPv6 support by
526 .An Eric Jackson Aq Mt ericj@monkey.org .
528 UDP port scans using the
530 combination of flags will always report success irrespective of
531 the target machine's state.
533 in conjunction with a traffic sniffer either on the target machine
534 or an intermediary device,
537 combination could be useful for communications diagnostics.
538 Note that the amount of UDP traffic generated may be limited either
539 due to hardware resources and/or configuration settings.