# Mininet 5 ## Bridge Topology ![](https://i.imgur.com/oNcHVkw.png) ``` apt install bridge-utils ``` ## Configure Bridge ``` root@ubuntu:/home/user/scriptNet/bridge# brctl addbr meowbr root@ubuntu:/home/user/scriptNet/bridge# brctl addif meowbr br1-eth0 root@ubuntu:/home/user/scriptNet/bridge# brctl addif meowbr br1-eth1 root@ubuntu:/home/user/scriptNet/bridge# brctl addif meowbr br1-eth2 root@ubuntu:/home/user/scriptNet/bridge# ifconfig meowbr up ``` meowbri 是Bridge name ## python script ``` #! /usr/bin/env python from mininet.cli import CLI from mininet.net import Mininet from mininet.link import Link,TCLink,Intf if '__main__' == __name__: net = Mininet(link=TCLink) h1 = net.addHost('h1') h2 = net.addHost('h2') h3 = net.addHost('h3') br1 = net.addHost('br1') net.addLink(h1, br1) net.addLink(h2, br1) net.addLink(h3, br1) net.build() h1.cmd("ifconfig h1-eth0 0") h2.cmd("ifconfig h2-eth0 0") h3.cmd("ifconfig h3-eth0 0") br1.cmd("ifconfig br1-eth0 0") br1.cmd("ifconfig br1-eth1 0") br1.cmd("ifconfig br1-eth2 0") br1.cmd("brctl addbr mybr") br1.cmd("brctl addif mybr br1-eth0") br1.cmd("brctl addif mybr br1-eth1") br1.cmd("brctl addif mybr br1-eth2") br1.cmd("ifconfig mybr up") h1.cmd("ip address add 192.168.10.1/24 dev h1-eth0") h2.cmd("ip address add 192.168.10.2/24 dev h2-eth0") h3.cmd("ip address add 192.168.10.3/24 dev h3-eth0") CLI(net) net.stop() ``` ![](https://i.imgur.com/wfOOO9K.png) --- ## Test h1 ping --> h2 h1 ping --> h3 ``` PING 192.168.10.2 (192.168.10.2) 56(84) bytes of data. 64 bytes from 192.168.10.2: icmp_seq=1 ttl=64 time=1.60 ms 64 bytes from 192.168.10.2: icmp_seq=2 ttl=64 time=0.083 ms 64 bytes from 192.168.10.2: icmp_seq=3 ttl=64 time=0.122 ms --- 192.168.10.2 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2001ms rtt min/avg/max/mdev = 0.083/0.602/1.603/0.708 ms root@ubuntu:/home/user/scriptNet/bridge# ping 192.168.10.3 PING 192.168.10.3 (192.168.10.3) 56(84) bytes of data. 64 bytes from 192.168.10.3: icmp_seq=1 ttl=64 time=0.516 ms 64 bytes from 192.168.10.3: icmp_seq=2 ttl=64 time=0.076 ms 64 bytes from 192.168.10.3: icmp_seq=3 ttl=64 time=0.096 ms ``` ## Topology 2 ![](https://i.imgur.com/1I4oSNJ.png) ``` #! /usr/bin/env python from mininet.cli import CLI from mininet.net import Mininet from mininet.link import Link,TCLink,Intf if '__main__' == __name__: net = Mininet(link=TCLink) h1 = net.addHost('h1') h2 = net.addHost('h2') h3 = net.addHost('h3') h4 = net.addHost('h4') br1 = net.addHost('br1') net.addLink(h1, br1) net.addLink(h2, br1) net.addLink(h3, br1) net.addLink(h4, br1) net.build() h1.cmd("ifconfig h1-eth0 0") h2.cmd("ifconfig h2-eth0 0") h3.cmd("ifconfig h3-eth0 0") h3.cmd("ifconfig h4-eth0 0") br1.cmd("ifconfig br1-eth0 0") br1.cmd("ifconfig br1-eth1 0") br1.cmd("ifconfig br1-eth2 0") br1.cmd("ifconfig br1-eth3 0") br1.cmd("brctl addbr mybr1") br1.cmd("brctl addbr mybr2") br1.cmd("brctl addif mybr1 br1-eth0") br1.cmd("brctl addif mybr1 br1-eth1") br1.cmd("brctl addif mybr2 br1-eth2") br1.cmd("brctl addif mybr2 br1-eth3") br1.cmd("ifconfig mybr1 up") br1.cmd("ifconfig mybr2 up") h1.cmd("ip address add 192.168.10.1/24 dev h1-eth0") h2.cmd("ip address add 192.168.10.2/24 dev h2-eth0") h3.cmd("ip address add 192.168.20.1/24 dev h3-eth0") h4.cmd("ip address add 192.168.20.2/24 dev h4-eth0") CLI(net) net.stop() ``` Test h1 ping h2 ![](https://i.imgur.com/bra50BI.png) h1 ping h3 ![](https://i.imgur.com/3KDSvHS.png) ## Topology 3 ![](https://i.imgur.com/h5EABRR.png) ```python #! /usr/bin/env python from mininet.cli import CLI from mininet.net import Mininet from mininet.link import Link,TCLink,Intf if '__main__' == __name__: net = Mininet(link=TCLink) h1 = net.addHost('h1') h2 = net.addHost('h2') h3 = net.addHost('h3') h4 = net.addHost('h4') br1 = net.addHost('br1') r1 = net.addHost('r1') net.addLink(h1, br1) net.addLink(h2, br1) net.addLink(h3, br1) net.addLink(h4, br1) net.addLink(br1,r1) net.addLink(br1,r1) net.build() h1.cmd("ifconfig h1-eth0 0") h2.cmd("ifconfig h2-eth0 0") h3.cmd("ifconfig h3-eth0 0") h4.cmd("ifconfig h4-eth0 0") br1.cmd("ifconfig br1-eth0 0") br1.cmd("ifconfig br1-eth1 0") br1.cmd("ifconfig br1-eth2 0") br1.cmd("ifconfig br1-eth3 0") br1.cmd("ifconfig br1-eth4 0") br1.cmd("ifconfig br1-eth5 0") br1.cmd("brctl addbr mybr1") br1.cmd("brctl addbr mybr2") br1.cmd("brctl addif mybr1 br1-eth0") br1.cmd("brctl addif mybr1 br1-eth1") br1.cmd("brctl addif mybr1 br1-eth4") br1.cmd("brctl addif mybr2 br1-eth2") br1.cmd("brctl addif mybr2 br1-eth3") br1.cmd("brctl addif mybr2 br1-eth5") br1.cmd("ifconfig mybr1 up") br1.cmd("ifconfig mybr2 up") r1.cmd('ifconfig r1-eth0 192.168.10.254 netmask 255.255.255.0') r1.cmd('ifconfig r1-eth1 192.168.20.254 netmask 255.255.255.0') r1.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward") h1.cmd("ip address add 192.168.10.1/24 dev h1-eth0") h1.cmd("ip route add default via 192.168.10.254") h2.cmd("ip address add 192.168.10.2/24 dev h2-eth0") h2.cmd("ip route add default via 192.168.10.254") h3.cmd("ip address add 192.168.20.1/24 dev h3-eth0") h3.cmd("ip route add default via 192.168.20.254") h4.cmd("ip address add 192.168.20.2/24 dev h4-eth0") h4.cmd("ip route add default via 192.168.20.254") CLI(net) net.stop() ``` 再有 router 情況下 h1 是能夠 ping h2 的