10.13 nc:多功能网络工具
生活随笔
收集整理的這篇文章主要介紹了
10.13 nc:多功能网络工具
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
nc命令
是一個簡單、可靠、強大的網絡工具,它可以建立TCP連接,發送UDP數據包,監聽任意的TCP和UDP端口,進行端口掃描,處理IPv4和IPv6數據包。 如果系統沒有nc命令,那么可以手動安裝,安裝命令為yum -y install nc。? ???nc命令的參數選項及說明
-l?? ?指定監聽端口,然后一直等待網絡連接 -z?? ?表示zero,表示掃描時不發送任何數據 -v?? ?顯示詳細輸出 -w?? ?設置超時時間,對-l選項失效 -p?? ?指定nc命令使用的端口,不能和-l選項一起使用,可能引起錯誤 -u?? ?使用UDP連接,默認是TCP連接 -s?? ?指定發送數據的源IP地址,適用于多網卡主機 [root@cs6 ~]# /etc/init.d/iptables status 表格:filter Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 5 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibitedChain FORWARD (policy ACCEPT) num target prot opt source destination 1 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibitedChain OUTPUT (policy ACCEPT) num target prot opt source destination [root@cs6 ~]# /etc/init.d/iptables stop iptables:將鏈設置為政策 ACCEPT:filter [確定] iptables:清除防火墻規則: [確定] iptables:正在卸載模塊: [確定] [root@cs6 ~]# setenforce 0 [root@cs6 ~]# getenforce Permissive模擬 TCP 連接并傳輸文本內容(遠程復制文件
[root@cs6 ~]# nc -l 12345 > lewen.nc #<=監聽12345端口,將數據寫入oldboy.nc #<=執行完上面的命令后,當前窗口掛起。 #<=新開一個窗口執行命令。 [root@cs6 ~]# cat lewen.nc #<=首先查看12345端口。 [root@cs6 ~]# netstat -lntup|grep 12345 tcp 0 0 0.0.0.0:12345 0.0.0.0:* LISTEN 2612/nc [root@cs6 ~]# cat lewen.log #<=待用的文件。 6.8.0 #<=文件中的內容 [root@cs6 ~]# nc 10.0.0.100 12345 < lewen.log #<=使用nc命令向10.0.0.100 主機12345 端口傳輸lewen.log文件 [root@cs6 ~]# netstat -lntup|grep 12345 #<==nc命令傳輸完數據后自動終止。 [root@cs6 ~]# cat lewen.nc #檢查結果 6.8.0用Shell 模擬一個簡單的Web 服務器效果案例
[root@cs6 ~]# echo "I love linux www.wenyule.top" >test.txt [root@cs6 ~]# vim web.sh #!/bin/bash while truedonc -l 80 < test.txt # 一直監聽80端口,test.txt是發送給用戶的內容。 done[root@cs6 ~]# sh web.sh &>/dev/null & [1] 2685 [root@cs6 ~]#[root@cs6 ~]# netstat -lntup|grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2686/nc [root@cs6 ~]# curl 127.0.0.1 I love linux www.wenyule.top手動與HTTP服務器建立連接的例子。
[root@cs6 ~]# nc blog.oldboyedu.com 80 GET /about-us/ HTTP/1.1 <=粘貼這兩行語句,速度要快,如果慢了程序就會超時自動退出。 host:blog.oldboyedu.com <==HTTP/1.1的要求必須寫明hosto #<==敲兩次回車確認發送請求報文,下面就是響應報文的內容。 HTTP/1.1 301 Moved Permanently Server: nginx Date: Tue, 07 May 2019 13:40:50 GMT Content-Type: text/html Content-Length: 178 Connection: keep-alive Location: https://blog.oldboyedu.com/about-us/<html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx</center> </body> </html>利用nc 進行端口掃描
[root@cs6 ~]# nc -z 10.0.0.100 20-30 Connection to 10.0.0.100 22 port [tcp/ssh] succeeded! [root@cs6 ~]# nc -z 10.0.0.100 22 Connection to 10.0.0.100 22 port [tcp/ssh] succeeded! [root@cs6 ~]# nc -zv 10.0.0.100 20-30 #<=使用-v選項詳細顯示掃描過程 nc: connect to 10.0.0.100 port 20 (tcp) failed: Connection refused nc: connect to 10.0.0.100 port 21 (tcp) failed: Connection refused Connection to 10.0.0.100 22 port [tcp/ssh] succeeded! nc: connect to 10.0.0.100 port 23 (tcp) failed: Connection refused nc: connect to 10.0.0.100 port 24 (tcp) failed: Connection refused nc: connect to 10.0.0.100 port 25 (tcp) failed: Connection refused nc: connect to 10.0.0.100 port 26 (tcp) failed: Connection refused nc: connect to 10.0.0.100 port 27 (tcp) failed: Connection refused nc: connect to 10.0.0.100 port 28 (tcp) failed: Connection refused nc: connect to 10.0.0.100 port 29 (tcp) failed: Connection refused nc: connect to 10.0.0.100 port 30 (tcp) failed: Connection refused?使用nc命令,模擬QQ聊天工具聊天
打開兩個命令行窗口,模擬兩個人聊天的場景。 首先在第一個窗口執行如下命令,執行完會hang住等待輸入狀態: [root@cs6 ~]# nc -l 12345 然后在第2個窗口執行如下命令,執行完也會hang住等待輸入狀態: [root@cs6 ~]# nc 127.0.0.1 12345 此時兩個窗口都等待輸入內容。我們先新建第3個窗口,查看他們建立的網絡連接: [root@cs6 ~]# netstat -ntp|grep nc #<=12345端口是nc指定開放的,52978端口是系統為了和12345就口通信隨機開放的,當然也可以使用-p選項指定開放端口。 tcp????????0??????0 127.0.0.1:12345?????????????127.0.0.1:37060?????????????ESTABLISHED 2748/nc????????????? tcp????????0??????0 127.0.0.1:37060?????????????127.0.0.1:12345?????????????ESTABLISHED 2749/nc??? 怎么聊天呢?很簡單,你在第一個窗口中輸入想要說的話,然后敲回車鍵,悄悄話就會自動發送到對方(第二個窗口),和QQ的效果一樣: [root@cs6 ~]# nc -l 12345 hi,I am lewen 對方(第二個窗口)也只需要輸入回復的話然后敲回車鍵,消息就能發送給你: [root@cs6 ~]# nc 127.0.0.1 12345 hi,I am lewen hello,lewen總結
以上是生活随笔為你收集整理的10.13 nc:多功能网络工具的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 6.6 rsync:文件同步工具
- 下一篇: 10.2-3 ifupifdown:激活