Original article link
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.
$ Nc-l 2389
Then you can connect to port 2389 in client mode:
$ NC localhost 2389
Now, if you enter some text, it will be sent to the server:
$ NC localhost 2389
Hi, oschina
The following content is displayed in the terminal window of the server:
$ Nc-l 2389
Hi, oschina
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:
$Cat Testfile
Hello oschina
On the server side, there is an empty file named test.
Run the following command to enable the server:
$ Nc-l 2389>Test
Then run the client:
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:
$Cat Test
Hello oschina
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:
NC-l 2389
Client:
$ 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:
$ Nc-4-l 2389
Client:
$ Nc-4 localhost 2389
Then we can use the netstat command to view the network conditions:
$Netstat |Grep 2389
TCP 0 0 localhost: 2389 localhost: 50851 established
TCP 0 0 localhost: 50851 localhost: 2389 established
Next, let's look at the IPv6 situation:
Server:
$ Nc-6-l 2389
Client:
$ Nc-6 local host 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
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:
$ Nc-l 2389
Client:
$ Nc-D localhost 2389
Hi
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:
$ Nc-l 2389
Client:
$ NC localhost 2389
^ C
Server:
$ Nc-l 2389
$
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:
$ Nc-K-l 2389
Client:
$ NC localhost 2389
^ C
Server:
$ Nc-K-l 2389
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:
NC-Q 5 local host 2389
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:
$ 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 using the netstat command:
$Netstat |Grep 2389
UDP 0 0 localhost: 42634 localhost: 2389 established