8 practical examples of netcat command

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

8 practical examples of netcat command

Netcat or NC is a toolkit for debugging and checking the network under Linux. Can be used to create a TCP/IP connection, the biggest use is used to process the TCP/UDP socket.

Here we will through some examples to learn netcat commands.

1 in the server client architecture using Netcat

The netcat tool can be run on the server mode, listen to the specified port

$ nc -l 2389

Then you can use client mode to connect to port 2389:

$ nc localhost 2389

Now if you enter some text, it will be sent to the server:

$ nc localhost 2389
HI, server

In the server terminal window will display the following contents:

$ nc -l 2389
HI, server

2 using Netcat to transfer files

The netcat tool can be used to transfer files, the client, assume we have a testfile file:

$ cat testfile
hello server

On the server side of an empty file named test

Then we use the following command to enable the server:

$ nc -l 2389 > test

Then run the client:

$ cat testfile | nc localhost 2389

And then you stop the server, you can view the test content is just the client pass over the contents of the testfile file:

$ cat test
hello server

3 Netcat timeout control

Most of the time we don't want the connection remains, then we can use the -w parameter to specify connection idle timeout, the parameter followed by a number value, represent the number of seconds, if the connection is longer than the specified time the connection will be terminated.

The server:

$ nc -l 2389

The client:

$ nc -w 10 localhost 2389

The connection will be interrupted in 10 seconds.

Note: the -w and -l parameters at the same time, do not use on the server, because the -w parameter on the server without effect.

4 Netcat IPv6

Netcat -4 and the -6 parameter is used to specify the IP address type is IPv4 and IPv6, respectively.:

The server:

$ nc -4 -l 2389

The client:

$ nc -4 localhost 2389

Then we can use the netstat command to view the network situation:

$ netstat | grep 2389
tcp        0      0 localhost:2389          localhost:50851         ESTABLISHED
tcp        0      0 localhost:50851         localhost:2389          ESTABLISHED

Next we have a look IPv6.:

The server:

$ nc -6 -l 2389

The client:

$ nc -6 localhost 2389

Run the netstat command again:

$ netstat | grep 2389
tcp6       0      0 localhost:2389          localhost:33234         ESTABLISHED
tcp6       0      0 localhost:33234         localhost:2389          ESTABLISHED

The prefix is used for tcp6 representation is the address of the IPv6.

5 in the Netcat forbidden to read data from the standard input

This function uses the -d parameter, see the following example:

The server:

$ nc -l 2389

The client:

$ nc -d localhost 2389
Hi

You enter the Hi text and will not be sent to the server.

6 forced Netcat server maintains the start state

If the connection to the server to the client is disconnected, then the server will follow the exit.

The server:

$ nc -l 2389

The client:

$ nc localhost 2389
^C

The server:

$ nc -l 2389
$

In the example above, but the client disconnect server also immediately exit.

We can through the -k parameter to control let the server will not because the client disconnect and exit.

The server:

$ nc -k -l 2389

The client:

$ nc localhost 2389
^C

The server:

$ nc -k -l 2389

7 Netcat client won't quit because of EOF

The Netcat client through the -q parameter to control the receiving EOF every time to exit, the parameters of the unit is seconds:

The client uses the following start:

$ nc  -q 5  localhost 2389

Now if the client receives the EOF, it will wait 5 seconds after exit.

8 using Netcat to deal with UDP protocol

Netcat is the use of TCP protocol by default, but also support UDP, can use the -u parameter to enable UDP protocol communication.

The server:

$ nc -4 -u -l 2389

The client:

$ nc -4 -u localhost 2389

The client and server using UDP protocol, can be viewed through the netstat command:

$ netstat | grep 2389
udp        0      0 localhost:42634         localhost:2389          ESTABLISHED
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Nick at December 22, 2013 - 3:31 AM