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