Netcat or NC is one of the Linux tools for debugging and checking the Network toolkit. Can be used to create TCP/IP connections, and the biggest use is to handle TCP/UDP sockets.
Here we will use some examples to learn the Netcat command.
1. Using Netcat on the server-client architecture
The Netcat tool can run in server mode and listen on a specified port
?
You can then use client mode to connect to port 2389:
?
Now if you enter some text, it will be sent to the server side:
?
12 |
$ nc localhost 2389 HI, oschina |
The following is displayed in the terminal window of the server:
?
12 |
$ nc -l 2389 HI, oschina |
2. Using Netcat to transfer files
The Netcat tool can also be used to transfer files on the client, assuming we have a testfile file:
?
12 |
$ cat testfile hello oschina |
And on the server side there is an empty file named Test
We then use the following command to enable the server side:
?
Run the client immediately:
?
1 |
cat testfile | nc localhost 2389 |
Then you stop the server side, you can view the test content is the content of the Testfile file sent by the client just now:
?
12 |
$ cat test hello oschina |
3. Netcat Support Timeout control
Most of the time we don't want the connection to remain, so we can use the-w parameter to specify the idle timeout for the connection, which is immediately a numeric value that represents the number of seconds, and the connection is terminated if the connection exceeds the specified time.
Server:
?
Client:
?
1 |
$ nc -w 10 localhost 2389 |
The connection will be interrupted after 10 seconds.
Note: Do not use the-W and-L parameters on the server side, because the-w parameter will have no effect on the server side.
4. Netcat Support IPV6
The Netcat-4 and 6 parameters are used to specify the IP address type, respectively, IPV4 and IPV6:
Server-side:
?
Client:
?
Then we can use the Netstat command to view the network situation:
?
123 |
$ 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:
Server-side:
?
Client:
?
Run the netstat command again:
?
123 |
$ netstat | grep 2389 tcp6 0 0 localhost:2389 localhost:33234 ESTABLISHED tcp6 0 0 localhost:33234 localhost:2389 ESTABLISHED |
The prefix is TCP6, which indicates that the address used is IPv6.
5. Prohibit reading data from standard input in Netcat
This feature uses the-d parameter, see the following example:
Server-side:
?
Client:
?
12 |
$ nc -d localhost 2389 Hi |
The Hi text you entered will not be sent to the server side.
6. Force the NETCAT server side to remain in the boot state
If the client connecting to the server disconnects, the server side also exits.
Server-side:
?
Client:
?
Server-side:
?
In the above example, the server side also exits immediately when the client disconnects.
We can use the-K parameter to control that the server does not exit because the client disconnects.
Server-side:
?
Client:
?
Server-side:
?
7. Configuring the Netcat client does not exit because of EOF
The Netcat client can use the-Q parameter to control how long it takes to exit after EOF is received, in seconds:
The client launches using the following method:
?
Now if the client receives EOF, it waits 5 seconds to exit.
8. Using Netcat to Process UDP protocol
The netcat default is to use the TCP protocol, but it also supports UDP, and you can use the-u parameter to enable UDP protocol traffic.
Server-side:
?
Client:
?
1 |
$ nc -4 -u localhost 2389 |
This allows both the client and server to use the UDP protocol, which can be viewed through the netstat command:
?
12 |
$ netstat | grep 2389 udp 0 0 localhost:42634 localhost:2389 ESTABLISHED |
8 Practical examples of Linux netcat commands