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