?📚?博主的專欄
🐧?Linux???|?? 🖥??C++???|?? 📊?數據結構??|?💡C++ 算法?|?🅒?C 語言? |?🌐?計算機網絡
相關文章:計算機網絡專欄
目錄
ping(檢測網絡連通性)
netstat(網絡狀態、用來查看網絡服務)
pidof [進程名](查看服務器的進程 id 時非常方便)
ping(檢測網絡連通性)
window和云服務器linux是否能連通
ping后面跟上目標網址對應的域名或者ip地址。
pupu@VM-8-15-ubuntu:~/computer-network/class_44/1.udp_echo_server$ ping www.baidu.com PING www.a.shifen.com (220.181.111.1) 56(84) bytes of data. 64 bytes from 220.181.111.1 (220.181.111.1): icmp_seq=1 ttl=251 time=2.82 ms 64 bytes from 220.181.111.1 (220.181.111.1): icmp_seq=2 ttl=251 time=2.83 ms 64 bytes from 220.181.111.1 (220.181.111.1): icmp_seq=3 ttl=251 time=2.83 ms ^C --- www.a.shifen.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 2.818/2.827/2.834/0.006 ms
默認ping開始后是不會停止的,因此可以加上選項c(count次數)num
pupu@VM-8-15-ubuntu:~/computer-network/class_44/1.udp_echo_server$ ping -c1 www.baidu.com PING www.a.shifen.com (110.242.70.57) 56(84) bytes of data. 64 bytes from 110.242.70.57 (110.242.70.57): icmp_seq=1 ttl=251 time=16.5 ms--- www.a.shifen.com ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 16.545/16.545/16.545/0.000 ms
netstat(網絡狀態、用來查看網絡服務)
常用選項:
? n 拒絕顯示別名, 能顯示數字的全部轉化成數字
? l 僅列出有在 Listen (監聽) 的服務狀態
? p 顯示建立相關鏈接的程序名
? t (tcp)僅顯示 tcp 相關選項? u (udp)僅顯示 udp 相關選項
? a (all)顯示所有選項, 默認不顯示 LISTEN 相關
準備好一個udp或者tcp的服務器,開啟服務器之后使用netstat【選項】可以查看是否運行
例如現在:“已有一個udp服務”:在開啟服務器(端口號8899)后,這里就可以看到udp服務的相關信息以及看到相關進程(udpserver)的信息
pupu@VM-8-15-ubuntu:~/computer-network/class_44/1.udp_echo_server$ netstat -aup (Not all processes could be identified, non-owned process infowill not be shown, you would have to be root to see it all.) Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name udp 0 0 localhost:8899 0.0.0.0:* 3500381/./udpserver udp 0 0 localhost:domain 0.0.0.0:* - udp 0 0 VM-8-15-ubuntu:bootpc 0.0.0.0:* - udp 0 0 VM-8-15-ubuntu:ntp 0.0.0.0:* - udp 0 0 localhost:ntp 0.0.0.0:* - udp6 0 0 VM-8-15-ubuntu:ntp [::]:* - udp6 0 0 ip6-localhost:ntp [::]:* -
注意:當前只能查到(看到相關信息)我自己這個pupu身份啟動的進程,其他是不允許我看到的,是系統本身自啟動的,因此我們提權,想看到數字的ip地址就帶n
pupu@VM-8-15-ubuntu:~/computer-network/class_44/1.udp_echo_server$ sudo netstat -aup Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name udp 0 0 localhost:8899 0.0.0.0:* 3500381/./udpserver udp 0 0 localhost:domain 0.0.0.0:* 826/systemd-resolve udp 0 0 VM-8-15-ubuntu:bootpc 0.0.0.0:* 824/systemd-network udp 0 0 VM-8-15-ubuntu:ntp 0.0.0.0:* 411559/ntpd udp 0 0 localhost:ntp 0.0.0.0:* 411559/ntpd udp6 0 0 VM-8-15-ubuntu:ntp [::]:* 411559/ntpd udp6 0 0 ip6-localhost:ntp [::]:* 411559/ntpd pupu@VM-8-15-ubuntu:~/computer-network/class_44/1.udp_echo_server$ sudo netstat -anup [sudo] password for pupu: Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name udp 0 0 127.0.0.1:8899 0.0.0.0:* 3500381/./udpserver udp 0 0 127.0.0.53:53 0.0.0.0:* 826/systemd-resolve udp 0 0 10.2.8.15:64 0.0.0.0:* 824/systemd-network udp 0 0 10.2.8.15:123 0.0.0.0:* 411559/ntpd udp 0 0 127.0.0.1:123 0.0.0.0:* 411559/ntpd udp6 0 0 fe80::5054:ff:fe45::123 :::* 411559/ntpd udp6 0 0 ::1:123 :::* 411559/ntpd pupu@VM-8-15-ubuntu:~/computer-network/class_44/1.udp_echo_server$
watch:n 是刷新的次數 ,可以定期檢測,指定命令的執行
每個 1s 執行一次 netstat -aup $ watch -n 1 netstat -aup
pidof [進程名](查看服務器的進程 id 時非常方便)
想要關閉掉進程的時候需要知道進程的pid,之前使用的都是這樣的方法:
pupu@VM-8-15-ubuntu:~/computer-network/class_44/1.udp_echo_server$ ps -ajx | grep udpserver 3485448 3506444 3506444 3485448 pts/8 3506444 S+ 1002 0:00 ./udpserver 3439936 3507463 3507462 3439936 pts/0 3507462 S+ 1002 0:00 grep --color=auto
我們可以直接使用pidof 指定進程的名稱,就可以拿到,當前進程的pid:
pupu@VM-8-15-ubuntu:~/computer-network/class_44/1.udp_echo_server$ pidof udpserver 3506444
再直接kill掉進程:
kill -9 3506444
或者:管道是一個文件,當pidof將管道傳給kill命令時,他是通過自己的標準輸入文件描述符0,來把數據讀到kill命令里面的,|命令是將命令行參數也就是進程pid放到kill命令的命令行參數,實際上xargs就相當于把從管道傳過來的數據,傳給后續命令的命令行參數上,拼接上。
pidof udpserver | xargs kill -9
結語:
? ? ? ?隨著這篇博客接近尾聲,我衷心希望我所分享的內容能為你帶來一些啟發和幫助。學習和理解的過程往往充滿挑戰,但正是這些挑戰讓我們不斷成長和進步。我在準備這篇文章時,也深刻體會到了學習與分享的樂趣。 ? ?
? ? ? ? ?在此,我要特別感謝每一位閱讀到這里的你。是你的關注和支持,給予了我持續寫作和分享的動力。我深知,無論我在某個領域有多少見解,都離不開大家的鼓勵與指正。因此,如果你在閱讀過程中有任何疑問、建議或是發現了文章中的不足之處,都歡迎你慷慨賜教。
? ? ? ? 你的每一條反饋都是我前進路上的寶貴財富。同時,我也非常期待能夠得到你的點贊、收藏,關注這將是對我莫大的支持和鼓勵。當然,我更期待的是能夠持續為你帶來有價值的內容。