What is the nc command in Linux system? Detailed explanation of the usage of nc command

伊谢尔伦
Release: 2017-05-30 15:01:20
Original
10191 people have browsed it

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 <( ̄▽ ̄)>, 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 < somepage.html; done

Example 6:
Simulating HTTP Headers
[root@hatest1 ~]# nc www .huanxiangwu.com 80
GET / HTTP/1.1
Host: ispconfig.org
Referrer: mypage.com
User-Agent: my-browser
After the nc command, enter the red part content, and then press Enter twice to obtain the HTTP Headers content from the other party.

Example 7:
Chat
Listen on port 1234 on server1 [root@hatest2 tmp]# nc -lp 1234
Send a message on server2 to port 1234 of server1 [root@hatest1 ~] # nc server1 1234
In this way, both parties can communicate with each other. Use Ctrl+D to exit normally.
For more usage, see the man manual

nc example.host port Opens a TCP connection to the port of the example.host host. If the connection fails, no error message is displayed and just exits
nc -p 31337 -w 5 example.host 42 opens with example. A TCP link on port 42 of the host host. Use 31337 as the source port, the timeout link time is 5 seconds
nc -u example.host 53 Specify the protocol as udp protocol

Syntax: nc [-hlnruz][-g ][-G][-i][-o][-p][-s][ -v...][-w][Host name][Communication port...]

Parameters:
-g Set router jump communication gateway, up to 8 can be set.
-G Set the source routing director, its value is a multiple of 4.
-h Online help.
-i 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.
-o Specifies the file name, and dumps the transmitted data in hexadecimal characters into this file and saves it.
-p Set the communication port used by the local host.
-r A random number specifies the communication port between the local and remote hosts.
-s Set the IP address of the local host to send data packets.
-u Use UDP transmission protocol.
-v Displays the instruction execution process.
-w 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
Copy after login

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
Copy after login

(UNKNOWN) [10.1.1.180] 80 (www) open

root@10.1.1.43:~# nc -v -w 10 10.1.1.180 -z 80-30000
Copy after login

(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
Copy after login


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
Copy after login

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
Copy after login

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 68
Copy after login
Connection 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!