当前位置:首页 > Linux/Unix > Linux/Unix

八 涓疄鐢ㄧ殑 Linux netcat 鍛戒护绀轰緥

优良自学吧提供八 涓疄鐢ㄧ殑 Linux netcat 鍛戒护绀轰緥,8 涓疄鐢ㄧ殑 Linux netcat 鍛戒护绀轰緥 Netcat 鎴栬€呭彨 nc 鏄?Linux 涓嬬殑涓€涓敤浜庤皟璇曞拰妫€鏌ョ綉缁滃伐鍏峰寘

8 涓疄鐢ㄧ殑 Linux netcat 鍛戒护绀轰緥

Netcat 鎴栬呭彨 nc 鏄?Linux 涓嬬殑涓涓敤浜庤皟璇曞拰妫鏌ョ綉缁滃伐鍏峰寘銆傚彲鐢ㄤ簬鍒涘缓 TCP/IP 杩炴帴锛屾渶澶х殑鐢ㄩ斿氨鏄敤鏉ュ鐞?TCP/UDP 濂楁帴瀛椼?/p>

杩欓噷鎴戜滑灏嗛氳繃涓浜涘疄渚嬫潵瀛︿範 netcat 鍛戒护銆?/p>

1. 鍦ㄦ湇鍔″櫒-瀹㈡埛绔灦鏋勪笂浣跨敤 Netcat

netcat 宸ュ叿鍙繍琛屼簬鏈嶅姟鍣ㄦā寮忥紝渚﹀惉鎸囧畾绔彛

$ nc -l 2389

鐒跺悗浣犲彲浠ヤ娇鐢ㄥ鎴风妯″紡鏉ヨ繛鎺ュ埌 2389 绔彛锛?/p>

$ nc localhost 2389

鐜板湪濡傛灉浣犺緭鍏ヤ竴浜涙枃鏈紝瀹冨皢琚彂閫佸埌鏈嶅姟鍣ㄧ锛?/p>

$ nc localhost 2389
HI, oschina

鍦ㄦ湇鍔″櫒鐨勭粓绔獥鍙e皢浼氭樉绀轰笅闈㈠唴瀹癸細

$ nc -l 2389
HI, oschina

2. 浣跨敤 Netcat 鏉ヤ紶杈撴枃浠?/h3>

netcat 宸ュ叿杩樺彲鐢ㄦ潵浼犺緭鏂囦欢锛屽湪瀹㈡埛绔紝鍋囪鎴戜滑鏈変竴涓?testfile 鏂囦欢锛?/p>

$ cat testfile
hello oschina

鑰屽湪鏈嶅姟鍣ㄧ鏈変竴涓┖鏂囦欢鍚嶄负 test

鐒跺悗鎴戜滑浣跨敤濡備笅鍛戒护鏉ュ惎鐢ㄦ湇鍔″櫒绔細

$ nc -l 2389 > test

绱ф帴鐫杩愯瀹㈡埛绔細

cat testfile | nc localhost 2389

鐒跺悗浣犲仠姝㈡湇鍔″櫒绔紝浣犲彲浠ユ煡鐪?test 鍐呭灏辨槸鍒氭墠瀹㈡埛绔紶杩囨潵鐨?testfile 鏂囦欢鐨勫唴瀹癸細

$ cat test
hello oschina

3. Netcat 鏀寔瓒呮椂鎺у埗

澶氭暟鎯呭喌鎴戜滑涓嶅笇鏈涜繛鎺ヤ竴鐩翠繚鎸侊紝閭d箞鎴戜滑鍙互浣跨敤 -w 鍙傛暟鏉ユ寚瀹氳繛鎺ョ殑绌洪棽瓒呮椂鏃堕棿锛岃鍙傛暟绱ф帴涓涓暟鍊硷紝浠h〃绉掓暟锛屽鏋滆繛鎺ヨ秴杩囨寚瀹氭椂闂村垯杩炴帴浼氳缁堟銆?/p>

鏈嶅姟鍣?

nc -l 2389

瀹㈡埛绔?

$ nc -w 10 localhost 2389

璇ヨ繛鎺ュ皢鍦?10 绉掑悗涓柇銆?/p>

娉ㄦ剰: 涓嶈鍦ㄦ湇鍔″櫒绔悓鏃朵娇鐢?-w 鍜?-l 鍙傛暟锛屽洜涓?-w 鍙傛暟灏嗗湪鏈嶅姟鍣ㄧ鏃犳晥鏋溿?/p>

4. Netcat 鏀寔 IPv6

netcat 鐨?-4 鍜?-6 鍙傛暟鐢ㄦ潵鎸囧畾 IP 鍦板潃绫诲瀷锛屽垎鍒槸 IPv4 鍜?IPv6锛?/p>

鏈嶅姟鍣ㄧ锛?/p>

$ nc -4 -l 2389

瀹㈡埛绔細

$ nc -4 localhost 2389

