20210322 Bridge ( 一 )
課堂資料
課堂練習
cd mininetmkdir mytestcd mytest4.py

gedit 4.py4.py
#!/usr/bin/env python
from mininet.cli import CLI
from mininet.net import Mininet
from mininet.link import Link,TCLink
if '__main__' == __name__:
net = Mininet(link=TCLink)
h1 = net.addHost('h1')
h2 = net.addHost('h2')
r1 = net.addHost('r1')
r2 = net.addHost('r2')
Link(h1, r1)
Link(h2, r2)
Link(r1, r2)
net.build()
h1.cmd("ifconfig h1-eth0 0")
h1.cmd("ip addr add 192.168.1.1/24 brd + dev h1-eth0")
h1.cmd("ip route add default via 192.168.1.254")
h2.cmd("ifconfig h2-eth0 0")
h2.cmd("ip addr add 192.168.2.1/24 brd + dev h2-eth0")
h2.cmd("ip route add default via 192.168.2.254")
r1.cmd("ifconfig r1-eth0 0")
r1.cmd("ifconfig r1-eth1 0")
r1.cmd("ip addr add 192.168.1.254/24 brd + dev r1-eth0")
r1.cmd("ip addr add 10.0.0.1/24 brd + dev r1-eth1")
r1.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
r1.cmd("ip route add 192.168.2.0/24 via 10.0.0.2")
r2.cmd("ifconfig r2-eth0 0")
r2.cmd("ifconfig r2-eth1 0")
r2.cmd("ip addr add 192.168.2.254/24 brd + dev r2-eth0")
r2.cmd("ip addr add 10.0.0.2/24 brd + dev r2-eth1")
r2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
r2.cmd("ip route add 192.168.1.0/24 via 10.0.0.1")
CLI(net)
net.stop()
python 4.pyxterm r1 r2 
ip ro sh
exitpython 4.pyxterm h1 h2 
ping 192.168.2.1
exit5.py

gedit 5.py5.py
#!/usr/bin/env python
from mininet.cli import CLI
from mininet.net import Mininet
from mininet.link import Link,TCLink
if '__main__' == __name__:
net = Mininet(link=TCLink)
h1 = net.addHost('h1')
h2 = net.addHost('h2')
r1 = net.addHost('r1')
r2 = net.addHost('r2')
Link(h1, r1)
Link(h2, r2)
Link(r1, r2)
net.build()
h1.cmd("ifconfig h1-eth0 0")
h1.cmd("ip addr add 192.168.1.1/24 brd + dev h1-eth0")
h1.cmd("ip route add default via 192.168.1.254")
h2.cmd("ifconfig h2-eth0 0")
h2.cmd("ip addr add 22.1.1.1/24 brd + dev h2-eth0")
h2.cmd("ip route add default via 22.1.1.254")
r1.cmd("ifconfig r1-eth0 0")
r1.cmd("ifconfig r1-eth1 0")
r1.cmd("ip addr add 192.168.1.254/24 brd + dev r1-eth0")
r1.cmd("ip addr add 12.1.1.1/24 brd + dev r1-eth1")
r1.cmd("ip route add default via 12.1.1.2")
r1.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
r1.cmd("iptables -t nat -A POSTROUTING -o r1-eth1 -s 192.168.1.0/24 -j MASQUERADE")
r2.cmd("ifconfig r2-eth0 0")
r2.cmd("ifconfig r2-eth1 0")
r2.cmd("ip addr add 22.1.1.254/24 brd + dev r2-eth0")
r2.cmd("ip addr add 12.1.1.2/24 brd + dev r2-eth1")
r2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
CLI(net)
net.stop()
python 5.pyxterm h1 r1 r1
wireshark
wireshark
ping 22.1.1.2 -c 5
h1 ping h2 -c 5
ping 127.0.0.1 -c 5ping 192.168.1.1 -c 5ping 192.168.1.254 -c 5ping 12.1.1.1 -c 5
exitcd ..cd ..Bridge

