×
Community Blog Eight Practical Netcat Command Instances

Eight Practical Netcat Command Instances

This article gives eight practical Netcat command instances with examples.

Netcat (NC) is a toolkit for debugging and checking networks under Linux. It can create TCP/IP connections and process TCP/UDP sockets.

Here, we will learn Netcat commands using examples.

1. Use Netcat on the Server – Client Architecture

The Netcat tool can run in server mode and listen to a 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

The following will be displayed in the terminal window of the server:

$ nc -l 2389
HI, server

2. Use Netcat to Transfer Files

The Netcat tool can also be used to transfer files. Let's suppose we have a testfile file on the client side:

$ cat testfile
hello server

There is an empty file named test on the server side.

Then, we use the following command to enable the server side:

$ nc -l 2389 > test

Run the client:

$ cat testfile | nc localhost 2389

Then, you stop the server side, and you can check that the test content is the testfile file sent by the client just now:

$ cat test
hello server

3. Netcat Supports Timeout Control

In most cases, we do not want the connection to be maintained all the time, so we can use the -w parameter to specify the idle timeout period of the connection. This parameter is followed by a value representing the number of seconds. If the connection exceeds the specified time, the connection will be terminated.

Server:

$ nc -l 2389

Client:

$ nc -w 10 localhost 2389

The connection will be interrupted after ten seconds.

Note: Do not use the -w and -l parameters at the same time on the server side because the -w parameter will not affect the server side.

4. Netcat Supports IPv6

The -4 and -6 parameters of Netcat are used to specify IP address types, which are IPv4 and IPv6, respectively:

Server:

$ nc -4 -l 2389

Client:

$ nc -4 localhost 2389

Then, we can use the Netstat command to view the network:

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

Next, let's look at IPv6:

Server:

$ nc -6 -l 2389

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

A prefix of tcp6 indicates an IPv6 address is used.

5. Reading Data from Standard Input Is Prohibited in Netcat

This function uses the -d parameter. Please see the following example:

Server:

$ nc -l 2389

Client:

$ nc -d localhost 2389
Hi

The Hi text you enter will not be sent to the server.

6. Force the Netcat Server to Remain Started

If the client connected to the server disconnects, the server will also exit.

Server:

$ nc -l 2389

Client:

$ nc localhost 2389
^C

Server:

$ nc -l 2389
$

In the preceding examples, the server side also exits immediately when the client is disconnected.

We can use the -k parameter to control the server. It will not exit due to the disconnection of the client.

Server:

$ nc -k -l 2389

Client:

$ nc localhost 2389
^C

Server:

$ nc -k -l 2389

7. Configure the Netcat Client – It Will Not Exit Because of EOF

The Netcat client can use the -q parameter to control how long it takes to exit after receiving the EOF. The unit of this parameter is seconds:

The client starts in the following way:

$ nc  -q 5  localhost 2389

Now, if the client receives the EOF, it will wait five seconds and exit.

8. Use Netcat to Handle the UDP Protocol

Netcat uses TCP protocol by default, but it also supports UDP. You can use the -u parameter to enable UDP protocol communication.

Server:

$ nc -4 -u -l 2389

Client:

$ nc -4 -u localhost 2389

This way, the client and server use the UDP protocol, which can be viewed through the Netstat command:

$ netstat | grep 2389
udp        0      0 localhost:42634         localhost:2389          ESTABLISHED

Disclaimer: This is a translated article from Linux China, all rights reserved to the original author. The views expressed herein are for reference only and don't necessarily represent the official views of Alibaba Cloud.

0 0 0
Share on

Alibaba Cloud Community

1,044 posts | 257 followers

You may also like

Comments