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