鐒跺悗鎴戜滑鍙互浣跨敤 netstat 鍛戒护鏉ユ煡鐪嬬綉缁滅殑鎯呭喌锛?/p>

$ netstat | grep 2389
tcp        0      0 localhost:2389          localhost:50851         ESTABLISHED
tcp        0      0 localhost:50851         localhost:2389          ESTABLISHED

鎺ヤ笅鏉ユ垜浠湅鐪婭Pv6 鐨勬儏鍐碉細

鏈嶅姟鍣ㄧ锛?/p>

$ nc -6 -l 2389

瀹㈡埛绔細

$ nc -6 localhost 2389

鍐嶆杩愯 netstat 鍛戒护锛?/p>

$ netstat | grep 2389
tcp6       0      0 localhost:2389          localhost:33234         ESTABLISHED
tcp6       0      0 localhost:33234         localhost:2389          ESTABLISHED

鍓嶇紑鏄?tcp6 琛ㄧず浣跨敤鐨勬槸 IPv6 鐨勫湴鍧銆?/p>

5. 鍦?Netcat 涓姝粠鏍囧噯杈撳叆涓鍙栨暟鎹?/h3>

璇ュ姛鑳戒娇鐢?-d 鍙傛暟锛岃鐪嬩笅闈緥瀛愶細

鏈嶅姟鍣ㄧ锛?/p>

$ nc -l 2389

瀹㈡埛绔細

$ nc -d localhost 2389
Hi

浣犺緭鍏ョ殑 Hi 鏂囨湰骞朵笉浼氶佸埌鏈嶅姟鍣ㄧ銆?/p>

6. 寮哄埗 Netcat 鏈嶅姟鍣ㄧ淇濇寔鍚姩鐘舵?/h3>

濡傛灉杩炴帴鍒版湇鍔″櫒鐨勫鎴风鏂紑杩炴帴锛岄偅涔堟湇鍔″櫒绔篃浼氳窡鐫閫鍑恒?/p>

鏈嶅姟鍣ㄧ锛?/p>

$ nc -l 2389

瀹㈡埛绔細

$ nc localhost 2389
^C

鏈嶅姟鍣ㄧ锛?/p>

$ nc -l 2389
$

涓婅堪渚嬪瓙涓紝浣嗗鎴风鏂紑鏃舵湇鍔″櫒绔篃绔嬪嵆閫鍑恒?/p>

鎴戜滑鍙互閫氳繃 -k 鍙傛暟鏉ユ帶鍒惰鏈嶅姟鍣ㄤ笉浼氬洜涓哄鎴风鐨勬柇寮杩炴帴鑰岄鍑恒?/p>

鏈嶅姟鍣ㄧ锛?/p>

$ nc -k -l 2389

瀹㈡埛绔細

$ nc localhost 2389
^C

鏈嶅姟鍣ㄧ锛?/p>

$ nc -k -l 2389

7. 閰嶇疆 Netcat 瀹㈡埛绔笉浼氬洜涓?EOF 鑰岄鍑?/h3>

Netcat 瀹㈡埛绔彲浠ラ氳繃 -q 鍙傛暟鏉ユ帶鍒舵帴鏀跺埌 EOF 鍚庨殧澶氶暱鏃堕棿鎵嶉鍑猴紝璇ュ弬鏁扮殑鍗曚綅鏄锛?/p>

瀹㈡埛绔娇鐢ㄥ涓嬫柟寮忓惎鍔細

nc  -q 5  localhost 2389

鐜板湪濡傛灉瀹㈡埛绔帴鏀跺埌 EOF 锛屽畠灏嗙瓑寰?5 绉掑悗閫鍑恒?/p>

8. 浣跨敤 Netcat 鏉ュ鐞?UDP 鍗忚

netcat 榛樿鏄娇鐢?TCP 鍗忚锛屼絾涔熸敮鎸?UDP锛屽彲浣跨敤 -u 鍙傛暟鏉ュ惎鐢?UDP 鍗忚閫氳銆?/p>

鏈嶅姟鍣ㄧ锛?/p>

$ nc -4 -u -l 2389

瀹㈡埛绔細

$ nc -4 -u localhost 2389

杩欐牱瀹㈡埛绔拰鏈嶅姟鍣ㄧ閮戒娇鐢ㄤ簡 UDP 鍗忚锛屽彲閫氳繃 netstat 鍛戒护鏉ユ煡鐪嬶細

$ netstat | grep 2389
udp        0      0 localhost:42634         localhost:2389          ESTABLISHED

鑻辨枃鍘熸枃锛?span class="Apple-converted-space">聽OSCHINA鍘熷垱缈昏瘧


(本文来自互联网,不代表搜站(http://www.ylzx8.cn/)的观点和立场)
本站所有内容来自互联网,若本站收录的信息无意侵犯了贵司版权,请给我们来信(ylzx8cn@163.com),我们会及时处理和回复,谢谢