日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

好用的netcat工具

發(fā)布時(shí)間:2025/4/16 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 好用的netcat工具 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
NetCat,在網(wǎng)絡(luò)工具中有“瑞士軍刀”美譽(yù),其有Windows和Linux的版本。因?yàn)樗绦【?#xff08;1.84版本也不過(guò)25k,舊版本或縮減版甚至更小)、功能實(shí)用,被設(shè)計(jì)為一個(gè)簡(jiǎn)單、可靠的網(wǎng)絡(luò)工具,可通過(guò)TCP或UDP協(xié)議傳輸讀寫數(shù)據(jù)。同時(shí),它還是一個(gè)網(wǎng)絡(luò)應(yīng)用Debug分析器,因?yàn)樗梢愿鶕?jù)需要?jiǎng)?chuàng)建各種不同類型的網(wǎng)絡(luò)連接。

一、版本
通常的Linux發(fā)行版中都帶有NetCat(簡(jiǎn)稱nc),甚至在拯救模式光盤中也由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、克隆硬盤或分區(qū)
操作與上面的拷貝是雷同的,只需要由dd獲得硬盤或分區(qū)的數(shù)據(jù),然后傳輸即可。
克隆硬盤或分區(qū)的操作,不應(yīng)在已經(jīng)mount的的系統(tǒng)上進(jìn)行。所以,需要使用安裝光盤引導(dǎo)后,進(jìn)入拯救模式(或使用 Knoppix 工具光盤)啟動(dòng)系統(tǒng)后,在server2上進(jìn)行類似的監(jiān)聽(tīng)動(dòng)作:

# nc -l -p 1234 | dd of=/dev/sda
server1上執(zhí)行傳輸,即可完成從server1克隆sda硬盤到server2的任務(wù):

# dd if=/dev/sda | nc 192.168.228.222 1234
※ 完成上述工作的前提,是需要落實(shí)光盤的拯救模式支持服務(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)容,然后按兩次回車,即可從對(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式的指定范圍。
三、版本差異
不用系統(tǒng)上提供的nc版本會(huì)有說(shuō)不同,其提供的參數(shù)使用方法也略有差異。
例如,紅旗Asianux 3.0 SP1拯救光盤上的版本是供使用的參數(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ù)用法寫會(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ì)在客戶端連接完成并退出的同時(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/webproxy
/usr/share/doc/nc-1.10/scripts/webrelay
/usr/share/doc/nc-1.10/scripts/websearch
/usr/share/man/man1/nc.1.gz
五、Windows版介紹
不詳細(xì)說(shuō)明,見(jiàn) 這里 相關(guān)日志
[原]Asianux 4.0 的拯救模式 《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的好用的netcat工具的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。