我已经来回玩了几天的路由游戏。
由于很难用语言描述,我做了一个图表以下:
我想要实现的是能够从 Rpi 3B ping Rpi 4B,反之亦然。但是,使用默认设置,我只能从 Rpi 3B eth0 访问主机 A eth0。
注意:我无法控制路由器。因此,所有路由/转发都应在主机 A/B 和 Rpi 3B/4B 内进行。
下面是我采取的步骤,这些步骤使我更进一步,但我不知道是否正确:
在主机 A 上设置路由规则,这使我能够从 Rpi 3B eth0 访问主机 B eth1:
sudo ip route add 192.168.10.0/24 via 192.168.18.70
在主机 B 上设置路由规则,这使我能够从 Rpi 4B eth0 访问主机 A eth1:
sudo ip route add 192.168.11.0/24 via 192.168.18.69
但我仍然无法 ping 通 Rpi 3B eth0 <--> eth0 Rpi 4B。欢迎提供任何建议和解释?
编辑:配置主机A(以及主机B):
user@localhost:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:04:9f:07:3a:df brd ff:ff:ff:ff:ff:ff
inet 192.168.18.69/24 brd 192.168.18.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::204:9fff:fe07:3adf/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:04:9f:07:3a:e0 brd ff:ff:ff:ff:ff:ff
inet 192.168.11.1/24 brd 192.168.11.255 scope global eth1
valid_lft forever preferred_lft forever
inet6 fe80::204:9fff:fe07:3ae0/64 scope link
valid_lft forever preferred_lft forever
4: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
link/sit 0.0.0.0 brd 0.0.0.0
5: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:a1:01:e8:50 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
user@localhost:~$ ip route
default via 192.168.18.1 dev eth0
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown
192.168.10.0/24 via 192.168.18.70 dev eth0
192.168.11.0/24 dev eth1 proto kernel scope link src 192.168.11.1
192.168.18.0/24 dev eth0 proto kernel scope link src 192.168.18.69
user@localhost:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:04:9f:07:3a:39 brd ff:ff:ff:ff:ff:ff
inet 192.168.18.70/24 brd 192.168.18.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::204:9fff:fe07:3a39/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:04:9f:07:3a:3a brd ff:ff:ff:ff:ff:ff
inet 192.168.10.1/24 brd 192.168.10.255 scope global eth1
valid_lft forever preferred_lft forever
inet6 fe80::204:9fff:fe07:3a3a/64 scope link
valid_lft forever preferred_lft forever
4: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
link/sit 0.0.0.0 brd 0.0.0.0
5: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:b0:5e:9a:50 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
user@localhost:~$ ip r
default via 192.168.18.1 dev eth0
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown
192.168.10.0/24 dev eth1 proto kernel scope link src 192.168.10.1
192.168.11.0/24 via 192.168.18.69 dev eth0
192.168.18.0/24 dev eth0 proto kernel scope link src 192.168.18.70
配置 Rpi 3B(以及分别针对 Rpi 4B):
user@raspberrypi3:~ $ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether b8:27:eb:53:e4:ef brd ff:ff:ff:ff:ff:ff
inet 192.168.11.100/24 brd 192.168.11.255 scope global eth0
valid_lft forever preferred_lft forever
inet 169.254.215.51/16 brd 169.254.255.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::389a:40b4:ec75:7634/64 scope link
valid_lft forever preferred_lft forever
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether b8:27:eb:06:b1:ba brd ff:ff:ff:ff:ff:ff
inet 192.168.18.48/24 brd 192.168.18.255 scope global dynamic noprefixroute wlan0
valid_lft 212378sec preferred_lft 179978sec
inet6 fe80::160d:2dd3:e73d:d65c/64 scope link
valid_lft forever preferred_lft forever
user@raspberrypi3:~ $ ip r
default via 192.168.11.1 dev eth0 onlink
default via 192.168.18.1 dev wlan0 proto dhcp src 192.168.18.48 metric 303
169.254.0.0/16 dev eth0 scope link src 169.254.215.51 metric 202
192.168.11.0/24 dev eth0 proto kernel scope link src 192.168.11.100
192.168.18.0/24 dev wlan0 proto dhcp scope link src 192.168.18.48 metric 303
user@raspberrypi4:~ $ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether dc:a6:32:7a:7b:bf brd ff:ff:ff:ff:ff:ff
inet 192.168.10.150/24 brd 192.168.10.255 scope global eth0
valid_lft forever preferred_lft forever
inet 169.254.58.248/16 brd 169.254.255.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::bf3:d533:b50a:cf7a/64 scope link
valid_lft forever preferred_lft forever
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether dc:a6:32:7a:7b:c1 brd ff:ff:ff:ff:ff:ff
inet 192.168.18.33/24 brd 192.168.18.255 scope global dynamic noprefixroute wlan0
valid_lft 209358sec preferred_lft 176958sec
inet6 fe80::368:6092:4094:328c/64 scope link
valid_lft forever preferred_lft forever
user@raspberrypi4:~ $ ip r
default via 192.168.10.1 dev eth0
default via 192.168.18.1 dev wlan0 proto dhcp src 192.168.18.33 metric 303
169.254.0.0/16 dev eth0 scope link src 169.254.58.248 metric 202
192.168.10.0/24 dev eth0 proto kernel scope link src 192.168.10.150
192.168.18.0/24 dev wlan0 proto dhcp scope link src 192.168.18.33 metric 303
以下配置启用绿色标记的 ping。图表仅涉及图表的左侧,但相同的行为也适用于右侧。Ping 图
答案1
即使无法修改路由器上的配置,这仍然只是一个路由问题。
我已经模拟了你的网络拓扑迷你网使用此代码:
from mininet.cli import CLI
from mininet.net import Mininet
from mininet.node import OVSBridge
from mininet.topo import Topo
from mininet.log import info, error, setLogLevel
class HomeNetwork(Topo):
def build(self):
s0 = self.addSwitch('s0')
router = self.addHost('router', ip='192.168.18.1/24')
hostA = self.addHost('hostA', ip='192.168.18.69/24')
hostB = self.addHost('hostB', ip='192.168.18.70/24')
rpi3b = self.addHost('rpi3b', ip='192.168.11.100/24')
rpi4b = self.addHost('rpi4b', ip='192.168.10.150/24')
self.addLink(router, s0)
self.addLink(hostA, s0)
self.addLink(hostB, s0)
self.addLink(rpi3b, hostA, params2=dict(ip='192.168.11.1/24'))
self.addLink(rpi4b, hostB, params2=dict(ip='192.168.10.1/24'))
topos = {"home": HomeNetwork}
我们像这样启动模拟环境:
sudo mn --custom homenetwork.py --topo home
由于我们在此示例中实际上并未使用 DHCP,因此我们的主机都没有适当的路由,因此让我们在网络周围填充必要的默认路由:
mininet> hostA ip route add default via 192.168.18.1
mininet> hostB ip route add default via 192.168.18.1
mininet> rpi3b ip route add default via 192.168.11.1
mininet> rpi4b ip route add default via 192.168.10.1
有了这个,我们应该能够hostA
从ping 通rpi3b
:
mininet> rpi3b ping -c1 hostA
PING 192.168.18.69 (192.168.18.69) 56(84) bytes of data.
64 bytes from 192.168.18.69: icmp_seq=1 ttl=64 time=0.032 ms
--- 192.168.18.69 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.032/0.032/0.032/0.000 ms
我们想能够从 到达rpi4b
( 192.168.10.150
) rpi3b
。如果我们现在尝试这样做,它将无法工作:
rpi3b
没有到 的直接路由192.168.10.0/24
,因此它通过其默认路由(即hostA
)发送流量hostA
没有直接路由192.168.10.0/24
,因此它通过其默认路由(即 ISP 路由器)发送流量- ISP 路由器没有直接到的路由
192.168.10.0/24
,因此它实际上会丢弃流量。
这里最简单的解决方案就是将 isp 路由器从等式中去掉。
我们可以通过以下方式添加从hostA
到网络的路由:192.168.10.0/24
hostB
mininet> hostA ip route add 192.168.10.0/24 via 192.168.18.70
hostB
以及从到192.168.11.0/24
网络的路由hostA
:
mininet> hostB ip route add 192.168.11.0/24 via 192.168.18.69
有了这些路线,rpi3b
可以rpi4b
毫无问题地到达:
mininet> rpi3b ping -c2 rpi4b
PING 192.168.10.150 (192.168.10.150) 56(84) bytes of data.
64 bytes from 192.168.10.150: icmp_seq=1 ttl=62 time=0.037 ms
64 bytes from 192.168.10.150: icmp_seq=2 ttl=62 time=0.043 ms
--- 192.168.10.150 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1003ms
rtt min/avg/max/mdev = 0.037/0.040/0.043/0.003 ms
反之亦然:
mininet> rpi4b ping -c2 rpi3b
PING 192.168.11.100 (192.168.11.100) 56(84) bytes of data.
64 bytes from 192.168.11.100: icmp_seq=1 ttl=62 time=0.474 ms
64 bytes from 192.168.11.100: icmp_seq=2 ttl=62 time=0.043 ms
--- 192.168.11.100 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1005ms
rtt min/avg/max/mdev = 0.043/0.258/0.474/0.215 ms
运行上述命令后,路线hostA
如下:
mininet> hostA ip route
default via 192.168.18.1 dev hostA-eth0
192.168.10.0/24 via 192.168.18.70 dev hostA-eth0
192.168.11.0/24 dev hostA-eth1 proto kernel scope link src 192.168.11.1
192.168.18.0/24 dev hostA-eth0 proto kernel scope link src 192.168.18.69
看起来hostB
像:
mininet> hostB ip route
default via 192.168.18.1 dev hostB-eth0
192.168.10.0/24 dev hostB-eth1 proto kernel scope link src 192.168.10.1
192.168.11.0/24 via 192.168.18.69 dev hostB-eth0
192.168.18.0/24 dev hostB-eth0 proto kernel scope link src 192.168.18.70