Swiss Army Knife for Linux network troubleshooting
Original: Zhang Xiaofang high-performance server development May 7
nc namely the n- et c AT command, this tool is very useful when troubleshooting network problems, very powerful, they have been referred to as network performance sector, "Swiss Army Knife", the reader is sure to grasp. The default system does not have this command, you need to install it, installation method:
yum install nc
The common usage of the nc command is to simulate a server program to be connected by other clients, or to simulate a client to connect to other servers, and data can be sent and received after the connection. Let's introduce them one by one:
-
Simulate a server program
Use the -l option ( the first letter of the word l isten) to start a listening service on an ip address and port number to allow other clients to connect. Usually in order to display more detailed information, will bring the -v option.
Examples are as follows:
[root@iZ238vnojlyZ ~]# nc -v -l 127.0.0.1 6000 Ncat: Version 6.40 ( http://nmap.org/ncat ) Ncat: Listening on 127.0.0.1:6000
In this way, a listening server is opened on port 6000 , and we can connect to it through 127.0.0.1:6000 ; if your machine can be accessed by the external network, you can use a listening address like 0.0.0.0 , for example:
[root@iZ238vnojlyZ ~]# nc -v -l 0.0.0.0 6000 Ncat: Version 6.40 ( http://nmap.org/ncat ) Ncat: Listening on 0.0.0.0:6000
-
Simulate a client program
When using the nc command to simulate a client program, we do not need to use the -l option, just write the ip address (or domain name, the nc command can automatically resolve the domain name) and the port number.
## 连接百度 web 服务器 [root@iZ238vnojlyZ ~]# nc -v www.baidu.com 80 Ncat: Version 6.40 ( http://nmap.org/ncat ) Ncat: Connected to 115.239.211.112:80.
The output prompts us to successfully connect to the Baidu Web server.
We know that when the client connects to the server, the operating system randomly assigns an available port number to connect to the server. When using the nc command as a client, you can use the -p option to specify which port number to use to connect to the server. For example, we want to use the local 5555 The port is connected to Baidu's Web server, you can enter this:
[root@iZ238vnojlyZ ~]# nc -v -p 5555 www.baidu.com 80
Ncat: Version 6.40 ( http://nmap.org/ncat )
Ncat: Connected to 115.239.211.112:80.
Open another shell window, we use the lsof command introduced above to verify that it is indeed connected to the Baidu Web server through port 5555 .
[root@iZ238vnojlyZ ~]# lsof -Pni | grep nc
nc 32610 root 3u IPv4 113369437 0t0 TCP 120.55.94.78:5555->115.239.211.112:80 (ESTABLISHED)
The result is indeed what we expected.
Of course, when the nc command is used to establish a connection with the peer, we can send a message. Let's demonstrate this process through a specific example
-
Use nc -v -l 0.0.0.0 6000 to simulate a listening service, and then create a new shell window to use nc -v 127.0.0.1 6000 to simulate a client program to connect to the server just now.
-
At this time, the client and server can send messages to each other. We can achieve a simplified version of IM software chat effect:
Client effect:
Server-side effects:
If you accidentally make a mistake when using the nc command to send a message, you can use Ctrl + Backspace to delete it.
The nc command will default to \ n as the end of each message. If you specify the -C option, \ r \ n will be used as the end of the message.
The nc command can not only send messages, but also send files. Let's also demonstrate:
It should be noted that the party receiving the file is the server, and the party sending the file is the client .
-
Server-side commands:
nc -l ip地址 端口号 > 接收的文件名
-
Client commands:
nc ip地址 端口号 < 发送的文件名
Server-side effects :
Client effect:
Note: Here the client sends a file called index.html , and the server side saves it with the file name xxx.html , which means that the server side does not necessarily use the file name sent by the client when saving the received file name.
According to the above introduction, when we need to debug our own server or client program, and do not want to develop the corresponding peer, we can use the nc command to simulate.
Of course, the nc command is very powerful, and its functions are far from those introduced in this section. If you are interested, you can go to the nc man manual for more information.