8 Practical examples of Linux netcat commands

Source: Internet
Author: User

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

?
1 $ nc-l 2389

You can then use client mode to connect to port 2389:

?
1 $ nclocalhost 2389

Now if you enter some text, it will be sent to the server side:

?
12 $ nclocalhost 2389HI, oschina

The following is displayed in the terminal window of the server:

?
12 $ nc-l 2389HI, 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 $ cattestfilehello oschina

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

We then use the following command to enable the server side:

?
1 $ nc-l 2389 > test

Run the client immediately:

?
1 cattestfile | nclocalhost 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 $ cattesthello 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:

?
1 nc-l 2389

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:

?
1 $ nc-4 -l 2389

Client:

?
1 $ nc-4 localhost 2389

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

?
123 $ netstat| grep2389tcp        0      0 localhost:2389          localhost:50851         ESTABLISHEDtcp        0      0 localhost:50851         localhost:2389          ESTABLISHED

Next we look at the IPV6:

Server-side:

?
1 $ nc-6 -l 2389

Client:

?
1 $ nc-6 localhost 2389

Run the netstat command again:

?
123 $ netstat| grep2389tcp6       0      0 localhost:2389          localhost:33234         ESTABLISHEDtcp6       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:

?
1 $ nc-l 2389

Client:

?
12 $ nc-d localhost 2389Hi

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:

?
1 $ nc-l 2389

Client:

?
12 $ nclocalhost 2389^C

Server-side:

?
12 $ nc-l 2389$

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:

?
1 $ nc-k -l 2389

Client:

?
12 $ nclocalhost 2389^C

Server-side:

?
1 $ nc-k -l 2389

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:

?
1 nc-q 5  localhost 2389

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:

?
1 $ nc-4 -u -l 2389

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| grep2389udp        0      0 localhost:42634         localhost:2389          ESTABLISHED

8 Practical examples of Linux netcat commands

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.