1 .\" $OpenBSD: nc.1,v 1.36 2005/01/07 10:11:31 jmc Exp $
3 .\" Copyright (c) 1996 David Sacerdote
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
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
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.
35 .Nd arbitrary TCP and UDP connections and listens
39 .Op Fl 46DEdhklnorStUuvz
40 .Op Fl e Ar IPsec_policy
42 .Op Fl p Ar source_port
43 .Op Fl s Ar source_ip_address
45 .Op Fl X Ar proxy_protocol
47 .Fl x Ar proxy_address Ns Oo : Ns
51 .Op Ar port Ns Bq Ar s
58 utility is used for just about anything under the sun involving TCP
60 It can open TCP connections, send UDP packets, listen on arbitrary
61 TCP and UDP ports, do port scanning, and deal with both IPv4 and
66 scripts nicely, and separates error messages onto standard error instead
67 of sending them to standard output, as
73 .Bl -bullet -offset indent -compact
77 shell-script based HTTP clients and servers
79 network daemon testing
81 a SOCKS or HTTP ProxyCommand for
87 The options are as follows:
92 to use IPv4 addresses only.
96 to use IPv6 addresses only.
98 Enable debugging on the socket.
100 Do not attempt to read from stdin.
108 .Li "-e 'in ipsec esp/transport//require'"
109 .Li "-e 'out ipsec esp/transport//require'"
111 which enables IPsec ESP transport mode in both
114 If IPsec support is available, then one can specify the IPsec policies
115 to be used using the syntax described in
116 .Xr ipsec_set_policy 3 .
117 This flag can be specified up to two times, as typically one policy for
118 each direction is needed.
120 Specifies a delay time interval between lines of text sent and received.
121 Also causes a delay time between connections to multiple ports.
125 to stay listening for another connection after its current connection
127 It is an error to use this option without the
133 should listen for an incoming connection rather than initiate a
134 connection to a remote host.
135 It is an error to use this option in conjunction with the
141 Additionally, any timeouts specified with the
145 Do not do any DNS or service lookups on any specified addresses,
151 does not terminate on EOF condition on input,
152 but continues until the network side has been closed down.
155 will make it terminate on EOF as well.
156 .It Fl p Ar source_port
157 Specifies the source port
159 should use, subject to privilege restrictions and availability.
160 It is an error to use this option in conjunction with the
164 Specifies that source and/or destination ports should be chosen randomly
165 instead of sequentially within a range or in the order that the system
168 Enables the RFC 2385 TCP MD5 signature option.
169 .It Fl s Ar source_ip_address
170 Specifies the IP of the interface which is used to send the packets.
171 It is an error to use this option in conjunction with the
177 to send RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL requests.
178 This makes it possible to use
180 to script telnet sessions.
182 Specifies to use Unix Domain Sockets.
184 Use UDP instead of the default option of TCP.
188 give more verbose output.
190 If a connection and stdin are idle for more than
192 seconds, then the connection is silently closed.
195 flag has no effect on the
199 will listen forever for a connection, with or without the
202 The default is no timeout.
203 .It Fl X Ar proxy_version
206 should use the specified protocol when talking to the proxy server.
207 Supported protocols are
215 If the protocol is not specified, SOCKS version 5 is used.
217 .Fl x Ar proxy_address Ns Oo : Ns
230 is not specified, the well-known port for the proxy protocol is used (1080
231 for SOCKS, 3128 for HTTPS).
235 should just scan for listening daemons, without sending any data to them.
236 It is an error to use this option in conjunction with the
242 can be a numerical IP address or a symbolic hostname
246 In general, a hostname must be specified,
250 (in which case the local host is used).
253 can be single integers or ranges.
254 Ranges are in the form nn-mm.
256 a destination port must be specified,
260 (in which case a socket must be specified).
261 .Sh CLIENT/SERVER MODEL
262 It is quite simple to build a very basic client/server model using
264 On one console, start
266 listening on a specific port for a connection.
272 is now listening on port 1234 for a connection.
274 .Pq or a second machine ,
275 connect to the machine and port being listened on:
277 .Dl $ nc 127.0.0.1 1234
279 There should now be a connection between the ports.
280 Anything typed at the second console will be concatenated to the first,
282 After the connection has been set up,
284 does not really care which side is being used as a
286 and which side is being used as a
288 The connection may be terminated using an
292 The example in the previous section can be expanded to build a
293 basic data transfer model.
294 Any information input into one end of the connection will be output
295 to the other end, and input and output can be easily captured in order to
296 emulate file transfer.
300 to listen on a specific port, with output captured into a file:
302 .Dl $ nc -l 1234 \*(Gt filename.out
304 Using a second machine, connect to the listening
306 process, feeding it the file which is to be transferred:
308 .Dl $ nc host.example.com 1234 \*(Lt filename.in
310 After the file has been transferred, the connection will close automatically.
311 .Sh TALKING TO SERVERS
312 It is sometimes useful to talk to servers
314 rather than through a user interface.
315 It can aid in troubleshooting,
316 when it might be necessary to verify what data a server is sending
317 in response to commands issued by the client.
318 For example, to retrieve the home page of a web site:
320 .Dl $ echo \&"GET\&" | nc host.example.com 80
322 Note that this also displays the headers sent by the web server.
323 They can be filtered, using a tool such as
327 More complicated examples can be built up when the user knows the format
328 of requests required by the server.
329 As another example, an email may be submitted to an SMTP server using:
330 .Bd -literal -offset indent
331 $ nc localhost 25 \*(Lt\*(Lt EOF
332 HELO host.example.com
333 MAIL FROM: \*(Ltuser@host.example.com\*(Gt
334 RCPT TO: \*(Ltuser2@host.example.com\*(Gt
342 It may be useful to know which ports are open and running services on
346 flag can be used to tell
348 not to initiate a connection,
353 to report open ports.
355 .Bd -literal -offset indent
356 $ nc -vz host.example.com 20-30
357 Connection to host.example.com 22 port [tcp/ssh] succeeded!
358 Connection to host.example.com 25 port [tcp/smtp] succeeded!
361 The port range was specified to limit the search to ports 20 \- 30.
363 Alternatively, it might be useful to know which server software
364 is running, and which versions.
365 This information is often contained within the greeting banners.
366 In order to retrieve these, it is necessary to first make a connection,
367 and then break the connection when the banner has been retrieved.
368 This can be accomplished by specifying a small timeout with the
370 flag, or perhaps by issuing a
372 command to the server:
373 .Bd -literal -offset indent
374 $ echo "QUIT" | nc host.example.com 20-30
375 SSH-1.99-OpenSSH_3.6.1p2
377 220 host.example.com IMS SMTP Receiver Version 0.84 Ready
380 Open a TCP connection to port 42 of hostname, using port 31337 as
381 the source port, with a timeout of 5 seconds:
383 .Dl $ nc -p 31337 -w 5 hostname 42
385 Open a UDP connection to port 53 of hostname:
387 .Dl $ nc -u hostname 53
389 Open a TCP connection to port 42 of example.host using 10.1.2.3 as the
390 IP for the local end of the connection:
392 .Dl "nc -E example.host 42"
394 Open a TCP connection to port 42 of example.host using IPsec ESP for
395 incoming and outgoing traffic.
397 .Dl "nc -e 'out ipsec esp/transport//require' example.host 42"
399 Open a TCP connection to port 42 of example.host using IPsec ESP for
400 outgoing traffic only.
402 .Dl $ nc -s 10.1.2.3 example.host 42
404 Send UDP packets to ports 20-30 of example.host, and report which ones
405 responded with an ICMP packet after three seconds:
407 .Dl $ nc -uvz -w 3 hostname 20-30
409 Create and listen on a Unix Domain Socket:
411 .Dl $ nc -lU /var/tmp/dsocket
413 Connect to port 42 of hostname via an HTTP proxy at 10.2.3.4, port 8080:
415 .Dl $ nc -x10.2.3.4:8080 -Xconnect hostname 42
419 Original implementation by *Hobbit*
420 .Aq hobbit@avian.org .
422 Rewritten with IPv6 support by
423 .An Eric Jackson Aq ericj@monkey.org .