Try   HackMD

網路技術小組 讀書會 2023.7.7

  • 日期:2023.7.7 (五)

ping

『高手救命!我的 Linux 不能連上網路了!』 - 魯迅(沒説過)

我們可以使用測試軟體來追蹤網路錯誤原因,而很多的網路偵測指令其實都在 Linux 裡,其中我們要介紹ping這個指令。

ping 主要透過 ICMP 封包 來進行整個網路的狀況報告,ping 還需透過 IP 封包來傳送 ICMP 封包,而 IP 封包裡面有個相當重要的 TTL 屬性。

IP 封包
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →
ICMP
網際網路訊息控制協定 (Internet Control Message Protocol)

格式(語法)

# ping [option & parameter] IP
option & parameter (選項與參數)
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
  • -c [數值] : 執行 ping 的次數,例如 -c 3
  • -i [秒數]:設定間隔幾秒送一個網絡封包給主機,預設值是一秒送一次
  • -s [數值] : 發送出去的 ICMP 封包大小,預設為 56bytes,可以放大此一數值
  • -t [數值] : TTL 的數值,預設是 255,每經過一個節點就會減掉1
  • -n : 在輸出資料時不進行 IP 與主機名稱的反查,直接使用 IP 輸出(速度較快)
  • -W [數值]:等待回應對方主機的秒數
  • -M [do|dont] : 主要偵測網路的 MTU 大小 (Maximum Transmission Unit/最大傳輸單位)
    • do : 代表傳送一個 DF (Don't Fragment) 旗標,讓封包不能重新拆包與打包
    • dont: 代表不要傳送 DF 旗標,表示封包可以在其他主機上拆包與打包

示範
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

ping 163.22.17.162

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

ping 會持續不斷,用 Ctrl-C 來中斷 ping

解析
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • 64 bytes : 表示這次傳送的 ICMP 封包大小為 64 bytes (預設值)
  • icmp_seq=1 : ICMP 所偵測進行的次數,第一次編號為 1
  • ttl=64 : 表示 ping 請求經過了64個網絡節點的路徑,它是生存時間字段的值,用於控制數據包在網絡中的存活時間
    • 可以作為網絡的拓撲結構的一種指示,因為每個網絡節點都會減少TTL的值。
  • time=0.022 ms : 回應時間,單位有 ms (0.001秒)及 us (0.000001秒), 一般來說越小的回應時間,表示兩部主機之間的網路連線越良好

ping 就是傳送 ICMP 封包去要求對方主機回應是否存在於網路環境中,若傳送成功並主機有接受到,表示這部 DNS 主機是存在;反之,這部 DNS 主機不存在伺服器的網路環境中


指定次數 -c

ping -c 3 www.google.com

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

ping www.google.com 3 次後自動退出


時間間隔 -i

ping -c 15 -i 0.5 163.22.17.162

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

每隔 0.5 秒的時間間隔,ping 163.22.17.162,到達 15 次數後退出


多參數使用

ping -c 5 -i 0.1 -s 1024 -t 255 www.google.com

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  • -c 次數限制爲 5
  • -i 發送週期爲 0.1 秒
  • -s 設置發送包的大小爲 1024
  • -t 設置TTL值爲 255

traceroute

ping 是兩部主機之間的回聲與否判斷, traceroute 是可以追蹤兩部主機之間通過的各個節點 (node) 通訊狀況的好壞,可以檢查網路環境

traceroute 指令可追蹤網絡數據包的路由途徑,預設數據包大小是40 Bytes

格式(語法)

# traceroute [option & parameter] IP
option & parameter (選項與參數)
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
  • -n :可直接使用IP地址而非主機名稱
  • -m [數值] :設置檢測數據包跳數次數
  • -q [數值] :設置探測包的個數
  • -w [秒數] :設置等待遠端主機回報的時間,預設是 5 秒
  • -U :使用 UDP 的 port 33434 來進行偵測(traceroute 預設偵測協定)
  • -T :使用 TCP 來進行偵測,一般使用 port 80 測試
  • -p [埠號]:設置UDP傳輸協議的通信端口,若不想使用 UDP 與 TCP 的預設埠號來偵測,可在此改變埠號
  • -I :使用 ICMP 的方式來進行偵測
  • -i [裝置]:使用指定的網絡界面送出數據包
    • 用在比較複雜的環境,如果你的網路介面很多很複雜時,才會用到這個參數
    • 舉例來說,你有兩條 ADSL 可以連接到外部,那你的主機會有兩個 ppp,你可以使用 -i 來選擇是 ppp0 還是 ppp1
  • -g [路由]:設置來源路由網關,最多可設置8個,與 -i 的參數相仿,只是 -g 後面接的是 gateway 的 IP 就是了

示範
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

traceroute 常見的用法:

traceroute www.google.com

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

解析
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • 偵測本機到 www.google.com 去的各節點(共 11 個)連線狀態
  • traceroute 會主動地對這 11 個節點做 UDP 的回聲等待,並偵測回覆的時間,每節點偵測三次,最後會回傳上面圖片顯示的結果

有些 Linux 系統(如:Centos7)沒有安裝上 traceroute 命令工具,若沒有安裝,先登入管理員 (root) 用戶,使用 yum install traceroute 命令來安裝即可。


顯示IP地址,不查主機名 -n

traceroute -n www.google.com

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →


設置探測包的個數 -q

traceroute -n -q 1 www.google.com

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →


跳數設置 -m

traceroute -n -m 5 www.google.com

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →


對外發探測包的等待響應時間設置 -w

traceroute -n -m 15 -w 0.5 tw.yahoo.com

*** 表示該節點可能設有某些防護措施(如:防火牆),讓我們發送的封包資訊被丟棄所致, traceroute 預設使用的是 UDP 封包,可嘗試使用其他封包, 如:-T-I-p [埠號]

*lilina 會有權限問題,不能使用 -T-I

iwlist, iwconfig

  • iwlist:利用無線網卡進行無線 AP 的偵測與取得相關的資料
  • iwconfig:設定無線網卡的相關參數,和 ifconfig 是同級別的用戶級管理工具,但專注於無線網絡管理


*沒有無線網卡

iwlist iwconfig 這兩個指令必須要有無線網卡才能夠進行,可以打 man iwlist , man iwconfig 這個命令瞭解詳情

dhclient

dhclient [無線網卡名稱]

如果你是使用 DHCP 協定在區域網路內取得 IP 的話,可以使用 dhclient 這個指令,發送 dhcp 要求工作,必須要有無線網卡才能夠進行,可以打 man dhclient 這個指令瞭解詳情

參考資料