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