Home >Operation and Maintenance >Linux Operation and Maintenance >What is the nc command in Linux system? Detailed explanation of the usage of nc command
This article mainly introduces the basic usage of the nc command in the Linux system. The nc command is very powerful. Here is a brief introduction to its basic use for port scanning and file transfer.
Function description: A powerful network tool 0a0ab0495c3035f36a29016608ecf616, known as the "Swiss Army Knife" among network tools, and has Windows and Linux versions. Because it is short, concise and practical, it is designed as a simple and reliable network tool that can transmit reading and writing data through TCP or UDP protocols. At the same time, it is also a network application debug analyzer because it can create various types of network connections as needed.
nc can provide the following network functions:
1) Listen to a specific port, then nc can be used as a server, but I found that the server generated by using nc in this way is just an echo server, and there is no other more powerful one. Function.
2) Connect to a specific port, then nc becomes a client. Similarly, it is also a simple client, which can only play the role of echo.
3) Scan the port, which can be used as a query Whether a certain port is opened on a certain machine.
A few specific usage examples
Example 1:
Use nc to open a specific port under linux
nc -lp 23 & (i.e. telnet)
netstat -an|grep 23 (check whether the port is open normally)
Example 2:
Use nc for file transfer, command
ssh root@www.freetstar. com "( nc -l 10003 > destination 2>/dev/null & )" && cat source | nc www.freetstar.com 10003
&& Log in to the remote host www.freetstar.com via ssh and use the nc command Open the local 10003 port and become a background process. Afterwards, open the source file on the local machine and redirect it to port 10003 of www.freetstar.com, that is, let the remote www.freetstar.com host 10003 Number port to receive the source file
Example 3:
Use nc to scan a specific port under linux
nc -v -z host.example.com 70-80
Scan port (70 to 80 ), the range can be specified. -v output verbose information.
Example 4:
Clone the hard disk or partition
Similar to Example 2, you only need to obtain the data of the hard disk or partition from dd and then transfer it.
The operation of cloning a hard disk or partition should not be performed on a system that has been mounted. Therefore, you need to use the installation CD to boot, enter the rescue mode (or use the Knoppix tool CD) to start the system and then execute on server1: # nc -l -p 1234 | dd of=/dev/sda
execute on server1 Listen on port 1234 and save the obtained file to /dev/sda
Execute on server2: # dd if=/dev/sda | nc server1 1234
Example 5:
Save the Web page
while true; do nc -l -p 80 -q 1 206cce7cb32103de21fb8fb1de9c3e31 ][-Gdb9cc1624dcce7afe0063e2917642793][-i2f3fe0dbd5be06ed715573553cf2ef98][-od6889eeca6a25bc7a49d619152a304e6][-pbbc15f1e974e04b0f2f9736be4497725][-s67bc88d38eaf5cd1b9287dea429c3a11][ -v...][-w8c2fb036788d596a605bd17c92dd7f47][Host name][Communication port...]
Parameters:
-g73f07b02b8e329271b84a1cdf7f36a79 Set router jump communication gateway, up to 8 can be set.
-Ga76503b6107cf2b3d1c831135445012a Set the source routing director, its value is a multiple of 4.
-h Online help.
-i2f3fe0dbd5be06ed715573553cf2ef98 Set the time interval for transmitting information and scanning communication ports.
-l Use listening mode to control incoming data.
-n Use the IP address directly instead of going through the domain name server.
-od6889eeca6a25bc7a49d619152a304e6 Specifies the file name, and dumps the transmitted data in hexadecimal characters into this file and saves it.
-p0080fcb76bc350fe2d74a623493a766b Set the communication port used by the local host.
-r A random number specifies the communication port between the local and remote hosts.
-sebe9dff2ea243408392d26c2a6895070 Set the IP address of the local host to send data packets.
-u Use UDP transmission protocol.
-v Displays the instruction execution process.
-w8c2fb036788d596a605bd17c92dd7f47 Set the time to wait for connection.
-z Use 0 input/output mode, only used when scanning communication ports.
Let’s take a look at its basic usage:
1. Listen to the local port
root@10.1.1.43:~# nc -l -p 1234 root@10.1.1.43:~# netstat -tunlp | grep 1234
tcp 0 0 0.0.0.0:1234 0.0.0.0:* LISTEN 15543 /nc
2. Port scan
root@10.1.1.43:~# nc -v -w 10 10.1.1.180 80
(UNKNOWN) [10.1.1.180] 80 (www) open
root@10.1.1.43:~# nc -v -w 10 10.1.1.180 -z 80-30000
(UNKNOWN) [10.1 .1.180] 22000 (?) open
(UNKNOWN) [10.1.1.180] 80 (www) open
3. File outgoing
Source 10.1.1.43 text.txt
Purpose 10.1.1.180
root@10:~# nc -l -p 1234 > test.txt #开10.1.1.180:1234端口监听,并将socket传输过来的数据重定向到test.txt文件 test 43 nc root@10.1.1.43:~#cat test.txt root@10.1.1.43:~# nc 10.1.1.180 1234 < test.txt #连接远程的10.1.1.180,从test.txt的路径从定向到socket,从而将文件传输到远方。 root@10:~# cat test.txt test 43 nc
4. Directory transfer
Source 10.1.1.43 python_program
Purpose 10.1.1.180
root@10:~# nc -l -p 1234 | tar xzvf - root@10.1.1.43:~# tar czvf - python_program | nc 10.1.1.180 1234
python_program/
python_program/1.py
python_program/4.py
python_program/3.py
5. Test UDP port
root@172.16.211.34:web# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0 .0.0:* ##udp 0 0 0.0.0.0:68 0.0.0.0:* 887/dhclient
root@172.16.211.35:~# nc -vuz 172.16.211.34 68Connection to 172.16.2 11.34 68 port [udp/bootpc] succeeded!
The above is the detailed content of What is the nc command in Linux system? Detailed explanation of the usage of nc command. For more information, please follow other related articles on the PHP Chinese website!