目錄
1. 為什么要使用知道那個網卡ping操作
2. 使用指定網卡ping操作
3. 總結
1、 為什么要使用指定網卡ping操作
現在很多服務器都擁有雙網卡甚至多網卡,有些是為了保持高可用,有些是網絡隔離(內網與外網或者不同網絡)
很多學者在模擬企業級環境時也為一台虛擬機配置了雙網卡,一塊NAT模式,一塊LAN區段模式,如果使用簡單的ping ip形式的命令得到ping通的結果,只能證明兩個虛擬機可以ping通,那究竟是使用了哪塊網卡去ping另一個虛擬機?
2、實現使用指定網卡進行ping操作
(1) 查ping的幫助
遇到問題首先就想到查看命令幫助,獲取到的幫助如下:
1 # ping --help 2 ping: invalid option -- '-' 3 Usage: ping [-aAbBdDfhLnOqrRUvV64] [-c count] [-i interval] [-I interface] 4 [-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos] 5 [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option] 6 [-w deadline] [-W timeout] [hop1 ...] destination 7 Usage: ping -6 [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface] 8 [-l preload] [-m mark] [-M pmtudisc_option] 9 [-N nodeinfo_option] [-p pattern] [-Q tclass] [-s packetsize] 10 [-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline] 11 [-W timeout] destination
一眼就看到了這個 -I 接口 這個選項(也就這個沾點邊),其他完全沒有看出任何可以指定網卡設備的可能,接下來查看下man幫助,搜索 -I 這個選項查看詳細解釋。
1 # man ping 2 -I interface 3 interface is either an address, or an interface name. If interface is an 4 address, it sets source address to specified interface address. If interface in 5 an interface name, it sets source interface to specified interface. For IPv6, 6 when doing ping to a link-local scope address, link specification (by the 7 '%'-notation in destination, or by this option) is required.
本人英文不好,百度翻譯了一下,大概意思為:接口可以是地址,也可以是接口名。如果接口是4地址,將源地址設置為指定的接口地址。
也就是說, -I 接口 這個接口可以是網卡設備名,也可以是網卡設備的IP地址。接下來進行試驗
(2) 使用 -I 選項實現指定網卡進行ping操作
我的linux虛擬機(主機A)有兩張網卡,一張是NAT(192.168.187.10),另一張是LAN區段(172.16.0.10)
第二台虛擬機(主機B),一張NAT(192.168.187.11),另一張是LAN區段(172.16.0.11)
正常情況下 主機A ping 主機B 是可以ping通的,但是不知道使用的是那張網卡:
1 # ping 192.168.187.11 2 PING 192.168.187.11 (192.168.187.11) 56(84) bytes of data. 3 64 bytes from 192.168.187.11: icmp_seq=1 ttl=64 time=1.21 ms 4 64 bytes from 192.168.187.11: icmp_seq=2 ttl=64 time=0.686 ms
使用IP地址指定主機A的NAT網卡ping主機B的LAN區段網卡(理論不能ping通):
1 # ping -I 192.168.187.10 172.16.0.11 2 PING 172.16.0.11 (172.16.0.11) from 192.168.187.10 : 56(84) bytes of data. 3 From 192.168.187.10 icmp_seq=1 Destination Host Unreachable
實際確實ping不通,而且顯示 PING 172.16.0.11 (172.16.0.11) from 192.168.187.10 證明ping操作是來自IP地址為192.168.187.10這塊網卡的,實驗成功
另一種方式,使用主機A的NAT網卡名ping主機B的NAT網卡(理論可以ping通):
1 # ping -I ens33 192.168.187.11 2 PING 192.168.187.11 (192.168.187.11) from 192.168.187.10 ens33: 56(84) bytes of data. 3 64 bytes from 192.168.187.11: icmp_seq=1 ttl=64 time=18.1 ms 4 64 bytes from 192.168.187.11: icmp_seq=2 ttl=64 time=4.11 ms
實驗成功,顯示的也是 ping 192.168.187.11 來自 192.168.187.10 ens33網卡。
3、總結
ping命令可以使用指定網卡進行ping操作
用法是:
1 # ping -I 本機指定網卡名或網卡的IP地址 目標地址
個人公眾號(linuxjsz)
專注IT技術、知識分享,面試資源共享、講解
只做全網最比心的公眾號,歡迎你的關注!