Netcat tool for CentOS

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. The common Linux releases contain NetCat (nc), and even

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

[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 the desired link: 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. To use 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


Related Article

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.