Linux NC (Netcat) commands

Source: Internet
Author: User
Tags file copy

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:

Reference

Server1: 192.168.228.221
Server2: 192.168.228.222
Ii. Common Use
1. Remote file copy
Copy the file 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-LP 1234> install. Log
Run on server1:

Reference

[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
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, 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.

3. Port Scanning
Run the following command:

Reference

# Nc-v-W 1 192.168.228.222-Z 1-1000
Hatest2 [192.168.228.222] 22 (SSH) Open
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.huanxiangwu.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.

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

[Root @ hatest2 TMP] # nc-LP 1234

Transmission on Server 1:

[Root @ hatest1 ~] # NC 192.168.228 .222 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 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
8. Parameter Introduction
This is only a simple description of version 1.10. For detailed parameter usage, see man:

Reference

Want to connect to a location: NC [-options] hostname port [s] [ports]…
Bind the port to wait for the connection: NC-l-P port [-options] [hostname] [port]
Parameters:
-G gateway source-routing hop point [s], up to 8
-G num source-routing pointer: 4, 8, 12 ,...
-H help information
-I secs latency Interval
-L listening mode for inbound connection
-N: the IP address of the specified number. The hostname cannot be used.
-O file records hexadecimal Transmission
-P port: local port number
-R: Specify any local or remote ports
-S ADDR local source address
-U udp Mode
-V detailed output -- use two-V to get more detailed content
-W secs timeout time
-Z turns off the input and output -- when used for scanning, the port number can specify a range or use the lo-Hi format.
Iii. Version differences
The system does not need to provide the NC version. The parameter usage varies slightly.
For example, the version on the rescue disc of Hongqi Asianux 3.0 SP1 is only part of the available parameters:

Reference

# Nc-H
Busybox v1.2.0 (2008.04.14-0+ 0000) Multi-call binary

Usage: NC [Options] [IP] [port]

Netcat opens a pipe to IP: Port

Options:
-L listen mode, for inbound connects
-P port local port number
-I secs delay interval for lines sent
-E prog program to Exec after connect (Dangerous !)
-W secs timeout for connects and Final Net reads
The NC version provided in Asianux 3.0 SP1 is 1.84, which cannot be executed according to the preceding parameter usage:

Reference

[Root @ ftpserver ~] # Rpm-Q NC
Nc-1.84-10
[Root @ ftpserver ~] # Nc-LP 1234
Usage: NC [-46 ddhklnrstuuvzc] [-I interval] [-P source_port]
[-S source_ip_address] [-t tos] [-W timeout] [-x proxy_version]
[-X proxy_address [: Port] [hostname] [port [s]
In this version,-l cannot be used with-S,-P, and-Z, and the-W parameter is ignored, the correct usage is:

[Root @ ftpserver TMP] # nc-l 1234

4. Used in scripts
Each time the NC starts the listener, the server exits when the client connection is complete and exits. Therefore, if you need to continuously use NC for data transmission, you need to use loops in the script. Using NC to implement more functions, refer to the reference script provided by its RPM:

Reference

# Rpm-Qd NC
/Usr/share/doc/nc-1.10/changelog
/Usr/share/doc/nc-1.10/readme
/Usr/share/doc/nc-1.10/scripts/readme
/Usr/share/doc/nc-1.10/scripts/Alta
/Usr/share/doc/nc-1.10/scripts/bsh
/Usr/share/doc/nc-1.10/scripts/Dist. Sh
/Usr/share/doc/nc-1.10/scripts/IRC
/Usr/share/doc/nc-1.10/scripts/iscan
/Usr/share/doc/nc-1.10/scripts/NCP
/Usr/share/doc/nc-1.10/scripts/Probe
/Usr/share/doc/nc-1.10/scripts/Web
/Usr/share/doc/nc-1.10/scripts/WebProxy
/Usr/share/doc/nc-1.10/scripts/webrelay
/Usr/share/doc/nc-1.10/scripts/Websearch
/Usr/share/man/Man1/nc.1.gz

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.