Linux nc commands

Source: Internet
Author: User

NetCat is known as "Swiss Army Knife" in network tools, and has Windows and Linux versions. It is designed as a simple and reliable network tool because it is short (1.84 is only 25 kb, earlier version or even smaller version) and has practical functions, read/write data can be transmitted over TCP or UDP. At the same time, it is also a network application Debug analyzer, because it can create different types of network connections as needed.

I. Version
Generally, Linux releases contain NetCat (nc), and busybox provides a simplified nc tool even on the rescue Mode CD. However, the parameters of different versions vary slightly.
NetCat official address: http://netcat.sourceforge.net/


Reference [root @ hatest1 ~] # Cat/etc/asianux-release
Asianux release 2.0 (Trinity SP2)
[Root @ hatest1 ~] # Cat/etc/redflag-release
Red Flag DC Server release 5.0 (Trinity SP2)
[Root @ hatest1 ~] # Type-a nc
Nc is/usr/bin/nc
[Root @ hatest1 ~] # Rpm-q nc
Nc-1.10-22

We recommend that you use man nc to check the help before using it. Here we will briefly describe the 5.0 version of the red-flag DC Server 1.10.
Assume that the two servers have the following information:

Server1: 192.168.10.10
Server2: 192.168.10.11

Ii. Common Use
1. Remote file copy
Copy the file from server1 to server2. You need to activate the listener with nc on Server 2 first,

Run: nc-l 1234> text.txt on Server 2

Run: nc 192.168.10.11 1234 <text.txt

Note: Server 2 listeners must be enabled first.


2. 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, you need to use the installation CD boot, enter the rescue mode (or use the Knoppix tool CD) to start the system, and then perform similar listening actions on server2:

Nc-l-p 1234 | dd of =/dev/sda

The task of cloning an sda hard disk from server1 to server2:

Dd if =/dev/sda | nc192.168.10.11 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.

3. Port Scanning
Run the following command:

# Nc-v-w 2 192.168.10.11-z 21-24
Nc: connect to 192.168.10.11 port 21 (tcp) failed: Connection refused
Connection to 192.168.10.11 22 port [tcp/ssh] succeeded!
Nc: connect to 192.168.10.11 port 23 (tcp) failed: Connection refused
Nc: connect to 192.168.10.11 port 24 (tcp) failed: Connection refused
-Z is followed by the port to be scanned


4. Save the Web page

# While true; do nc-l-p 80-q 1 <somepage.html; done


5. Simulate HTTP Headers

Reference [root @ hatest1 ~] # Nc www.linuxfly.org 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.

6. Chat
Nc can also be used as a chat tool with simple characters. Similarly, the listener needs to be started on Server 2:

Start on Server 2: # nc-lp 1234
Transfer on Server 1: # nc 192.168.10.11 1234


In this way, the two sides can communicate with each other. Use Ctrl + D to exit normally.

7. Transmission directory
Copy the contents of the nginx-0.6.34 directory from server1 to server2. You need to activate the listener with nc on Server 2 first,

Run on server2: # nc-l 1234 | tar xzvf-

Run on server1: # tar czvf-nginx-0.6.34 | nc 192.168.10.11 1234

 


8. Operate memcached with nc name

1) Data Storage: printf "set key 0 10 6rnresultrn" | nc 192.168.10.11 11211
2) obtain data: printf "get keyrn" | nc 192.168.10.11 11211
3) delete data: printf "delete keyrn" | nc 192.168.10.11 11211
4) view the status: printf "statsrn" | nc 192.168.10.11 11211
5) simulate the top command to view the status: watch "echo stats" | nc 192.168.10.11 11211
6) Clear the cache: printf "flush_allrn" | nc 192.168.10.11 11211 (Be careful when clearing the cache, it will be gone)

  • 1
  • 2
  • Next Page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.