Document directory
- 1. Use Netcat in the server-client architecture
- 2. Use Netcat to transfer files
- 3. Netcat supports timeout Control
- 5. Disable reading data from standard input in Netcat
- 6. Force the Netcat server to be started
- 7. configuring the Netcat client will not exit because of EOF
- 8. Use Netcat to process UDP protocol
Netcat or NC is a network tool kit for debugging and checking in Linux. It can be used to create TCP/IP connections. The maximum purpose is to process TCP/UDP sockets.
Here we will use some instances to learn the Netcat command.
1. Use Netcat in the server-client architecture
The Netcat tool runs in server mode and listens to specified ports.
Then you can connect to port 2389 in client mode:
Now, if you enter some text, it will be sent to the server:
The following content is displayed in the terminal window of the server:
2. Use Netcat to transfer files
The Netcat tool can also be used to transmit files. on the client side, assume that we have a testfile file:
On the server side, there is an empty file named test.
Run the following command to enable the server:
Then run the client:
1 |
cat testfile | nc localhost 2389 |
Then you stop the server. You can check that the content of test is the content of the testfile File Uploaded by the client just now:
3. Netcat supports timeout Control
In most cases, we do not want to keep the connection. We can use the-W parameter to specify the idle timeout time for the connection. this parameter is followed by a value, which indicates the number of seconds, if the connection exceeds the specified time, the connection will be terminated.
Server:
Client:
1 |
$ nc -w 10 localhost 2389 |
The connection will be interrupted in 10 seconds.
Note: Do not use the-W and-l parameters on the server, because the-W parameter will have no effect on the server.
4. Netcat supports IPv6
The-4 and-6 parameters of Netcat are used to specify the IP address type, namely IPv4 and IPv6:
Server:
Client:
Then we can use the netstat command to view the network conditions:
2 |
tcp 0 0 localhost:2389 localhost:50851 ESTABLISHED |
3 |
tcp 0 0 localhost:50851 localhost:2389 ESTABLISHED |
Next, let's look at the IPv6 situation:
Server:
Client:
Run the netstat command again:
2 |
tcp6 0 0 localhost:2389 localhost:33234 ESTABLISHED |
3 |
tcp6 0 0 localhost:33234 localhost:2389 ESTABLISHED |
If the prefix is tcp6, the IPv6 address is used.
5. Disable reading data from standard input in Netcat
This function uses the-D parameter. See the following example:
Server:
Client:
The Hi text you entered will not be sent to the server.
6. Force the Netcat server to be started
If the client connected to the server is disconnected, the server also exits.
Server:
Client:
Server:
In the preceding example, the server exits immediately when the client is disconnected.
We can use the-k parameter to control the server from exiting due to client disconnection.
Server:
Client:
Server:
7. configuring the Netcat client will not exit because of EOF
The Netcat client can use the-Q Parameter to control how long it will exit after receiving the EOF. The unit of this parameter is seconds:
Start the client as follows:
Now, if the client receives the EOF, it will wait 5 seconds before exiting.
8. Use Netcat to process UDP protocol
Netcat uses the TCP protocol by default, but also supports UDP. You can use the-u parameter to enable UDP communication.
Server:
Client:
1 |
$ nc -4 -u localhost 2389 |
In this way, both the client and the server use the UDP protocol, which can be viewed using the netstat command:
2 |
udp 0 0 localhost:42634 localhost:2389 |