Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of netcat Command in Linux system

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

The content of this article is mainly around the Linux system netcat command example analysis, the article content is clear and easy to understand, organized, very suitable for beginners to learn, worth reading. Interested friends can read along with Xiaobian. I hope everyone gets something out of this article!

netcat is the Swiss Army knife of networking tools, which can read and write data over TCP and UDP. What netcat does is it creates a link between two computers and returns two streams of data, by combining and redirecting them with other tools.

Using Netcatnetcat tools on server-client architectures runs in server mode, listening on specified ports

$ nc -l 2389 Then you can use client mode to connect to port 2389:

$ nc localhost2389 Now if you type some text, it will be sent to the server side:

$ nc localhost 2389 HI, oschina The terminal window on the server will display the following:

$ nc -l 2389 HI, oschina Use Netcat to transfer files The netcat tool can also be used to transfer files. On the client side, suppose we have a testfile file:

$ cat testfile hello ochina and there is an empty file named test on the server side

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

$ nc -l 2389 > test Immediately after running the client:

cat testfile |nc localhost2389 Then you stop the server, you can check the test content is just passed by the client testfile content:

$ cat test hello oschinaNetcat supports timeout control Most of the time we don't want the connection to last, we can use the-w parameter to specify the idle timeout for the connection, which is followed by a number 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 localhost2389 The connection will be disconnected in 10 seconds.

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

Netcat supports IPv6Netcat parameters-4 and-6 are used to specify IP address types, IPv4 and IPv6 respectively:

Server side:

$ nc -4 -l 2389 Client:

$ nc -4 localhost 2389 Then we can use the netstat command to see what's going on with the network:

$ netstat | grep 2389 tcp 0 0 localhost:2389 localhost:50851 ESTABLISHED tcp 0 0 localhost:50851 localhost:2389 ESTABLISHED Next let's look at IPv6:

Server side:

$ nc -6 -l 2389 Client:

$ nc -6 localhost 2389 Run netstat again:

$ netstat | grep 2389 tcp6 0 0 localhost:2389 localhost:33234 ESTABLISHED tcp6 0 0 localhost:33234 localhost:2389 ESTABLISHED is prefixed with tcp6 to indicate that IPv6 addresses are used.

Disables reading data from standard input in Netcat. This function uses the-d parameter. See the following example:

Server side:

$ nc -l 2389 Client:

$ nc -d localhost 2389 Hi The Hi text you type is not sent to the server.

Force Netcat server side to stay up If a client connected to the server disconnects, the server side will exit as well.

Server side:

$ nc -l 2389 Client:

$ nc localhost 2389 ^C Server side:

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

We can control this by using the-k parameter so that the server does not quit due to client disconnections.

Server side:

$ nc -k -l 2389 Client:

$ nc localhost 2389 ^C Server side:

$ nc -k -l 2389

Configure Netcat client not to exit due to EOF Netcat client can control how long it takes to exit after receiving EOF through-q parameter, the unit of this parameter is seconds:

The client starts as follows:

nc -q 5 localhost 2389 Now if the client receives EOF, it waits 5 seconds before exiting.

Netcat uses TCP by default, but it also supports UDP, which can be enabled with the-u parameter.

Server side:

$ nc -4 -u -l 2389 Client:

$ nc -4 -u localhost2389 So both client and server use UDP protocol, which can be seen by netstat command:

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

Thank you for reading, I believe you have a certain understanding of the "Linux netcat command example analysis" this issue, go to practice it quickly, if you want to know more related knowledge points, you can pay attention to the website! The editor will continue to bring better articles to everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report