網(wǎng)絡(luò)編程利器netcat

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ì)用法。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容