主頁(yè) > 知識(shí)庫(kù) > Linux 中的nc命令小結(jié)

Linux 中的nc命令小結(jié)

熱門(mén)標(biāo)簽:呼叫中心市場(chǎng)需求 鐵路電話系統(tǒng) 百度競(jìng)價(jià)排名 地方門(mén)戶(hù)網(wǎng)站 Linux服務(wù)器 服務(wù)外包 網(wǎng)站排名優(yōu)化 AI電銷(xiāo)

NetCat,在網(wǎng)絡(luò)工具中有“瑞士軍刀”美譽(yù),其有Windows和Linux的版本。因?yàn)樗绦【罚?.84版本也不過(guò)25k,舊版本或縮減版甚至更?。?、功能實(shí)用,被設(shè)計(jì)為一個(gè)簡(jiǎn)單、可靠的網(wǎng)絡(luò)工具,可通過(guò)TCP或UDP協(xié)議傳輸讀寫(xiě)數(shù)據(jù)。同時(shí),它還是一個(gè)網(wǎng)絡(luò)應(yīng)用Debug分析器,因?yàn)樗梢愿鶕?jù)需要?jiǎng)?chuàng)建各種不同類(lèi)型的網(wǎng)絡(luò)連接。

一、版本

通常的Linux發(fā)行版中都帶有NetCat(簡(jiǎn)稱(chēng)nc),甚至在拯救模式光盤(pán)中也由busybox提供了簡(jiǎn)版的nc工具。但不同的版本,其參數(shù)的使用略有差異。

NetCat 官方地址:http://netcat.sourceforge.net/

