1 .\" $OpenBSD: nc.1,v 1.67 2014/02/26 20:56:11 claudio 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 46DdEFhklNnrStUuvz
40 .Op Fl e Ar IPsec_policy
45 .Op Fl P Ar proxy_username
46 .Op Fl p Ar source_port
48 .Op Fl T Ar toskeyword
51 .Op Fl X Ar proxy_protocol
53 .Fl x Ar proxy_address Ns Oo : Ns
64 utility is used for just about anything under the sun involving TCP,
68 It can open TCP connections, send UDP packets, listen on arbitrary
69 TCP and UDP ports, do port scanning, and deal with both IPv4 and
74 scripts nicely, and separates error messages onto standard error instead
75 of sending them to standard output, as
81 .Bl -bullet -offset indent -compact
85 shell-script based HTTP clients and servers
87 network daemon testing
89 a SOCKS or HTTP ProxyCommand for
95 The options are as follows:
100 to use IPv4 addresses only.
104 to use IPv6 addresses only.
106 Enable debugging on the socket.
108 Do not attempt to read from stdin.
112 .Li "-e 'in ipsec esp/transport//require'"
113 .Li "-e 'out ipsec esp/transport//require'"
115 which enables IPsec ESP transport mode in both
118 If IPsec support is available, then one can specify the IPsec policies
119 to be used using the syntax described in
120 .Xr ipsec_set_policy 3 .
121 This flag can be specified up to two times, as typically one policy for
122 each direction is needed.
124 Pass the first connected socket using
127 This is useful in conjunction with
131 perform connection setup with a proxy but then leave the rest of the
132 connection to another program (e.g.\&
143 Specifies the size of the TCP receive buffer.
145 Specifies a delay time interval between lines of text sent and received.
146 Also causes a delay time between connections to multiple ports.
150 to stay listening for another connection after its current connection
152 It is an error to use this option without the
155 When used together with the
157 option, the server socket is not connected and it can receive UDP datagrams from
162 should listen for an incoming connection rather than initiate a
163 connection to a remote host.
164 It is an error to use this option in conjunction with the
170 Additionally, any timeouts specified with the
175 the network socket after EOF on the input.
176 Some servers require this to finish their work.
178 Do not do any DNS or service lookups on any specified addresses,
181 Disables the use of TCP options on the socket, by setting the boolean
185 Specifies the size of the TCP send buffer.
186 .It Fl P Ar proxy_username
187 Specifies a username to present to a proxy server that requires authentication.
188 If no username is specified then authentication will not be attempted.
189 Proxy authentication is only supported for HTTP CONNECT proxies at present.
190 .It Fl p Ar source_port
191 Specifies the source port
193 should use, subject to privilege restrictions and availability.
194 It is an error to use this option in conjunction with the
198 Specifies that source and/or destination ports should be chosen randomly
199 instead of sequentially within a range or in the order that the system
202 Enables the RFC 2385 TCP MD5 signature option.
204 Specifies the IP of the interface which is used to send the packets.
207 datagram sockets, specifies the local temporary socket file
208 to create and use so that datagrams can be received.
209 It is an error to use this option in conjunction with the
212 .It Fl T Ar toskeyword
213 Change IPv4 TOS value.
222 or one of the DiffServ Code Points:
226 or a number in either hex or decimal.
230 to send RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL requests.
231 This makes it possible to use
233 to script telnet sessions.
239 Use UDP instead of the default option of TCP.
242 sockets, use a datagram socket instead of a stream socket.
245 socket is used, a temporary receiving socket is created in
251 Set the routing table
257 give more verbose output.
259 Connections which cannot be established or are idle timeout after
264 flag has no effect on the
268 will listen forever for a connection, with or without the
271 The default is no timeout.
272 .It Fl X Ar proxy_protocol
275 should use the specified protocol when talking to the proxy server.
276 Supported protocols are
284 If the protocol is not specified, SOCKS version 5 is used.
286 .Fl x Ar proxy_address Ns Oo : Ns
299 is not specified, the well-known port for the proxy protocol is used (1080
300 for SOCKS, 3128 for HTTPS).
304 should just scan for listening daemons, without sending any data to them.
305 It is an error to use this option in conjunction with the
311 can be a numerical IP address or a symbolic hostname
315 In general, a destination must be specified,
319 (in which case the local host is used).
322 sockets, a destination is required and is the socket path to connect to
328 can be a single integer or a range of ports.
329 Ranges are in the form nn-mm.
331 a destination port must be specified,
335 .Sh CLIENT/SERVER MODEL
336 It is quite simple to build a very basic client/server model using
338 On one console, start
340 listening on a specific port for a connection.
346 is now listening on port 1234 for a connection.
348 .Pq or a second machine ,
349 connect to the machine and port being listened on:
351 .Dl $ nc 127.0.0.1 1234
353 There should now be a connection between the ports.
354 Anything typed at the second console will be concatenated to the first,
356 After the connection has been set up,
358 does not really care which side is being used as a
360 and which side is being used as a
362 The connection may be terminated using an
366 The example in the previous section can be expanded to build a
367 basic data transfer model.
368 Any information input into one end of the connection will be output
369 to the other end, and input and output can be easily captured in order to
370 emulate file transfer.
374 to listen on a specific port, with output captured into a file:
376 .Dl $ nc -l 1234 \*(Gt filename.out
378 Using a second machine, connect to the listening
380 process, feeding it the file which is to be transferred:
382 .Dl $ nc -N host.example.com 1234 \*(Lt filename.in
384 After the file has been transferred, the connection will close automatically.
385 .Sh TALKING TO SERVERS
386 It is sometimes useful to talk to servers
388 rather than through a user interface.
389 It can aid in troubleshooting,
390 when it might be necessary to verify what data a server is sending
391 in response to commands issued by the client.
392 For example, to retrieve the home page of a web site:
393 .Bd -literal -offset indent
394 $ printf "GET / HTTP/1.0\er\en\er\en" | nc host.example.com 80
397 Note that this also displays the headers sent by the web server.
398 They can be filtered, using a tool such as
402 More complicated examples can be built up when the user knows the format
403 of requests required by the server.
404 As another example, an email may be submitted to an SMTP server using:
405 .Bd -literal -offset indent
406 $ nc localhost 25 \*(Lt\*(Lt EOF
407 HELO host.example.com
408 MAIL FROM:\*(Ltuser@host.example.com\*(Gt
409 RCPT TO:\*(Ltuser2@host.example.com\*(Gt
417 It may be useful to know which ports are open and running services on
421 flag can be used to tell
423 to report open ports,
424 rather than initiate a connection.
426 .Bd -literal -offset indent
427 $ nc -z host.example.com 20-30
428 Connection to host.example.com 22 port [tcp/ssh] succeeded!
429 Connection to host.example.com 25 port [tcp/smtp] succeeded!
432 The port range was specified to limit the search to ports 20 \- 30.
434 Alternatively, it might be useful to know which server software
435 is running, and which versions.
436 This information is often contained within the greeting banners.
437 In order to retrieve these, it is necessary to first make a connection,
438 and then break the connection when the banner has been retrieved.
439 This can be accomplished by specifying a small timeout with the
441 flag, or perhaps by issuing a
443 command to the server:
444 .Bd -literal -offset indent
445 $ echo "QUIT" | nc host.example.com 20-30
446 SSH-1.99-OpenSSH_3.6.1p2
448 220 host.example.com IMS SMTP Receiver Version 0.84 Ready
451 Open a TCP connection to port 42 of host.example.com, using port 31337 as
452 the source port, with a timeout of 5 seconds:
454 .Dl $ nc -p 31337 -w 5 host.example.com 42
456 Open a UDP connection to port 53 of host.example.com:
458 .Dl $ nc -u host.example.com 53
460 Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as the
461 IP for the local end of the connection:
463 .Dl $ nc -s 10.1.2.3 host.example.com 42
465 Open a TCP connection to port 42 of host.example.com using IPsec ESP for
466 incoming and outgoing traffic.
468 .Dl $ nc -E host.example.com 42
470 Open a TCP connection to port 42 of host.example.com using IPsec ESP for
471 outgoing traffic only.
473 .Dl $ nc -e 'out ipsec esp/transport//require' host.example.com 42
475 Create and listen on a
479 .Dl $ nc -lU /var/tmp/dsocket
481 Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4,
483 This example could also be used by
489 for more information.
491 .Dl $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42
493 The same example again, this time enabling proxy authentication with username
495 if the proxy requires it:
497 .Dl $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42
506 Original implementation by *Hobbit*
507 .Aq Mt hobbit@avian.org .
509 Rewritten with IPv6 support by
510 .An Eric Jackson Aq Mt ericj@monkey.org .
512 UDP port scans using the
514 combination of flags will always report success irrespective of
515 the target machine's state.
517 in conjunction with a traffic sniffer either on the target machine
518 or an intermediary device,
521 combination could be useful for communications diagnostics.
522 Note that the amount of UDP traffic generated may be limited either
523 due to hardware resources and/or configuration settings.