nc:a test cmd for TCP HTTP

來源:互聯網
上載者:User

標籤:blog   http   io   ar   os   使用   for   sp   strong   

 docker 上需要nc 測試環境--internet上搜集了一些資料

---------------------------------------------------------------------------------

瑞士軍刀 nc //man 說的清楚了

Common uses include:

· simple TCP proxies
· shell-script based HTTP clients and servers
· network daemon testing
· a SOCKS or HTTP ProxyCommand for ssh(1)
· and much, much more

TCP:hostA:$ nc -l 1234  hostB:$ nc hostA 1234 //use for chatting

date transfer hostA:$ nc -l 1234 > t.out hostB:$ nc hostA 1234 < t.in

HTTP: hostA apache on   //get post

[[email protected] voh]# nc 192.168.110.135 80 < t.t
HTTP/1.1 200 OK
Date: Wed, 05 Nov 2014 05:50:28 GMT
Server: Apache/2.2.27 (Unix)
Last-Modified: Sat, 20 Nov 2004 20:16:24 GMT
ETag: "c4d07-2c-3e9564c23b600"
Accept-Ranges: bytes
Content-Length: 44
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html

<html><body><h1>It works!</h1></body></html>[[email protected] voh]# nc 192.168.110.135 80 < t.t
HTTP/1.1 200 OK

....

<html><body><h1>It works!</h1></body></html>

POST /scripts/nsiislog.dll HTTP/1.1
Host: www.test.com
Content-Length: 16758
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; zh-CN; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: ASPSESSIONIDAQDBSAQA=LDABJFEDGCCINNBJMHNNPKDI
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

scan
linux-f1ao:/home/voh # nc -v -w 10 192.168.110.135 -z 21-25
nc: connect to 192.168.110.135 port 21 (tcp) failed: Connection refused
Connection to 192.168.110.135 22 port [tcp/ssh] succeeded!
nc: connect to 192.168.110.135 port 23 (tcp) failed: Connection refused

 ----------------------------------------------------------------------------------------------------------------------------- 

source url

http://blog.csdn.net/wang7dao/article/details/7684998

 

NetCat,在網路工具中有“瑞士軍刀”美譽,其有Windows和Linux的版本。因為它短小精悍(1.84版本也不過25k,舊版本或縮減版甚至更小)、功能實用,被設計為一個簡單、可靠的網路工具,可通過TCP或UDP協議傳輸讀寫資料。同時,它還是一個網路應用Debug分析器,因為它可以根據需要建立各種不同類型的網路連接。

一、版本
通常的Linux發行版中都帶有NetCat(簡稱nc),甚至在拯救模式光碟片中也由busybox提供了簡版的nc工具。但不同的版本,其參數的使用略有差異。
NetCat 官方地址:http://netcat.sourceforge.net/

引用[[email protected] ~]# cat /etc/asianux-release
Asianux release 2.0 (Trinity SP2)
[[email protected] ~]# cat /etc/redflag-release
Red Flag DC Server release 5.0 (Trinity SP2)
[[email protected] ~]# type -a nc
nc is /usr/bin/nc
[[email protected] ~]# rpm -q nc
nc-1.10-22


建議在使用前,先用man nc看看協助。這裡以紅旗DC Server 5.0上的1.10版本進行簡單說明。
假設兩伺服器資訊:

引用server1: 192.168.228.221
server2: 192.168.228.222


二、常見使用
1、遠程拷貝檔案

從server1拷貝檔案到server2上。需要先在server2上,用nc啟用監聽,server2上運行:

引用[[email protected] tmp]# nc -lp 1234 > install.log


server1上運行:

引用[[email protected] ~]# ll install.log
-rw-r--r--   1 root root 39693 12月 20   2007 install.log
[[email protected] ~]# nc -w 1 192.168.228.222 1234 < install.log


2、複製硬碟或分區
操作與上面的拷貝是雷同的,只需要由dd獲得硬碟或分區的資料,然後傳輸即可。
複製硬碟或分區的操作,不應在已經mount的的系統上進行。所以,需要使用安裝光碟片引導後,進入拯救模式(或使用Knoppix工具光碟片)啟動系統後,在server2上進行類似的監聽動作:

# nc -l -p 1234 | dd of=/dev/sda


server1上執行傳輸,即可完成從server1複製sda硬碟到server2的任務:

# dd if=/dev/sda | nc 192.168.228.222 1234


※ 完成上述工作的前提,是需要落實光碟片的拯救模式支援伺服器上的網卡,並正確配置IP。

3、連接埠掃描

可以執行:

引用# nc -v -w 1 192.168.228.222 -z 1-1000
hatest2 [192.168.228.222] 22 (ssh) open


4、儲存Web頁面

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



5、類比HTTP Headers

引用[[email protected] ~]# 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
[......]


在nc命令後,輸入紅色部分的內容,然後按兩次斷行符號,即可從對方獲得HTTP Headers內容。

6、聊天
nc還可以作為簡單的字元下聊天工具使用,同樣的,server2上需要啟動監聽:

[[email protected] tmp]# nc -lp 1234



server1上傳輸:

