先提一下,| 是管道符號,把左邊的輸出當成右邊的輸入。
# nc -v -z -w2 127.0.0.1 1-100 Connection to 127.0.0.1 22 port [tcp/ssh] succeeded! Connection to 127.0.0.1 53 port [tcp/domain] succeeded! Connection to 127.0.0.1 80 port [tcp/http] succeeded! ... nc: connect to 127.0.0.1 port 100 (tcp) failed: Connection refused
-z 表示只掃描不傳資料,-w2 是逾時 2 秒。
接收端 192.168.1.3 先監聽:
nc -l 1234 > test.txt
發送端 192.168.1.2 再送出:
nc 192.168.1.3 < test.txt
順序不能顛倒,且兩端都要有同名的 test.txt。
把 server1(192.168.16.233)的 nginx 目錄搬到 server2(192.168.48.47)。先在 server2 監聽並解包:
nc -l 1234 | tar xzv-
再在 server1 打包送出:
tar czv- nginx | nc 192.168.48.47 1234
nc -l 1234 # 192.168.1.2 nc 192.168.1.2 1234 # 192.168.1.3
這樣雙方就能互傳訊息,按 Ctrl+C(或 D)結束。