]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/netcat/nc.1
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / contrib / netcat / nc.1
1 .\"     $OpenBSD: nc.1,v 1.62 2013/03/20 09:27:56 sthen Exp $
2 .\"
3 .\" Copyright (c) 1996 David Sacerdote
4 .\" All rights reserved.
5 .\"
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
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
16 .\"
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.
27 .\"
28 .\" $FreeBSD$
29 .\"
30 .Dd July 7, 2012
31 .Dt NC 1
32 .Os
33 .Sh NAME
34 .Nm nc
35 .Nd arbitrary TCP and UDP connections and listens
36 .Sh SYNOPSIS
37 .Nm nc
38 .Bk -words
39 .Op Fl 46DdEhklNnrStUuvz
40 .Op Fl e Ar IPsec_policy
41 .Op Fl I Ar length
42 .Op Fl i Ar interval
43 .Op Fl -no-tcpopt
44 .Op Fl O Ar length
45 .Op Fl P Ar proxy_username
46 .Op Fl p Ar source_port
47 .Op Fl s Ar source
48 .Op Fl T Ar toskeyword
49 .Op Fl V Ar rtable
50 .Op Fl w Ar timeout
51 .Op Fl X Ar proxy_protocol
52 .Oo Xo
53 .Fl x Ar proxy_address Ns Oo : Ns
54 .Ar port Oc
55 .Xc Oc
56 .Op Ar destination
57 .Op Ar port
58 .Ek
59 .Sh DESCRIPTION
60 The
61 .Nm
62 (or
63 .Nm netcat )
64 utility is used for just about anything under the sun involving TCP,
65 UDP, or
66 .Ux Ns -domain
67 sockets.
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
70 IPv6.
71 Unlike
72 .Xr telnet 1 ,
73 .Nm
74 scripts nicely, and separates error messages onto standard error instead
75 of sending them to standard output, as
76 .Xr telnet 1
77 does with some.
78 .Pp
79 Common uses include:
80 .Pp
81 .Bl -bullet -offset indent -compact
82 .It
83 simple TCP proxies
84 .It
85 shell-script based HTTP clients and servers
86 .It
87 network daemon testing
88 .It
89 a SOCKS or HTTP ProxyCommand for
90 .Xr ssh 1
91 .It
92 and much, much more
93 .El
94 .Pp
95 The options are as follows:
96 .Bl -tag -width Ds
97 .It Fl 4
98 Forces
99 .Nm
100 to use IPv4 addresses only.
101 .It Fl 6
102 Forces
103 .Nm
104 to use IPv6 addresses only.
105 .It Fl D
106 Enable debugging on the socket.
107 .It Fl d
108 Do not attempt to read from stdin.
109 .It Fl E
110 Shortcut for
111 .Qo
112 .Li "-e 'in ipsec esp/transport//require'"
113 .Li "-e 'out ipsec esp/transport//require'" 
114 .Qc ,
115 which enables IPsec ESP transport mode in both
116 directions.
117 .It Fl e
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.
123 .It Fl h
124 Prints out
125 .Nm
126 help.
127 .It Fl I Ar length
128 Specifies the size of the TCP receive buffer.
129 .It Fl i Ar interval
130 Specifies a delay time interval between lines of text sent and received.
131 Also causes a delay time between connections to multiple ports.
132 .It Fl k
133 Forces
134 .Nm
135 to stay listening for another connection after its current connection
136 is completed.
137 It is an error to use this option without the
138 .Fl l
139 option.
140 When used together with the
141 .Fl u
142 option, the server socket is not connected and it can receive UDP datagrams from
143 multiple hosts.
144 .It Fl l
145 Used to specify that
146 .Nm
147 should listen for an incoming connection rather than initiate a
148 connection to a remote host.
149 It is an error to use this option in conjunction with the
150 .Fl p ,
151 .Fl s ,
152 or
153 .Fl z
154 options.
155 Additionally, any timeouts specified with the
156 .Fl w
157 option are ignored.
158 .It Fl N
159 .Xr shutdown 2
160 the network socket after EOF on the input.
161 Some servers require this to finish their work.
162 .It Fl n
163 Do not do any DNS or service lookups on any specified addresses,
164 hostnames or ports.
165 .It Fl -no-tcpopt
166 Disables the use of TCP options on the socket, by setting the boolean
167 TCP_NOOPT
168 socket option.
169 .It Fl O Ar length
170 Specifies the size of the TCP send buffer.
171 .It Fl P Ar proxy_username
172 Specifies a username to present to a proxy server that requires authentication.
173 If no username is specified then authentication will not be attempted.
174 Proxy authentication is only supported for HTTP CONNECT proxies at present.
175 .It Fl p Ar source_port
176 Specifies the source port
177 .Nm
178 should use, subject to privilege restrictions and availability.
179 It is an error to use this option in conjunction with the
180 .Fl l
181 option.
182 .It Fl r
183 Specifies that source and/or destination ports should be chosen randomly
184 instead of sequentially within a range or in the order that the system
185 assigns them.
186 .It Fl S
187 Enables the RFC 2385 TCP MD5 signature option.
188 .It Fl s Ar source
189 Specifies the IP of the interface which is used to send the packets.
190 For
191 .Ux Ns -domain
192 datagram sockets, specifies the local temporary socket file
193 to create and use so that datagrams can be received.
194 It is an error to use this option in conjunction with the
195 .Fl l
196 option.
197 .It Fl T Ar toskeyword
198 Change IPv4 TOS value.
199 .Ar toskeyword
200 may be one of
201 .Ar critical ,
202 .Ar inetcontrol ,
203 .Ar lowdelay ,
204 .Ar netcontrol ,
205 .Ar throughput ,
206 .Ar reliability ,
207 or one of the DiffServ Code Points:
208 .Ar ef ,
209 .Ar af11 ... af43 ,
210 .Ar cs0 ... cs7 ;
211 or a number in either hex or decimal.
212 .It Fl t
213 Causes
214 .Nm
215 to send RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL requests.
216 This makes it possible to use
217 .Nm
218 to script telnet sessions.
219 .It Fl U
220 Specifies to use
221 .Ux Ns -domain
222 sockets.
223 .It Fl u
224 Use UDP instead of the default option of TCP.
225 For
226 .Ux Ns -domain
227 sockets, use a datagram socket instead of a stream socket.
228 If a
229 .Ux Ns -domain
230 socket is used, a temporary receiving socket is created in
231 .Pa /tmp
232 unless the
233 .Fl s
234 flag is given.
235 .It Fl V Ar rtable
236 Set the routing table
237 .Pq Dq FIB
238 to be used.
239 The default is 0.
240 .It Fl v
241 Have
242 .Nm
243 give more verbose output.
244 .It Fl w Ar timeout
245 Connections which cannot be established or are idle timeout after
246 .Ar timeout
247 seconds.
248 The
249 .Fl w
250 flag has no effect on the
251 .Fl l
252 option, i.e.\&
253 .Nm
254 will listen forever for a connection, with or without the
255 .Fl w
256 flag.
257 The default is no timeout.
258 .It Fl X Ar proxy_protocol
259 Requests that
260 .Nm
261 should use the specified protocol when talking to the proxy server.
262 Supported protocols are
263 .Dq 4
264 (SOCKS v.4),
265 .Dq 5
266 (SOCKS v.5)
267 and
268 .Dq connect
269 (HTTPS proxy).
270 If the protocol is not specified, SOCKS version 5 is used.
271 .It Xo
272 .Fl x Ar proxy_address Ns Oo : Ns
273 .Ar port Oc
274 .Xc
275 Requests that
276 .Nm
277 should connect to
278 .Ar destination
279 using a proxy at
280 .Ar proxy_address
281 and
282 .Ar port .
283 If
284 .Ar port
285 is not specified, the well-known port for the proxy protocol is used (1080
286 for SOCKS, 3128 for HTTPS).
287 .It Fl z
288 Specifies that
289 .Nm
290 should just scan for listening daemons, without sending any data to them.
291 It is an error to use this option in conjunction with the
292 .Fl l
293 option.
294 .El
295 .Pp
296 .Ar destination
297 can be a numerical IP address or a symbolic hostname
298 (unless the
299 .Fl n
300 option is given).
301 In general, a destination must be specified,
302 unless the
303 .Fl l
304 option is given
305 (in which case the local host is used).
306 For
307 .Ux Ns -domain
308 sockets, a destination is required and is the socket path to connect to
309 (or listen on if the
310 .Fl l
311 option is given).
312 .Pp
313 .Ar port
314 can be a single integer or a range of ports.
315 Ranges are in the form nn-mm.
316 In general,
317 a destination port must be specified,
318 unless the
319 .Fl U
320 option is given.
321 .Sh CLIENT/SERVER MODEL
322 It is quite simple to build a very basic client/server model using
323 .Nm .
324 On one console, start
325 .Nm
326 listening on a specific port for a connection.
327 For example:
328 .Pp
329 .Dl $ nc -l 1234
330 .Pp
331 .Nm
332 is now listening on port 1234 for a connection.
333 On a second console
334 .Pq or a second machine ,
335 connect to the machine and port being listened on:
336 .Pp
337 .Dl $ nc 127.0.0.1 1234
338 .Pp
339 There should now be a connection between the ports.
340 Anything typed at the second console will be concatenated to the first,
341 and vice-versa.
342 After the connection has been set up,
343 .Nm
344 does not really care which side is being used as a
345 .Sq server
346 and which side is being used as a
347 .Sq client .
348 The connection may be terminated using an
349 .Dv EOF
350 .Pq Sq ^D .
351 .Sh DATA TRANSFER
352 The example in the previous section can be expanded to build a
353 basic data transfer model.
354 Any information input into one end of the connection will be output
355 to the other end, and input and output can be easily captured in order to
356 emulate file transfer.
357 .Pp
358 Start by using
359 .Nm
360 to listen on a specific port, with output captured into a file:
361 .Pp
362 .Dl $ nc -l 1234 \*(Gt filename.out
363 .Pp
364 Using a second machine, connect to the listening
365 .Nm
366 process, feeding it the file which is to be transferred:
367 .Pp
368 .Dl $ nc host.example.com 1234 \*(Lt filename.in
369 .Pp
370 After the file has been transferred, the connection will close automatically.
371 .Sh TALKING TO SERVERS
372 It is sometimes useful to talk to servers
373 .Dq by hand
374 rather than through a user interface.
375 It can aid in troubleshooting,
376 when it might be necessary to verify what data a server is sending
377 in response to commands issued by the client.
378 For example, to retrieve the home page of a web site:
379 .Bd -literal -offset indent
380 $ printf "GET / HTTP/1.0\er\en\er\en" | nc host.example.com 80
381 .Ed
382 .Pp
383 Note that this also displays the headers sent by the web server.
384 They can be filtered, using a tool such as
385 .Xr sed 1 ,
386 if necessary.
387 .Pp
388 More complicated examples can be built up when the user knows the format
389 of requests required by the server.
390 As another example, an email may be submitted to an SMTP server using:
391 .Bd -literal -offset indent
392 $ nc localhost 25 \*(Lt\*(Lt EOF
393 HELO host.example.com
394 MAIL FROM:\*(Ltuser@host.example.com\*(Gt
395 RCPT TO:\*(Ltuser2@host.example.com\*(Gt
396 DATA
397 Body of email.
398 \&.
399 QUIT
400 EOF
401 .Ed
402 .Sh PORT SCANNING
403 It may be useful to know which ports are open and running services on
404 a target machine.
405 The
406 .Fl z
407 flag can be used to tell
408 .Nm
409 to report open ports,
410 rather than initiate a connection.
411 For example:
412 .Bd -literal -offset indent
413 $ nc -z host.example.com 20-30
414 Connection to host.example.com 22 port [tcp/ssh] succeeded!
415 Connection to host.example.com 25 port [tcp/smtp] succeeded!
416 .Ed
417 .Pp
418 The port range was specified to limit the search to ports 20 \- 30.
419 .Pp
420 Alternatively, it might be useful to know which server software
421 is running, and which versions.
422 This information is often contained within the greeting banners.
423 In order to retrieve these, it is necessary to first make a connection,
424 and then break the connection when the banner has been retrieved.
425 This can be accomplished by specifying a small timeout with the
426 .Fl w
427 flag, or perhaps by issuing a
428 .Qq Dv QUIT
429 command to the server:
430 .Bd -literal -offset indent
431 $ echo "QUIT" | nc host.example.com 20-30
432 SSH-1.99-OpenSSH_3.6.1p2
433 Protocol mismatch.
434 220 host.example.com IMS SMTP Receiver Version 0.84 Ready
435 .Ed
436 .Sh EXAMPLES
437 Open a TCP connection to port 42 of host.example.com, using port 31337 as
438 the source port, with a timeout of 5 seconds:
439 .Pp
440 .Dl $ nc -p 31337 -w 5 host.example.com 42
441 .Pp
442 Open a UDP connection to port 53 of host.example.com:
443 .Pp
444 .Dl $ nc -u host.example.com 53
445 .Pp
446 Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as the
447 IP for the local end of the connection:
448 .Pp
449 .Dl $ nc -s 10.1.2.3 host.example.com 42
450 .Pp
451 Open a TCP connection to port 42 of host.example.com using IPsec ESP for
452 incoming and outgoing traffic.
453 .Pp
454 .Dl $ nc -E host.example.com 42
455 .Pp
456 Open a TCP connection to port 42 of host.example.com using IPsec ESP for
457 outgoing traffic only.
458 .Pp
459 .Dl $ nc -e 'out ipsec esp/transport//require' host.example.com 42
460 .Pp
461 Create and listen on a
462 .Ux Ns -domain
463 stream socket:
464 .Pp
465 .Dl $ nc -lU /var/tmp/dsocket
466 .Pp
467 Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4,
468 port 8080.
469 This example could also be used by
470 .Xr ssh 1 ;
471 see the
472 .Cm ProxyCommand
473 directive in
474 .Xr ssh_config 5
475 for more information.
476 .Pp
477 .Dl $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42
478 .Pp
479 The same example again, this time enabling proxy authentication with username
480 .Dq ruser
481 if the proxy requires it:
482 .Pp
483 .Dl $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42
484 .Sh EXIT STATUS
485 .Ex -std
486 .Sh SEE ALSO
487 .Xr cat 1 ,
488 .Xr setfib 1 ,
489 .Xr ssh 1 ,
490 .Xr tcp 4
491 .Sh AUTHORS
492 Original implementation by *Hobbit*
493 .Aq hobbit@avian.org .
494 .br
495 Rewritten with IPv6 support by
496 .An Eric Jackson Aq ericj@monkey.org .
497 .Sh CAVEATS
498 UDP port scans using the
499 .Fl uz
500 combination of flags will always report success irrespective of
501 the target machine's state.
502 However,
503 in conjunction with a traffic sniffer either on the target machine
504 or an intermediary device,
505 the
506 .Fl uz
507 combination could be useful for communications diagnostics.
508 Note that the amount of UDP traffic generated may be limited either
509 due to hardware resources and/or configuration settings.