xref: /freebsd/contrib/netcat/nc.1 (revision 04a3ca71ead61b4ced23a791d3a5e2633ca1ef72)
1100e2a06SXin LI.\"     $OpenBSD: nc.1,v 1.68 2015/03/26 10:35:04 tobias Exp $
2b0683daeSXin LI.\"
38c384020SXin LI.\" Copyright (c) 1996 David Sacerdote
48c384020SXin LI.\" All rights reserved.
58c384020SXin LI.\"
68c384020SXin LI.\" Redistribution and use in source and binary forms, with or without
78c384020SXin LI.\" modification, are permitted provided that the following conditions
88c384020SXin LI.\" are met:
98c384020SXin LI.\" 1. Redistributions of source code must retain the above copyright
108c384020SXin LI.\"    notice, this list of conditions and the following disclaimer.
118c384020SXin LI.\" 2. Redistributions in binary form must reproduce the above copyright
128c384020SXin LI.\"    notice, this list of conditions and the following disclaimer in the
138c384020SXin LI.\"    documentation and/or other materials provided with the distribution.
148c384020SXin LI.\" 3. The name of the author may not be used to endorse or promote products
158c384020SXin LI.\"    derived from this software without specific prior written permission
168c384020SXin LI.\"
178c384020SXin LI.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
188c384020SXin LI.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
198c384020SXin LI.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
208c384020SXin LI.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
218c384020SXin LI.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
228c384020SXin LI.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
238c384020SXin LI.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
248c384020SXin LI.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
258c384020SXin LI.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
268c384020SXin LI.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
278c384020SXin LI.\"
2804def624SXin LI.\" $FreeBSD$
2904def624SXin LI.\"
30*04a3ca71SMark Johnston.Dd January 20, 2025
318c384020SXin LI.Dt NC 1
328c384020SXin LI.Os
338c384020SXin LI.Sh NAME
348c384020SXin LI.Nm nc
358c384020SXin LI.Nd arbitrary TCP and UDP connections and listens
368c384020SXin LI.Sh SYNOPSIS
378c384020SXin LI.Nm nc
388c384020SXin LI.Bk -words
391e62ecedSEdward Tomasz Napierala.Op Fl 46DdEFhklMNnrStUuvz
404bdc2338SRuslan Ermilov.Op Fl e Ar IPsec_policy
417dcaa833SXin LI.Op Fl I Ar length
428c384020SXin LI.Op Fl i Ar interval
43*04a3ca71SMark Johnston.Op Fl -lb
447dcaa833SXin LI.Op Fl -no-tcpopt
45d0415892SMark Johnston.Op Fl -sctp
46d4b3aefdSPietro Cerutti.Op Fl -crlf
477dcaa833SXin LI.Op Fl O Ar length
482440a458SXin LI.Op Fl P Ar proxy_username
498c384020SXin LI.Op Fl p Ar source_port
5059c7ad52SXin LI.Op Fl s Ar source
51ac3ea910SXin LI.Op Fl T Ar toskeyword
52cef7ab70STom Jones.Op Fl -tun Ar tundev
534f2bbc00SXin LI.Op Fl V Ar rtable
548c384020SXin LI.Op Fl w Ar timeout
558c384020SXin LI.Op Fl X Ar proxy_protocol
568c384020SXin LI.Oo Xo
578c384020SXin LI.Fl x Ar proxy_address Ns Oo : Ns
5826bf9c3bSXin LI.Ar port Oc
5926bf9c3bSXin LI.Xc Oc
6059c7ad52SXin LI.Op Ar destination
611a9dc239SXin LI.Op Ar port
628c384020SXin LI.Ek
638c384020SXin LI.Sh DESCRIPTION
648c384020SXin LIThe
658c384020SXin LI.Nm
668c384020SXin LI(or
678c384020SXin LI.Nm netcat )
6859c7ad52SXin LIutility is used for just about anything under the sun involving TCP,
6959c7ad52SXin LIUDP, or
7059c7ad52SXin LI.Ux Ns -domain
7159c7ad52SXin LIsockets.
728c384020SXin LIIt can open TCP connections, send UDP packets, listen on arbitrary
738c384020SXin LITCP and UDP ports, do port scanning, and deal with both IPv4 and
748c384020SXin LIIPv6.
758c384020SXin LIUnlike
768c384020SXin LI.Xr telnet 1 ,
778c384020SXin LI.Nm
788c384020SXin LIscripts nicely, and separates error messages onto standard error instead
798c384020SXin LIof sending them to standard output, as
808c384020SXin LI.Xr telnet 1
818c384020SXin LIdoes with some.
828c384020SXin LI.Pp
838c384020SXin LICommon uses include:
848c384020SXin LI.Pp
858c384020SXin LI.Bl -bullet -offset indent -compact
868c384020SXin LI.It
878c384020SXin LIsimple TCP proxies
888c384020SXin LI.It
898c384020SXin LIshell-script based HTTP clients and servers
908c384020SXin LI.It
918c384020SXin LInetwork daemon testing
928c384020SXin LI.It
938c384020SXin LIa SOCKS or HTTP ProxyCommand for
948c384020SXin LI.Xr ssh 1
958c384020SXin LI.It
968c384020SXin LIand much, much more
978c384020SXin LI.El
988c384020SXin LI.Pp
998c384020SXin LIThe options are as follows:
1008c384020SXin LI.Bl -tag -width Ds
1018c384020SXin LI.It Fl 4
1028c384020SXin LIForces
1038c384020SXin LI.Nm
1048c384020SXin LIto use IPv4 addresses only.
1058c384020SXin LI.It Fl 6
1068c384020SXin LIForces
1078c384020SXin LI.Nm
1088c384020SXin LIto use IPv6 addresses only.
109d4b3aefdSPietro Cerutti.It Fl -crlf
110d4b3aefdSPietro CeruttiConvert LF into CRLF when sending data over the network.
1118c384020SXin LI.It Fl D
1128c384020SXin LIEnable debugging on the socket.
1138c384020SXin LI.It Fl d
1148c384020SXin LIDo not attempt to read from stdin.
11504def624SXin LI.It Fl E
1164bdc2338SRuslan ErmilovShortcut for
1174bdc2338SRuslan Ermilov.Qo
1184bdc2338SRuslan Ermilov.Li "-e 'in ipsec esp/transport//require'"
1194bdc2338SRuslan Ermilov.Li "-e 'out ipsec esp/transport//require'"
1204bdc2338SRuslan Ermilov.Qc ,
1214bdc2338SRuslan Ermilovwhich enables IPsec ESP transport mode in both
12204def624SXin LIdirections.
12304def624SXin LI.It Fl e
12404def624SXin LIIf IPsec support is available, then one can specify the IPsec policies
12504def624SXin LIto be used using the syntax described in
12604def624SXin LI.Xr ipsec_set_policy 3 .
12704def624SXin LIThis flag can be specified up to two times, as typically one policy for
12804def624SXin LIeach direction is needed.
1293ae0125aSXin LI.It Fl F
1303ae0125aSXin LIPass the first connected socket using
1313ae0125aSXin LI.Xr sendmsg 2
1323ae0125aSXin LIto stdout and exit.
1333ae0125aSXin LIThis is useful in conjunction with
1343ae0125aSXin LI.Fl X
1353ae0125aSXin LIto have
1363ae0125aSXin LI.Nm
1373ae0125aSXin LIperform connection setup with a proxy but then leave the rest of the
1383ae0125aSXin LIconnection to another program (e.g.\&
1393ae0125aSXin LI.Xr ssh 1
1403ae0125aSXin LIusing the
1413ae0125aSXin LI.Xr ssh_config 5
142100e2a06SXin LI.Cm ProxyUseFdpass
1433ae0125aSXin LIoption).
144b6642dadSAntoine Brodin.It Fl h
145b6642dadSAntoine BrodinPrints out
146b6642dadSAntoine Brodin.Nm
147b6642dadSAntoine Brodinhelp.
1487dcaa833SXin LI.It Fl I Ar length
1497dcaa833SXin LISpecifies the size of the TCP receive buffer.
1508c384020SXin LI.It Fl i Ar interval
1518c384020SXin LISpecifies a delay time interval between lines of text sent and received.
1528c384020SXin LIAlso causes a delay time between connections to multiple ports.
1538c384020SXin LI.It Fl k
1548c384020SXin LIForces
1558c384020SXin LI.Nm
1568c384020SXin LIto stay listening for another connection after its current connection
1578c384020SXin LIis completed.
1588c384020SXin LIIt is an error to use this option without the
1598c384020SXin LI.Fl l
1608c384020SXin LIoption.
16152dba105SXin LIWhen used together with the
16252dba105SXin LI.Fl u
16352dba105SXin LIoption, the server socket is not connected and it can receive UDP datagrams from
16452dba105SXin LImultiple hosts.
1658c384020SXin LI.It Fl l
1668c384020SXin LIUsed to specify that
1678c384020SXin LI.Nm
1688c384020SXin LIshould listen for an incoming connection rather than initiate a
1698c384020SXin LIconnection to a remote host.
1708c384020SXin LIIt is an error to use this option in conjunction with the
1718c384020SXin LI.Fl p ,
1728c384020SXin LI.Fl s ,
1738c384020SXin LIor
1748c384020SXin LI.Fl z
1758c384020SXin LIoptions.
1768c384020SXin LIAdditionally, any timeouts specified with the
1778c384020SXin LI.Fl w
1788c384020SXin LIoption are ignored.
179*04a3ca71SMark Johnston.It Fl -lb
180*04a3ca71SMark JohnstonWhen using
181*04a3ca71SMark Johnston.Fl l ,
182*04a3ca71SMark Johnstonput the socket in load-balancing mode.
183*04a3ca71SMark JohnstonIn this mode, multiple sockets can bind to the same address and port,
184*04a3ca71SMark Johnstonand incoming connections are distributed among them.
1851e62ecedSEdward Tomasz Napierala.It Fl M
1861e62ecedSEdward Tomasz NapieralaCollect per-connection TCP statistics using the
1871e62ecedSEdward Tomasz Napierala.Xr stats 3
1881e62ecedSEdward Tomasz Napieralaframework and print them in JSON format to
1891e62ecedSEdward Tomasz Napierala.Xr stderr 4
1901e62ecedSEdward Tomasz Napieralaafter the connection is closed.
1910772266eSXin LI.It Fl N
1920772266eSXin LI.Xr shutdown 2
1930772266eSXin LIthe network socket after EOF on the input.
1940772266eSXin LISome servers require this to finish their work.
1958c384020SXin LI.It Fl n
1968c384020SXin LIDo not do any DNS or service lookups on any specified addresses,
1978c384020SXin LIhostnames or ports.
1987dcaa833SXin LI.It Fl -no-tcpopt
199b6642dadSAntoine BrodinDisables the use of TCP options on the socket, by setting the boolean
200b6642dadSAntoine BrodinTCP_NOOPT
201b6642dadSAntoine Brodinsocket option.
202d0415892SMark Johnston.It Fl -sctp
203d0415892SMark JohnstonUse SCTP instead of the default option of TCP.
2047dcaa833SXin LI.It Fl O Ar length
2057dcaa833SXin LISpecifies the size of the TCP send buffer.
2062440a458SXin LI.It Fl P Ar proxy_username
2072440a458SXin LISpecifies a username to present to a proxy server that requires authentication.
2082440a458SXin LIIf no username is specified then authentication will not be attempted.
2092440a458SXin LIProxy authentication is only supported for HTTP CONNECT proxies at present.
2108c384020SXin LI.It Fl p Ar source_port
2118c384020SXin LISpecifies the source port
2128c384020SXin LI.Nm
2138c384020SXin LIshould use, subject to privilege restrictions and availability.
2148c384020SXin LIIt is an error to use this option in conjunction with the
2158c384020SXin LI.Fl l
2168c384020SXin LIoption.
2178c384020SXin LI.It Fl r
2188c384020SXin LISpecifies that source and/or destination ports should be chosen randomly
2198c384020SXin LIinstead of sequentially within a range or in the order that the system
2208c384020SXin LIassigns them.
2218c384020SXin LI.It Fl S
2228c384020SXin LIEnables the RFC 2385 TCP MD5 signature option.
22359c7ad52SXin LI.It Fl s Ar source
2248c384020SXin LISpecifies the IP of the interface which is used to send the packets.
22559c7ad52SXin LIFor
22659c7ad52SXin LI.Ux Ns -domain
22759c7ad52SXin LIdatagram sockets, specifies the local temporary socket file
22859c7ad52SXin LIto create and use so that datagrams can be received.
2298c384020SXin LIIt is an error to use this option in conjunction with the
2308c384020SXin LI.Fl l
2318c384020SXin LIoption.
232ac3ea910SXin LI.It Fl T Ar toskeyword
233ac3ea910SXin LIChange IPv4 TOS value.
234ac3ea910SXin LI.Ar toskeyword
235ac3ea910SXin LImay be one of
236ac3ea910SXin LI.Ar critical ,
237ac3ea910SXin LI.Ar inetcontrol ,
238ac3ea910SXin LI.Ar lowdelay ,
239ac3ea910SXin LI.Ar netcontrol ,
240ac3ea910SXin LI.Ar throughput ,
241ac3ea910SXin LI.Ar reliability ,
242ac3ea910SXin LIor one of the DiffServ Code Points:
243ac3ea910SXin LI.Ar ef ,
244ac3ea910SXin LI.Ar af11 ... af43 ,
245ac3ea910SXin LI.Ar cs0 ... cs7 ;
246ac3ea910SXin LIor a number in either hex or decimal.
2478c384020SXin LI.It Fl t
2488c384020SXin LICauses
2498c384020SXin LI.Nm
2508c384020SXin LIto send RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL requests.
2518c384020SXin LIThis makes it possible to use
2528c384020SXin LI.Nm
2538c384020SXin LIto script telnet sessions.
254cef7ab70STom Jones.It Fl -tun Ar tundev
255cef7ab70STom JonesCauses
256cef7ab70STom Jones.Nm
257cef7ab70STom Jonesto use the provided
258cef7ab70STom Jones.Xr tun 4
259cef7ab70STom Jonesfor input and output rather than the default of stdin and stdout.
2608c384020SXin LI.It Fl U
26126bf9c3bSXin LISpecifies to use
26226bf9c3bSXin LI.Ux Ns -domain
26326bf9c3bSXin LIsockets.
2648c384020SXin LI.It Fl u
2658c384020SXin LIUse UDP instead of the default option of TCP.
26659c7ad52SXin LIFor
26759c7ad52SXin LI.Ux Ns -domain
26859c7ad52SXin LIsockets, use a datagram socket instead of a stream socket.
26959c7ad52SXin LIIf a
27059c7ad52SXin LI.Ux Ns -domain
27159c7ad52SXin LIsocket is used, a temporary receiving socket is created in
27259c7ad52SXin LI.Pa /tmp
27359c7ad52SXin LIunless the
27459c7ad52SXin LI.Fl s
27559c7ad52SXin LIflag is given.
2764f2bbc00SXin LI.It Fl V Ar rtable
2774f2bbc00SXin LISet the routing table
2784f2bbc00SXin LI.Pq Dq FIB
2794f2bbc00SXin LIto be used.
2808c384020SXin LI.It Fl v
2818c384020SXin LIHave
2828c384020SXin LI.Nm
2838c384020SXin LIgive more verbose output.
2848c384020SXin LI.It Fl w Ar timeout
285ac3ea910SXin LIConnections which cannot be established or are idle timeout after
2868c384020SXin LI.Ar timeout
287ac3ea910SXin LIseconds.
2888c384020SXin LIThe
2898c384020SXin LI.Fl w
2908c384020SXin LIflag has no effect on the
2918c384020SXin LI.Fl l
2928c384020SXin LIoption, i.e.\&
2938c384020SXin LI.Nm
2948c384020SXin LIwill listen forever for a connection, with or without the
2958c384020SXin LI.Fl w
2968c384020SXin LIflag.
2978c384020SXin LIThe default is no timeout.
2982440a458SXin LI.It Fl X Ar proxy_protocol
2998c384020SXin LIRequests that
3008c384020SXin LI.Nm
3018c384020SXin LIshould use the specified protocol when talking to the proxy server.
3028c384020SXin LISupported protocols are
3038c384020SXin LI.Dq 4
3048c384020SXin LI(SOCKS v.4),
3058c384020SXin LI.Dq 5
3068c384020SXin LI(SOCKS v.5)
3078c384020SXin LIand
3088c384020SXin LI.Dq connect
3098c384020SXin LI(HTTPS proxy).
3108c384020SXin LIIf the protocol is not specified, SOCKS version 5 is used.
3118c384020SXin LI.It Xo
3128c384020SXin LI.Fl x Ar proxy_address Ns Oo : Ns
3138c384020SXin LI.Ar port Oc
3148c384020SXin LI.Xc
3158c384020SXin LIRequests that
3168c384020SXin LI.Nm
3178c384020SXin LIshould connect to
31859c7ad52SXin LI.Ar destination
3198c384020SXin LIusing a proxy at
3208c384020SXin LI.Ar proxy_address
3218c384020SXin LIand
3228c384020SXin LI.Ar port .
3238c384020SXin LIIf
3248c384020SXin LI.Ar port
3258c384020SXin LIis not specified, the well-known port for the proxy protocol is used (1080
3268c384020SXin LIfor SOCKS, 3128 for HTTPS).
3278c384020SXin LI.It Fl z
3288c384020SXin LISpecifies that
3298c384020SXin LI.Nm
3308c384020SXin LIshould just scan for listening daemons, without sending any data to them.
3318c384020SXin LIIt is an error to use this option in conjunction with the
3328c384020SXin LI.Fl l
3338c384020SXin LIoption.
3348c384020SXin LI.El
3358c384020SXin LI.Pp
33659c7ad52SXin LI.Ar destination
3378c384020SXin LIcan be a numerical IP address or a symbolic hostname
3388c384020SXin LI(unless the
3398c384020SXin LI.Fl n
3408c384020SXin LIoption is given).
34159c7ad52SXin LIIn general, a destination must be specified,
3428c384020SXin LIunless the
3438c384020SXin LI.Fl l
3448c384020SXin LIoption is given
3458c384020SXin LI(in which case the local host is used).
34659c7ad52SXin LIFor
34759c7ad52SXin LI.Ux Ns -domain
34859c7ad52SXin LIsockets, a destination is required and is the socket path to connect to
34959c7ad52SXin LI(or listen on if the
35059c7ad52SXin LI.Fl l
35159c7ad52SXin LIoption is given).
3528c384020SXin LI.Pp
3531a9dc239SXin LI.Ar port
3541a9dc239SXin LIcan be a single integer or a range of ports.
3558c384020SXin LIRanges are in the form nn-mm.
3568c384020SXin LIIn general,
3578c384020SXin LIa destination port must be specified,
3588c384020SXin LIunless the
3598c384020SXin LI.Fl U
36059c7ad52SXin LIoption is given.
3618c384020SXin LI.Sh CLIENT/SERVER MODEL
3628c384020SXin LIIt is quite simple to build a very basic client/server model using
3638c384020SXin LI.Nm .
3648c384020SXin LIOn one console, start
3658c384020SXin LI.Nm
3668c384020SXin LIlistening on a specific port for a connection.
3678c384020SXin LIFor example:
3688c384020SXin LI.Pp
3698c384020SXin LI.Dl $ nc -l 1234
3708c384020SXin LI.Pp
3718c384020SXin LI.Nm
3728c384020SXin LIis now listening on port 1234 for a connection.
3738c384020SXin LIOn a second console
3748c384020SXin LI.Pq or a second machine ,
3758c384020SXin LIconnect to the machine and port being listened on:
3768c384020SXin LI.Pp
3778c384020SXin LI.Dl $ nc 127.0.0.1 1234
3788c384020SXin LI.Pp
3798c384020SXin LIThere should now be a connection between the ports.
3808c384020SXin LIAnything typed at the second console will be concatenated to the first,
3818c384020SXin LIand vice-versa.
3828c384020SXin LIAfter the connection has been set up,
3838c384020SXin LI.Nm
3848c384020SXin LIdoes not really care which side is being used as a
3858c384020SXin LI.Sq server
3868c384020SXin LIand which side is being used as a
3878c384020SXin LI.Sq client .
3888c384020SXin LIThe connection may be terminated using an
3898c384020SXin LI.Dv EOF
3908c384020SXin LI.Pq Sq ^D .
3918c384020SXin LI.Sh DATA TRANSFER
3928c384020SXin LIThe example in the previous section can be expanded to build a
3938c384020SXin LIbasic data transfer model.
3948c384020SXin LIAny information input into one end of the connection will be output
3958c384020SXin LIto the other end, and input and output can be easily captured in order to
3968c384020SXin LIemulate file transfer.
3978c384020SXin LI.Pp
3988c384020SXin LIStart by using
3998c384020SXin LI.Nm
4008c384020SXin LIto listen on a specific port, with output captured into a file:
4018c384020SXin LI.Pp
4028c384020SXin LI.Dl $ nc -l 1234 \*(Gt filename.out
4038c384020SXin LI.Pp
4048c384020SXin LIUsing a second machine, connect to the listening
4058c384020SXin LI.Nm
4068c384020SXin LIprocess, feeding it the file which is to be transferred:
4078c384020SXin LI.Pp
4083ae0125aSXin LI.Dl $ nc -N host.example.com 1234 \*(Lt filename.in
4098c384020SXin LI.Pp
4108c384020SXin LIAfter the file has been transferred, the connection will close automatically.
4118c384020SXin LI.Sh TALKING TO SERVERS
4128c384020SXin LIIt is sometimes useful to talk to servers
4138c384020SXin LI.Dq by hand
4148c384020SXin LIrather than through a user interface.
4158c384020SXin LIIt can aid in troubleshooting,
4168c384020SXin LIwhen it might be necessary to verify what data a server is sending
4178c384020SXin LIin response to commands issued by the client.
4188c384020SXin LIFor example, to retrieve the home page of a web site:
4192440a458SXin LI.Bd -literal -offset indent
420bc694058SBenedict Reuschling$ printf "GET / HTTP/1.0\er\en\er\en" | nc host.example.com 80
4212440a458SXin LI.Ed
4228c384020SXin LI.Pp
4238c384020SXin LINote that this also displays the headers sent by the web server.
4248c384020SXin LIThey can be filtered, using a tool such as
4258c384020SXin LI.Xr sed 1 ,
4268c384020SXin LIif necessary.
4278c384020SXin LI.Pp
4288c384020SXin LIMore complicated examples can be built up when the user knows the format
4298c384020SXin LIof requests required by the server.
4308c384020SXin LIAs another example, an email may be submitted to an SMTP server using:
4318c384020SXin LI.Bd -literal -offset indent
4328c384020SXin LI$ nc localhost 25 \*(Lt\*(Lt EOF
4338c384020SXin LIHELO host.example.com
4348c384020SXin LIMAIL FROM:\*(Ltuser@host.example.com\*(Gt
4358c384020SXin LIRCPT TO:\*(Ltuser2@host.example.com\*(Gt
4368c384020SXin LIDATA
4378c384020SXin LIBody of email.
4388c384020SXin LI\&.
4398c384020SXin LIQUIT
4408c384020SXin LIEOF
4418c384020SXin LI.Ed
4428c384020SXin LI.Sh PORT SCANNING
4438c384020SXin LIIt may be useful to know which ports are open and running services on
4448c384020SXin LIa target machine.
4458c384020SXin LIThe
4468c384020SXin LI.Fl z
4478c384020SXin LIflag can be used to tell
4488c384020SXin LI.Nm
4492440a458SXin LIto report open ports,
4502440a458SXin LIrather than initiate a connection.
4518c384020SXin LIFor example:
4528c384020SXin LI.Bd -literal -offset indent
4532440a458SXin LI$ nc -z host.example.com 20-30
4548c384020SXin LIConnection to host.example.com 22 port [tcp/ssh] succeeded!
4558c384020SXin LIConnection to host.example.com 25 port [tcp/smtp] succeeded!
4568c384020SXin LI.Ed
4578c384020SXin LI.Pp
4588c384020SXin LIThe port range was specified to limit the search to ports 20 \- 30.
4598c384020SXin LI.Pp
4608c384020SXin LIAlternatively, it might be useful to know which server software
4618c384020SXin LIis running, and which versions.
4628c384020SXin LIThis information is often contained within the greeting banners.
4638c384020SXin LIIn order to retrieve these, it is necessary to first make a connection,
4648c384020SXin LIand then break the connection when the banner has been retrieved.
4658c384020SXin LIThis can be accomplished by specifying a small timeout with the
4668c384020SXin LI.Fl w
4678c384020SXin LIflag, or perhaps by issuing a
4688c384020SXin LI.Qq Dv QUIT
4698c384020SXin LIcommand to the server:
4708c384020SXin LI.Bd -literal -offset indent
4718c384020SXin LI$ echo "QUIT" | nc host.example.com 20-30
4728c384020SXin LISSH-1.99-OpenSSH_3.6.1p2
4738c384020SXin LIProtocol mismatch.
4748c384020SXin LI220 host.example.com IMS SMTP Receiver Version 0.84 Ready
4758c384020SXin LI.Ed
4768c384020SXin LI.Sh EXAMPLES
4772440a458SXin LIOpen a TCP connection to port 42 of host.example.com, using port 31337 as
4788c384020SXin LIthe source port, with a timeout of 5 seconds:
4798c384020SXin LI.Pp
4802440a458SXin LI.Dl $ nc -p 31337 -w 5 host.example.com 42
4818c384020SXin LI.Pp
4822440a458SXin LIOpen a UDP connection to port 53 of host.example.com:
4838c384020SXin LI.Pp
4842440a458SXin LI.Dl $ nc -u host.example.com 53
4858c384020SXin LI.Pp
4862440a458SXin LIOpen a TCP connection to port 42 of host.example.com using 10.1.2.3 as the
4878c384020SXin LIIP for the local end of the connection:
4884bdc2338SRuslan Ermilov.Pp
4892440a458SXin LI.Dl $ nc -s 10.1.2.3 host.example.com 42
4904bdc2338SRuslan Ermilov.Pp
4912440a458SXin LIOpen a TCP connection to port 42 of host.example.com using IPsec ESP for
49204def624SXin LIincoming and outgoing traffic.
4934bdc2338SRuslan Ermilov.Pp
4942440a458SXin LI.Dl $ nc -E host.example.com 42
4954bdc2338SRuslan Ermilov.Pp
4962440a458SXin LIOpen a TCP connection to port 42 of host.example.com using IPsec ESP for
49704def624SXin LIoutgoing traffic only.
4988c384020SXin LI.Pp
4992440a458SXin LI.Dl $ nc -e 'out ipsec esp/transport//require' host.example.com 42
5008c384020SXin LI.Pp
50126bf9c3bSXin LICreate and listen on a
50226bf9c3bSXin LI.Ux Ns -domain
50359c7ad52SXin LIstream socket:
5048c384020SXin LI.Pp
5058c384020SXin LI.Dl $ nc -lU /var/tmp/dsocket
5068c384020SXin LI.Pp
5072440a458SXin LIConnect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4,
5082440a458SXin LIport 8080.
5092440a458SXin LIThis example could also be used by
5102440a458SXin LI.Xr ssh 1 ;
5112440a458SXin LIsee the
5122440a458SXin LI.Cm ProxyCommand
5132440a458SXin LIdirective in
5142440a458SXin LI.Xr ssh_config 5
5152440a458SXin LIfor more information.
5168c384020SXin LI.Pp
5172440a458SXin LI.Dl $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42
5182440a458SXin LI.Pp
5192440a458SXin LIThe same example again, this time enabling proxy authentication with username
5202440a458SXin LI.Dq ruser
5212440a458SXin LIif the proxy requires it:
5222440a458SXin LI.Pp
5232440a458SXin LI.Dl $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42
524c9b4549cSXin LI.Sh EXIT STATUS
525c9b4549cSXin LI.Ex -std
5268c384020SXin LI.Sh SEE ALSO
5272440a458SXin LI.Xr cat 1 ,
5282d5ea05aSXin LI.Xr setfib 1 ,
529bc38f653SBruce M Simpson.Xr ssh 1 ,
530bc38f653SBruce M Simpson.Xr tcp 4
5318c384020SXin LI.Sh AUTHORS
5328c384020SXin LIOriginal implementation by *Hobbit*
5335abd6fdcSXin LI.Aq Mt hobbit@avian.org .
5348c384020SXin LI.br
5358c384020SXin LIRewritten with IPv6 support by
5365abd6fdcSXin LI.An Eric Jackson Aq Mt ericj@monkey.org .
5372440a458SXin LI.Sh CAVEATS
538ac3ea910SXin LIUDP port scans using the
5392440a458SXin LI.Fl uz
540ac3ea910SXin LIcombination of flags will always report success irrespective of
541ac3ea910SXin LIthe target machine's state.
542ac3ea910SXin LIHowever,
543ac3ea910SXin LIin conjunction with a traffic sniffer either on the target machine
544ac3ea910SXin LIor an intermediary device,
545ac3ea910SXin LIthe
546ac3ea910SXin LI.Fl uz
547ac3ea910SXin LIcombination could be useful for communications diagnostics.
548ac3ea910SXin LINote that the amount of UDP traffic generated may be limited either
549ac3ea910SXin LIdue to hardware resources and/or configuration settings.
550