The
netcat
(or
netcat
utility is used for just about anything under the sun involving TCP
or UDP.
It can open TCP connections, send UDP packets, listen on arbitrary
TCP and UDP ports, do port scanning, and deal with both IPv4 and
IPv6.
Unlike
telnet(1),
netcat
scripts nicely, and separates error messages onto standard error instead
of sending them to standard output, as
telnet(1),
does with some.
Destination ports can be single integers or ranges.
Ranges are in the form nn-mm.
Common uses include:
simple TCP proxies
shell-script based HTTP clients and servers
network daemon testing
and much, much more
The options are as follows:
-4
Forces
netcat
to use IPv4 addresses only.
-6
Forces
netcat
to use IPv6 addresses only.
-h
Prints out
netcat
help.
-i interval
Specifies a delay time interval between lines of text sent and received.
Also causes a delay time between connections to multiple ports.
-k
Forces
netcat
to stay listening for another connection after its current connection
is completed.
-l
Used to specify that
netcat
should listen for an incoming connection rather than initiate a
connection to a remote host.
-n
Do not do any DNS or service lookups on any specified addresses,
hostnames or ports.
-p port
Specifies the source port
netcat
should use, subject to privilege restrictions and availability.
-r
Specifies that source and/or destination ports should be chosen randomly
instead of sequentially within a range or in the order that the system
assigns them.
-s hostname/ip address
Specifies the IP of the interface which is used to send the packets.
-t
Causes
netcat
to send RFC854 DON'T and WON'T responses to RFC854 DO and WILL requests.
This makes it possible to use
netcat
to script telnet sessions.
-u
Use UDP instead of the default option of TCP.
-v
Have
netcat
give more verbose output.
-x proxy address [:port]
Requests that
netcat
should connect to
hostname
using a SOCKS proxy at address and port.
If port is not specified, port 1080 is used.
-z
Specifies that
netcat
should just scan for listening daemons, without sending any data to them.
-U
Specifies to use Unix Domain Sockets.
-X version
Requests that
netcat
should use the specified version of the SOCKS protocol when talking to
a SOCKS proxy.
If version is not specified, SOCKS version 5 is used.
EXAMPLES
$ netcat hostname 42
Open a TCP connection to port 42 of hostname.
$ netcat -p 31337 hostname 42
Open a TCP connection to port 42 of hostname, and use port 31337 as
the source port.
$ netcat -w 5 hostname 42
Open a TCP connection to port 42 of hostname, and timeout after
five seconds while attempting to connect.
$ netcat -u hostname 53
Open a UDP connection to port 53 of hostname.
$ netcat -s 10.1.2.3 example.host 42
Open a TCP connection to port 42 of example.host using 10.1.2.3 as the
IP for the local end of the connection.
$ netcat -v hostname 42
Open a TCP connection to port 42 of hostname, displaying some
diagnostic messages on stderr.
$ netcat -v -z hostname 20-30
Attempt to open TCP connections to ports 20 through 30 of
hostname, and report which ones
netcat
was able to connect to.
$ netcat -v -u -z -w 3 hostname 20-30
Send UDP packets to ports 20-30 of example.host, and report which ones
did not respond with an ICMP packet after three seconds.
$ netcat -l 3000
Listen on TCP port 3000, and once there is a connection, send stdin to
the remote host, and send data from the remote host to stdout.
$ echo foobar | netcat hostname 1000
Connect to port 1000 of hostname, send the string "foobar"
followed by a newline, and move data from port 1000 of hostname to
stdout until hostname closes the connection.