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

How to use the nc command in linux

2025-02-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to use the nc command in linux. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Check if the port is occupied

Remember how to see how ports are occupied in netstat? nc can do the same thing. It uses the-l (listen) parameter to bind the specified port and listen for connections:

$nc-l 6379nc: Address already in use

When you see Address already in use, you know that this port is occupied, otherwise it will listen on this port. Of course, I'm talking nonsense about this effect, but it can be used.

Port scan

For example, you need to scan which ports of 21-30 on a machine are open:

$nc-n 127.0.0.1-z 1230-1234-v nc: connect to 127.0.0.1 port 1230 (tcp) failed: Connection refused nc: connect to 127.0.0.1 port 1231 (tcp) failed: Connection refused nc: connect to 127.0.0.1 port 1232 (tcp) failed: Connection refused nc: connect to 127.0.0.1 port 1233 (tcp) failed: Connection refused Connection to 127.0.0.1 1234 port [tcp/*] succeeded!

Where-n indicates that the ip address is directly used instead of the domain name,-z specifies the port range, and-v outputs details. As you can see from the results, port 1234 is connectable.

TCP/UDP connection testing

When you are learning about TCP, do you think about how to observe the status or packets in it? Although it is recommended to write a server-client program for a TCP connection, you can also use the nc command to do this, for example, enter the following in a terminal:

$nc-l 1234 hello programming

Indicates that you are listening at port 1234, and then you can connect at another terminal.

$nc 127.0.0.1 1234 hello programming Zhuji

Connect the local address port 1234, and during this process, you can grab the packet and analyze the three-way handshake of TCP.

In addition, you can also send messages (simple chat function) between established connections, allowing you to analyze more!

By default, it creates a TCP connection, and the-u (UDP) parameter allows you to test UDP connectivity.

$nc-v-u 182.3.226.35 80 Connection to 182.3.226.35 80 port [udp/http] succeeded!

In addition, there are many parameters for debugging, such as:

-b allow broadcast messages

-D turn on socket debug mode

-d prohibit reading from standard input

Keep the connection after the-k client exits

-v display details

Since it will be explained with the help of nc in a later article, it will not be explained in detail here.

HTTP connection testing

For example, check the HTTP header information:

$nc www.baidu.com 80 HEAD / HTTP/1.1 HTTP/1.1 302 Found Connection: Keep-Alive Content-Length: 17931 Content-Type: text/html Date: Sun, 23 Jun 2019 13:52:12 GMT Etag: "54d9748e-460b" Server: bfe/1.0.8.18

After connecting, enter HEAD / HTTP/1.1 or HEAD / HTTP/1.0, and then enter two carriage returns to view information about the web server.

Data transmission

Or using the previous connection, you can use the redirector to transfer the file, such as server listening, and output the content to out.txt:

$nc-l 1234 > out.txt

While the client connection:

$nc 127.0.0.1 1234

< in.txt 这样客户端in.txt的内容,就会传输到out.txt 关于重定向,可参考《如何linux shell中的2 >

& 1 "

Of course, for file transfer, the scp command does this better. If you have no other way, you can try nc.

Network testing

As mentioned earlier, data can be transferred through nc, and if we want to test the network speed between two hosts (of course, you can use the iperf tool to do this), nc can also help:

# Server listening: $nc-vl 1234 > / dev/null

Where redirect to / dev/null means that the data will be discarded.

Then execute on another host:

$dd if=/dev/zero bs=1M count=10 | nc-vn 127.0.0.1 1234 Connection to 127.0.0.1 1234 port [tcp/*] succeeded! 10 0 records in 10 0 records out 10485760 bytes (10 MB, 10 MiB) copied, 0.0333675 s, 314 MB/s

Dd copies the data, where it is copied from / dev/zero 0, 1m at a time, 10 times, and finally sent to the server through the nc command. When it's over, you can see the statistics.

This is the end of the article on "how to use nc commands in linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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

Servers

Wechat

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

12
Report