mkdir test-mininetcd test-mininetmkdir bridgecd bridge
apt install bridge-utils
gedit 1.py1.py
#! /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()
python 1.pyh1 ping h2 -c 5h1 ping h3 -c 5
exitpython 1.pyxterm br1ifconfig
brctl addbr mybrbrctl showbrctl addif mybr br1-eth0brctl showbrctl addif mybr br1-eth1brctl addif mybr br1-eth2brctl showifconfig -aifconfig mybr up
ifconfig
h1 ping h2 -c 5h1 ping h3 -c 5h2 ping h3 -c 5
xterm h2tcpdump -i h2-eth0
h1 ping h3 -c 5
exit
gedit 2.py2.py
#! /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("ifconfig h4-eth0 192.168.20.2/4")
CLI(net)
net.stop()
python 2.pybr1 ifconfig
br1 brctl showh1 ping h2 -c 5h1 ping h3h1 ping h4h3 ping h4 -c 5
exitgedit 2.py2.py
#! /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()
python 2.pyh1 ping h2 -c 5h1 ping h3h1 ping h4h3 ping h4h3 ifconfig
h4 ifconfigh4 ifconfig h4-eth0 0h4 ifconfig h4-eth0 192.168.20.2/24h3 ping h4 -c 5
exit
gedit 3.py3.py
#! /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()
python 3.pynetbr1 brctl showh1 ping h2 -c 5h1 ping h3 -c 5
h1 ping h4 -c 5exit
apt install vlan
gedit 4.py4.py
#! /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.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")
r1.cmd("ifconfig r1-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("vconfig add br1-eth4 10")
br1.cmd("vconfig add br1-eth4 20")
r1.cmd("vconfig add r1-eth0 10")
r1.cmd("vconfig add r1-eth0 20")
br1.cmd("brctl addbr mybr10")
br1.cmd("brctl addbr mybr20")
br1.cmd("brctl addif mybr10 br1-eth0")
br1.cmd("brctl addif mybr10 br1-eth1")
br1.cmd("brctl addif mybr10 br1-eth4.10")
br1.cmd("brctl addif mybr20 br1-eth2")
br1.cmd("brctl addif mybr20 br1-eth3")
br1.cmd("brctl addif mybr20 br1-eth4.20")
br1.cmd("ifconfig br1-eth4.10 up")
br1.cmd("ifconfig br1-eth4.20 up")
r1.cmd("ifconfig r1-eth0.10 up")
r1.cmd("ifconfig r1-eth0.20 up")
br1.cmd("ifconfig mybr10 up")
br1.cmd("ifconfig mybr20 up")
r1.cmd('ifconfig r1-eth0.10 192.168.10.254 netmask 255.255.255.0')
r1.cmd('ifconfig r1-eth0.20 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()
python 4.pyxterm br1 r1ifconfig
ifconfig
h1 ping h2 -c 5h1 ping h3 -c 5h1 ping h4 -c 5
wireshark
h1 ping h3 -c 5
exitgedit test.pytest.py
#! /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.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")
r1.cmd("ifconfig r1-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("vconfig add br1-eth4 10")
br1.cmd("vconfig add br1-eth4 20")
r1.cmd("vconfig add r1-eth0 10")
r1.cmd("vconfig add r1-eth0 20")
br1.cmd("brctl addbr mybr10")
br1.cmd("brctl addbr mybr20")
br1.cmd("brctl addif mybr10 br1-eth0")
br1.cmd("brctl addif mybr10 br1-eth1")
br1.cmd("brctl addif mybr10 br1-eth4.10")
br1.cmd("brctl addif mybr20 br1-eth2")
br1.cmd("brctl addif mybr20 br1-eth3")
br1.cmd("brctl addif mybr20 br1-eth4.20")
br1.cmd("ifconfig br1-eth4.10 up")
br1.cmd("ifconfig br1-eth4.20 up")
r1.cmd("ifconfig r1-eth0.10 up")
r1.cmd("ifconfig r1-eth0.20 up")
br1.cmd("ifconfig mybr10 up")
br1.cmd("ifconfig mybr20 up")
r1.cmd('ifconfig r1-eth0.10 192.168.10.254 netmask 255.255.255.0')
r1.cmd('ifconfig r1-eth0.20 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()
sed -i '/^$/d' test.py將原來的所有空行刪除並在每一行後面增加一空行這樣在輸出的文本中每一行後面將有且只有一空行
gedit test.py

Last updated
Was this helpful?