引用[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

建議在使用前,先用man nc看看幫助。這里以紅旗DC Server 5.0上的1.10版本進(jìn)行簡(jiǎn)單說(shuō)明。
假設(shè)兩服務(wù)器信息:

引用server1: 192.168.228.221
server2: 192.168.228.222

二、常見(jiàn)使用

1、遠(yuǎn)程拷貝文件
從server1拷貝文件到server2上。需要先在server2上,用nc激活監(jiān)聽(tīng),server2上運(yùn)行:

引用[root@hatest2 tmp]# nc -lp 1234 > install.log

server1上運(yùn)行:

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

2、克隆硬盤(pán)或分區(qū)

操作與上面的拷貝是雷同的,只需要由dd獲得硬盤(pán)或分區(qū)的數(shù)據(jù),然后傳輸即可。

克隆硬盤(pán)或分區(qū)的操作,不應(yīng)在已經(jīng)mount的的系統(tǒng)上進(jìn)行。所以,需要使用安裝光盤(pán)引導(dǎo)后,進(jìn)入拯救模式(或使用Knoppix

工具光盤(pán))啟動(dòng)系統(tǒng)后,在server2上進(jìn)行類(lèi)似的監(jiān)聽(tīng)動(dòng)作:

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

server1上執(zhí)行傳輸,即可完成從server1克隆sda硬盤(pán)到server2的任務(wù):

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

※ 完成上述工作的前提,是需要落實(shí)光盤(pán)的拯救模式支持服務(wù)器上的網(wǎng)卡,并正確配置IP。

3、端口掃描
可以執(zhí)行:

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

4、保存Web頁(yè)面

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

5、模擬HTTP Headers

引用[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
[......]

在nc命令后,輸入紅色部分的內(nèi)容,然后按兩次回車(chē),即可從對(duì)方獲得HTTP Headers內(nèi)容。

6、聊天
nc還可以作為簡(jiǎn)單的字符下聊天工具使用,同樣的,server2上需要啟動(dòng)監(jiān)聽(tīng):

[root@hatest2 tmp]# nc -lp 1234

server1上傳輸:

[root@hatest1 ~]# nc 192.168.228.222 1234

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

7、傳輸目錄

從server1拷貝nginx-0.6.34目錄內(nèi)容到server2上。需要先在server2上,用nc激活監(jiān)聽(tīng),server2上運(yùn)行:

引用[root@hatest2 tmp]# nc -l 1234 |tar xzvf -

server1上運(yùn)行:

引用[root@hatest1 ~]# ll -d nginx-0.6.34

drwxr-xr-x 8 1000 1000 4096 12-23 17:25 nginx-0.6.34
[root@hatest1 ~]# tar czvf - nginx-0.6.34|nc 192.168.228.222 1234

8、參數(shù)簡(jiǎn)介

這僅是一個(gè)1.10版本的簡(jiǎn)單說(shuō)明,詳細(xì)的參數(shù)使用還是需要看man:

引用想要連接到某處: nc [-options] hostname port[s] [ports] ...

綁定端口等待連接: nc -l -p port [-options] [hostname] [port]

參數(shù):

-g gateway source-routing hop point[s], up to 8
-G num source-routing pointer: 4, 8, 12, ...
-h 幫助信息
-i secs 延時(shí)的間隔
-l 監(jiān)聽(tīng)模式,用于入站連接
-n 指定數(shù)字的IP地址,不能用hostname
-o file 記錄16進(jìn)制的傳輸
-p port 本地端口號(hào)
-r 任意指定本地及遠(yuǎn)程端口
-s addr 本地源地址
-u UDP模式
-v 詳細(xì)輸出——用兩個(gè)-v可得到更詳細(xì)的內(nèi)容
-w secs timeout的時(shí)間

-z 將輸入輸出關(guān)掉——用于掃描時(shí),其中端口號(hào)可以指定一個(gè)或者用lo-hi式的指定范圍。 

9、1.84版本參數(shù)簡(jiǎn)介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 強(qiáng)制使用ipv4

2. -6 強(qiáng)制使用ipv6

3. -D 允許socket通信返回debug信息

4.       -d 不允許從標(biāo)準(zhǔn)輸入中讀取

5. -h 顯示nc幫助文檔

6. -i interval 

7.    指定每行之間內(nèi)容延時(shí)發(fā)送和接受,也可以使多個(gè)端口之間的連接延時(shí)

8. -k 當(dāng)一個(gè)連接結(jié)束時(shí),強(qiáng)制nc監(jiān)聽(tīng)另一個(gè)連接。必須和-l一起使用

9. -l 用于監(jiān)聽(tīng)傳入的數(shù)據(jù)鏈接,不能與-p -z -s一起使用。-w 參數(shù)的超時(shí)也會(huì)被忽略

10. -n 不執(zhí)行任何地址,主機(jī)名,端口或DNS查詢(xún)

11. -p 指定nc使用的源端口,受權(quán)限限制且不能余-l一起使用

12. -r 指定nc使用的源端口和目的端口,不能使用系統(tǒng)原來(lái)就指定的那些端口

13. -S 允許在RFC 2385的TCP MD5簽名選項(xiàng)

14. -s source_ip_address 

15.    指定用于發(fā)包的接口的IP地址,不能和-l一起使用

16. -T ToS

17.    指定鏈接的IP服務(wù)類(lèi)型(TOS)

18. -C 自動(dòng)換行

19. -t 使nc能夠與telnet交互

20. -U 使用UNIX域socket

21. -u 使用udp代替默認(rèn)的tcp選項(xiàng)

22. -v 輸出詳細(xì)報(bào)告

23. -w timeout

24.     一個(gè)鏈接一段時(shí)間無(wú)操作,則自動(dòng)斷開(kāi),默認(rèn)無(wú)超時(shí)

25. -X proxy_version

26.     指定nc使用代理時(shí)所采用的協(xié)議,可選的有socksv4,socks5以及https。默認(rèn)socks5

27. -x proxy_address[:port]

28.     指定nc使用的代理地址和端口。默認(rèn)設(shè)置:1080(SOCKS),3128(HTTPS)

29. -z 只監(jiān)聽(tīng)不發(fā)送任何包 

三、版本差異

不用系統(tǒng)上提供的nc版本會(huì)有說(shuō)不同,其提供的參數(shù)使用方法也略有差異。
例如,紅旗Asianux 3.0 SP1拯救光盤(pán)上的版本是供使用的參數(shù)僅有一部分:

引用# 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系統(tǒng)中提供的nc版本則是1.84的,按上面的參數(shù)用法寫(xiě)會(huì)執(zhí)行不了:

引用[root@ftpserver ~]# rpm -q nc

nc-1.84-10
[root@ftpserver ~]# 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文檔,可見(jiàn)在這個(gè)版本中,-l是不能與-s、-p、-z一起使用的,-w參數(shù)也會(huì)被忽略,所以,正確的用法是:

[root@ftpserver tmp]# nc -l 1234

四、用在腳本中

nc每次啟動(dòng)監(jiān)聽(tīng)后,都會(huì)在客戶(hù)端連接完成并退出的同時(shí),服務(wù)端一同退出。所以,如果需要不斷的使用nc進(jìn)行數(shù)據(jù)傳輸,需要在腳本中使用循環(huán)。利用nc實(shí)現(xiàn)更多的功能,可參考其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

您可能感興趣的文章:
  • Linux下rsync遠(yuǎn)程數(shù)據(jù)同步命令的詳細(xì)介紹
  • linux rsync同步命令(值得收藏)
  • Linux利用nc命令監(jiān)控服務(wù)器端口的方法

標(biāo)簽:蘭州 湖南 仙桃 湘潭 崇左 黃山 銅川 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Linux 中的nc命令小結(jié)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢(xún)

    • 400-1100-266