netcat(簡寫為nc)是什么?
Mac內(nèi)置了nc命令,使用man nc查看其幫助文檔:
DESCRIPTION
The nc (or netcat) utility is used for just about anything under the sun involving
TCP or UDP. It can open TCP connections, send UDP packets, listen on arbitrary TCP
and UDP ports, do port scanning, and deal with both IPv4 and IPv6. Unlike
telnet(1), nc scripts nicely, and separates error messages onto standard error
instead of sending them to standard output, as telnet(1) does with some.
Common uses include:
o simple TCP proxies
o shell-script based HTTP clients and servers
o network daemon testing
o a SOCKS or HTTP ProxyCommand for ssh(1)
o and much, much more
簡單翻譯一下,nc是一個強大的TCP和UDP相關(guān)的工具,可以打開TCP連接,發(fā)送UDP數(shù)據(jù)包,監(jiān)聽任意TCP或UDP端口,做端口掃描,同時支持IPv4和IPv6,等等。
通??梢杂胣c做下面這些事情:
- 簡單的TCP代理;
- 基于shell腳本的HTTP客戶端和服務(wù)端;
- 網(wǎng)絡(luò)守護(hù)進(jìn)程測試;
- 用于ssh的SOCKS或HTTP代理命令;
- ...
下面講下如何用nc啟動一個客戶端和服務(wù)端,并在兩者之間通信:
- 打開一個終端窗口,輸入命令:
nc -l 9999,這樣就開始監(jiān)聽9999端口; - 打開第二個新的終端窗口,輸入命令:
nc 127.0.0.1 9999 - 這個時候在第一個終端輸入任何文本,并按回車,在第二個終端窗口就可以立即看到內(nèi)容,反之亦然。此時nc已不care哪個是服務(wù)端、哪個是客戶端了,兩者互為客戶端/服務(wù)端。
- 在上面兩個終端窗口的任一個窗口按
Ctrl+C終止,則兩個窗口的監(jiān)聽都被終止。
使用man nc查看幫助手冊可以查看到nc的更詳細(xì)用法。