Linux nc command & lt; netcat & gt;

Source: Internet
Author: User
Tags domain name server

Linux nc command <netcat> Function Description: powerful network tool 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...] parameter quantity:-g <gateway> sets the router hop communication gateway. You can set up to eight parameter values. -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 shows the command execution process. -W <timeout seconds> sets the waiting time. -Z uses the 0 input/output mode and is only used when scanning communication ports. Extended information 1: nc simple use Example 1) port Scan # nc-v-w 2 192.168.2.34-z 21-24nc: connect to 192.168.2.34 port 21 (tcp) failed: connection refusedConnection to 192.168.2.34 22 port [tcp/ssh] succeeded! Nc: connect to 192.168.2.34 port 23 (tcp) failed: Connection refusednc: connect to 192.168.2.34 port 24 (tcp) failed: Connection refused 2) copy a file from 192.168.2.33 to 192.168.2.34 at 192.168.2.34: nc-l 1234> test.txt on 192.168.2.33: nc 192.168.2.34 <test.txt 3) Simple chat tool on 192.168.2.34: nc-l 1234 on 192.168.2.33: nc 192.168.2.34 1234, the two sides can communicate with each other. Use ctrl + C (or D) to exit. 3. run the nc command to operate memcached1) store data: printf "set key 0 10 6rnresultrn" | nc 192.168.2.34 112112) get data: printf "get keyrn" | nc 192.168.2.34 112113) delete data: printf "delete keyrn" | nc 192.168.2.34 112114) view status: printf "statsrn" | nc 192.168.2.34 112115) simulate top Command view status: watch "echo stats" | nc 192.168.2.34 112116) clear cache: printf "flush_allrn" | nc 192.168.2.34 11211 (careful operation, the cache is cleared) nc-l 1234 nc 127.0.0.1 1234 establish a connection on port 1234 and send the input nc-p to each other 1234-w 5 host.example.com 80 establish a connection from the local port 1234 to port 80 of host.example.com, 5 seconds of timeout nc-u host.example.com 53u for UDP connection echo-n "GET/HTTP/1.0" r "n" r "n" | nc host.example.com 80 connect to the host and execute nc-v -z host.example.com 70-80 scan port (70 to 80 ), you can specify a range. -V: Output details. Extended information 2: I do not describe how to use the linux nc command to transmit a file nc. Today I will focus on using nc to transfer files. The reason is that the company's device sudo does not have ssh, scp, or other remote access commands, or the host. deny contains ssh deny. Today, I used scp to illustrate the next more useful lightweight tool, another powerful feature of nc-file transfer. Example: target Host listening nc-l listening port> file name to be received nc-l 4444> cache.tar.gz source host initiating request nc Target Host ip address destination port nc 192.168.0.85 4444 netstat as follows [root @ localhost jiangbao] # netstat-tpln tcp 0 0 0.0.0.0: 4444 0.0.0.0: * LISTEN 18166/nc the following DATA TRANSFERStart by using nc to listen on a specific port, with output captured into a file: $ nc-l 1234> filename. outUsing a second machine, connect to the listening nc process, feeding it The file which is to be transferred: $ nc host.example.com 1234 Extended Document 3: linux nc (NetCat) command details. This article introduces nc in great detail, which is partially duplicated in the above documents. i. The common Linux releases contain NetCat (nc), and busybox provides a simplified nc tool even in the rescue mode. However, the parameters of different versions vary slightly. [Root @ hatest1 ~] # Cat/etc/asianux-releaseAsianux release 2.0 (Trinity SP2) [root @ hatest1 ~] # Cat/etc/redflag-releaseRed Flag DC Server release 5.0 (Trinity SP2) [root @ hatest1 ~] # Type-a ncnc is/usr/bin/nc [root @ hatest1 ~] # Rpm-q ncnc-1.10-22 is recommended before use, use man nc to see help. Here we will briefly describe the 5.0 version of the red-flag DC Server 1.10. Assume that the information of the two servers is: Reference server1: 192.168.228.221server2: 192.168.228.222 2. Common use 1. Remote copy of a file from server1 to server2. Run [root @ hatest2 tmp] # nc-lp 1234> install. logserver1: Reference [root @ hatest1 ~] # Ll install. log-rw-r-1 root 39693 2007 install. log [root @ hatest1 ~] # Nc-w 1 192.168.228.222 1234 <install. log 2. The clone hard disk or partition operation is the same as the copy operation above. You only need to obtain the data of the hard disk or partition by dd, and then transmit it. 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 server1 to complete the task of cloning sda from server1 to server2: # dd if =/dev/sda | nc 192.168.228.222 1234 ※the prerequisite 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 Scan can be performed: Reference # nc-v-w 1 192.168.228.222-z 1-1000hatest2 [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.linuxso.com 80GET/HTTP/1.1 Host: ispconfig. orgReferrer: mypage. comUser-Agent: my-browser HTTP/1.1 200 OKDate: Tue, 16 Dec 2008 07:23:24 GMTServer: 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.8Set-Cookie: PHPSESSID = empty; path =/Expires: 0Cache-Control: no-store, no-cache, must-revalidate, post-check = 0, pre-check = 0 Prag Ma: no-cacheCache-Control: private, post-check = 0, pre-check = 0, max-age = 0Set-Cookie: oWn_sid = xRutAY; expires = Tue, 23-Dec-2008 07:23:24 GMT; path =/Vary: Accept-EncodingTransfer-Encoding: chunkedContent-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 simple chat tool under characters. Similarly, Server 2 needs to start listening: [root @ hatest2 tmp] # nc-lp 1234 server1 transmission: [root @ hatest1 ~] # Nc 192.168.228.222 1234 so that the two parties can communicate with each other. Use Ctrl + D to exit normally. 7. The transfer directory copies the contents of the nginx-0.6.34 directory from server1 to server2. You must first activate the listener with nc on server2. Run the following command on server2: Reference [root @ hatest2 tmp] # nc-l 1234 | tar xzvf-server1: reference [root @ hatest1 ~] # Ll-d nginx-0.6.34drwxr-xr-x 8 1000 1000 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, the detailed parameter use still need to see man: Reference want to connect to somewhere: nc [-options] hostname port [s] [ports]… Bind the port to wait for the connection: nc-l-p port [-options] [hostname] [port] parameter:-g gateway source-routing hop point [s], up to 8-G num source-routing pointer: 4, 8, 12 ,... -H help information-I secs delay interval-l listening mode, used for inbound connection-n specified number IP address, the hostname-o file cannot be used to record the hexadecimal transmission-p port local port number-r arbitrarily specify the local and 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 turn off the input and output-for scanning, you can specify one port number or a range in the lo-hi format. 3. Version differences do not apply to the nc version provided by the system. The parameter usage varies slightly. For example, the version on the Hongqi Asianux 3.0 SP1 rescue disc is only available in part: Reference # nc-hBusyBox 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, while 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 ncnc-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] view man's document. In this version,-l cannot be used with-s,-p, and-z, the-w parameter is ignored, so the correct usage is: [root @ ftpserver tmp] # nc-l 1234 4. After each listener is started by nc in the script, when the client connection is complete and exits, the server exits together. 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

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.