8 Practical Linux netcat Command Examples

Netcat or nc is a Linux toolkit for debugging and checking networks. Can be used to create TCP/IP connections, the biggest use is to handle TCP/UDP sockets.

Here we will learn the netcat command through some examples.

1. Using Netcat on a Server-Client Architecture

The netcat tool can run in server mode, listening on 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 side:

$ nc localhost 2389
HI, oschina

The server's terminal window will display the following:

$ nc -l 2389
HI, oschina

 

2. Use Netcat to transfer files

The netcat tool can also be used to transfer files, on the client side, let's say we have a testfile:

$ cat testfile
hello oschina

And on the server side there is an empty file named test

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

$ nc -l 2389 > test

Then run the client:

cat testfile | nc localhost 2389

Then you stop the server, and you can check that the test content is the content of the testfile file just passed by the client:

$ cat test
hello oschina

 

3. Netcat supports timeout control

In most cases, we don't want the connection to be kept all the time, so we can use the -w parameter to specify the idle timeout period of the connection, which 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 terminated after 10 seconds.

Note: Do not use both the -w and -l parameters on the server side, as the -w parameter will have no effect on the server side.

4. Netcat supports IPv6

The -4 and -6 parameters of netcat are used to specify the IP address type, IPv4 and IPv6 respectively:

Service-Terminal:

$ nc -4 -l 2389

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 look at the IPv6 situation:

Service-Terminal:

$ 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

The prefix is ​​tcp6 to indicate that an IPv6 address is used.

 

5. Disable reading from standard input in Netcat

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

Service-Terminal:

$ nc -l 2389

Client:

$ nc -d localhost 2389
Hi

The Hi text you enter is not sent to the server.

 

6. Force the Netcat server to stay up

If the client connected to the server is disconnected, the server will also exit.

Service-Terminal:

$ nc -l 2389

Client:

$ nc localhost 2389
^C

Service-Terminal:

$ nc -l 2389 
$

In the above example, but the server also exits immediately when the client disconnects.

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

Service-Terminal:

$ nc -k -l 2389

Client:

$ nc localhost 2389
^C

Service-Terminal:

$ nc -k -l 2389

 

7. Configure Netcat client not to exit due to EOF

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

The client is started as follows:

nc  -q 5  localhost 2389

Now if the client receives EOF, it will wait 5 seconds before exiting.

 

8. Use Netcat to handle UDP protocol

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

Service-Terminal:

$ nc -4 -u -l 2389

Client:

$ nc -4 -u localhost 2389

In this way, both the client and the server use the UDP protocol, which can be viewed through the netstat command:

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

Original English , original translation by  OSCHINA

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326257716&siteId=291194637