如何ping和使用特定的网卡

One*_*ero 11 networking route

我有 3 个网卡,1 个 LAN(有线),1 个无线网卡和 1 个无线 USB

我如何从特定网卡ping通?

以及如何将特定网卡用于特定应用程序

例子

i want to ping google from wlan1
Run Code Online (Sandbox Code Playgroud)

特定应用示例

i want to use firefox or transmission from wan1
Run Code Online (Sandbox Code Playgroud)

Lan ip 192.168.0.2 > 工作正常 在此处输入图片说明 pin -I wlan1 google.com 在此处输入图片说明

route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 eth0
172.16.221.0    0.0.0.0         255.255.255.0   U     0      0        0 vmnet8
192.168.0.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
192.168.0.0     0.0.0.0         255.255.255.0   U     2      0        0 wlan1
192.168.48.0    0.0.0.0         255.255.255.0   U     0      0        0 vmnet1
Run Code Online (Sandbox Code Playgroud)

one@onezero:~$ ip 路由

default via 192.168.0.1 dev eth0  proto static 
169.254.0.0/16 dev eth0  scope link  metric 1000 
172.16.221.0/24 dev vmnet8  proto kernel  scope link  src 172.16.221.1 
192.168.0.0/24 dev eth0  proto kernel  scope link  src 192.168.0.2  metric 1 
192.168.0.0/24 dev wlan1  proto kernel  scope link  src 192.168.0.3  metric 2 
192.168.48.0/24 dev vmnet1  proto kernel  scope link  src 192.168.48.1 
Run Code Online (Sandbox Code Playgroud)

@Khaled

one@onezero:~$ ping -S 192.168.0.2  hotmail.com
PING hotmail.com (65.55.72.135) 56(84) bytes of data.
64 bytes from origin.sn131w.snt131.mail.live.com (65.55.72.135): icmp_req=1 ttl=236 time=391 ms
64 bytes from origin.sn131w.snt131.mail.live.com (65.55.72.135): icmp_req=2 ttl=236 time=296 ms
64 bytes from origin.sn131w.snt131.mail.live.com (65.55.72.135): icmp_req=3 ttl=236 time=393 ms
64 bytes from origin.sn131w.snt131.mail.live.com (65.55.72.135): icmp_req=4 ttl=236 time=352 ms
Run Code Online (Sandbox Code Playgroud)
 ping -S 192.168.0.3  hotmail.com
PING hotmail.com (65.55.72.183) 56(84) bytes of data.
64 bytes from origin.sn134w.snt134.mail.live.com (65.55.72.183): icmp_req=1 ttl=236 time=312 ms
64 bytes from origin.sn134w.snt134.mail.live.com (65.55.72.183): icmp_req=2 ttl=236 time=457 ms
64 bytes from origin.sn134w.snt134.mail.live.com (65.55.72.183): icmp_req=3 ttl=236 time=298 ms
64 bytes from origin.sn134w.snt134.mail.live.com (65.55.72.183): icmp_req=5 ttl=236 time=330 ms
64 bytes from origin.sn134w.snt134.mail.live.com (65.55.72.183): icmp_req=6 ttl=236 time=300 ms
Run Code Online (Sandbox Code Playgroud)

现在最后是应用程序问题

小智 9

如果您查看 ping 手册man ping,您可以阅读:

-I interface address
   Set source address to specified interface address. Argument may be numeric IP
   address or name of device.
Run Code Online (Sandbox Code Playgroud)


res*_*on6 5

检查您的路由表。

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth0
192.168.0.0     0.0.0.0         255.255.255.0   U     2      0        0 wlan1
Run Code Online (Sandbox Code Playgroud)

您的 wlan1 接口只知道如何访问 192.168.0.0 网络。您的 wlan1 和 eth0 也位于同一子网中,这可能会导致问题。您需要将路由添加到路由表中,以便您可以在 wlan 接口上访问任何目的地。例如

route add -host 65.55.72.135 gw 192.168.0.1 dev wlan1
ping -I wlan1 65.55.72.135
Run Code Online (Sandbox Code Playgroud)

请注意,这不会让您按应用程序路由。为此,您需要使用类似命令配置策略路由,iptables -m owner --uid-ownerping -S src_ip dest_ip命令实际上将使用 wlan1 IP 地址的源发送数据包,然后将数据包路由出 eth0,因为下一跳是路由表中的 eth0。最好的办法是将 wlan1 和 eth0 接口放在不同的子网上。


kaj*_*aji 4

BSD 的 ping(8)而言,您可以使用-Sping 开关来模拟来自特定接口的 ping:

-S src_addr
             Use the following IP address as the source address in outgoing packets.  On hosts
             with more than one IP address, this option can be used to force the source address to
             be something other than the IP address of the interface the probe packet is sent on.
             If the IP address is not one of this machine's interface addresses, an error is
             returned and nothing is sent.
Run Code Online (Sandbox Code Playgroud)

如果我们为不同的接口指定不同的网关或根据端口或其他标准通过防火墙规则重定向流量...例如,网络接口(IP 地址)的应用程序级别感知在某种程度上是可能的。如果您使用 Firefox 仅连接到 80 端口,那么您可以在 iptables 中指定 SNAT 规则,以通过您指定的 IP 地址进行连接,从而获得所需的接口