[[email protected] ~]# nc 192.168.228.222 1234


這樣,雙方就可以相互交流了。使用Ctrl+D正常退出。

7、傳輸目錄
從server1拷貝nginx-0.6.34目錄內容到server2上。需要先在server2上,用nc啟用監聽,server2上運行:

引用[[email protected] tmp]# nc -l 1234 |tar xzvf -


server1上運行:

引用[[email protected] ~]# ll -d nginx-0.6.34
drwxr-xr-x 8 1000 1000 4096 12-23 17:25 nginx-0.6.34
[[email protected] ~]# tar czvf - nginx-0.6.34|nc 192.168.228.222 1234


8、參數簡介
這僅是一個1.10版本的簡單說明,詳細的參數使用還是需要看man:

引用想要串連到某處: nc [-options] hostname port[s] [ports] ... 
綁定連接埠等待串連: nc -l -p port [-options] [hostname] [port] 
參數: 
-g gateway source-routing hop point[s], up to 8 
-G num source-routing pointer: 4, 8, 12, ... 
-h 協助資訊 
-i secs 延時的間隔 
-l 監聽模式,用於入站串連 
-n 指定數位IP地址,不能用hostname 
-o file 記錄16進位的傳輸 
-p port 本地連接埠號碼 
-r 任意指定本地及遠程連接埠 
-s addr 本地源地址 
-u UDP模式 
-v 詳細輸出——用兩個-v可得到更詳細的內容 
-w secs timeout的時間 

-z 將輸入輸出關掉——用於掃描時,其中連接埠號碼可以指定一個或者用lo-hi式的指定範圍。 


9、1.84版本參數簡介1. nc [-46DdhklnrStUuvzC] [-i interval] [-p source_port]
2.    [-s source_ip_address] [-T ToS] [-w timeout] [-X proxy_protocol] [-x 
3.    proxy_address[:port]] [hostname] [port[s]] 
 
1. -4 強制使用ipv4 
2. -6 強制使用ipv6 
3. -D 允許socket通訊返回debug資訊 
4.       -d 不允許從標準輸入中讀取 
5. -h 顯示nc協助文檔 
6. -i interval  
7.    指定每行之間內容延時發送和接受,也可以使多個連接埠之間的串連延時 
8. -k 當一個串連結束時,強制nc監聽另一個串連。必須和-l一起使用 
9. -l 用於監聽傳入的資料連結,不能與-p -z -s一起使用。-w 參數的逾時也會被忽略 
10. -n 不執行任何地址,主機名稱,連接埠或DNS查詢 
11. -p 指定nc使用的源連接埠,受許可權限制且不能餘-l一起使用 
12. -r 指定nc使用的源連接埠和目的連接埠,不能使用系統原來就指定的那些連接埠 
13. -S 允許在RFC 2385的TCP MD5簽名選項 
14. -s source_ip_address  
15.    指定用於發包的介面的IP地址,不能和-l一起使用 
16. -T ToS 
17.    指定連結的IP服務類型(TOS) 
18. -C 自動換行 
19. -t 使nc能夠與telnet互動 
20. -U 使用UNIX域socket 
21. -u 使用udp代替預設的tcp選項 
22. -v 輸出詳細報告 
23. -w timeout 
24.     一個連結一段時間無操作,則自動斷開,預設無逾時 
25. -X proxy_version 
26.     指定nc使用代理時所採用的協議,可選的有socksv4,socks5以及https。預設socks5 
27. -x proxy_address[:port] 
28.     指定nc使用的Proxy 位址和連接埠。預設設定:1080(SOCKS),3128(HTTPS) 
29. -z 只監聽不發送任何包

 
三、版本差異
不用系統上提供的nc版本會有說不同,其提供的參數使用方法也略有差異。
例如,紅旗Asianux 3.0 SP1拯救光碟片上的版本是供使用的參數僅有一部分:

引用# nc -h
BusyBox v1.2.0 (2008.04.14-01:35+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


而在Asianux 3.0 SP1系統中提供的nc版本則是1.84的,按上面的參數用法寫會執行不了:

引用[[email protected] ~]# rpm -q nc
nc-1.84-10
[[email protected] ~]# nc -lp 1234
usage: nc [-46DdhklnrStUuvzC] [-i interval] [-p source_port]
           [-s source_ip_address] [-T ToS] [-w timeout] [-X proxy_version]
           [-x proxy_address[:port]] [hostname] [port[s]]


講查看man文檔,可見在這個版本中,-l是不能與-s、-p、-z一起使用的,-w參數也會被忽略,所以,正確的用法是:

[[email protected] tmp]# nc -l 1234



四、用在指令碼中
nc每次啟動監聽後,都會在用戶端串連完成並退出的同時,服務端一同退出。所以,如果需要不斷的使用nc進行資料轉送,需要在指令碼中使用迴圈。利用nc實現更多的功能,可參考其rpm提供的參考指令碼:

引用# 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/webrelay
/usr/share/doc/nc-1.10/scripts/websearch
/usr/share/man/man1/nc.1.gz

nc:a test cmd for TCP HTTP

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.