> For the complete documentation index, see [llms.txt](https://anida-huang.gitbook.io/notes-network-simulation-and-analysis/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://anida-huang.gitbook.io/notes-network-simulation-and-analysis/qi-zhong/20210322-tou-guo-shell-script-fen-xi-shi-yan-jie-guo.md).

# 20210329 Bridge ( 二 )

## 課堂資料

{% embed url="<https://www.youtube.com/watch?v=QKXuQtd37jU>" %}

## 課堂練習

### Bridge

```
cd test-mininet/bridge
```

```
gedit 5.py 6.py
```

{% hint style="info" %}
**5.py**

```
#!/usr/bin/env python
from mininet.net import Mininet
from mininet.cli import CLI
from mininet.link import Link,TCLink,Intf

if '__main__' == __name__:
  net = Mininet(link=TCLink)
  #h1 is under vlan10
  h1 = net.addHost('h1')
  #h2 is under vlan20
  h2 = net.addHost('h2')
  #h3 is under vlan10
  h3 = net.addHost('h3')
  #h4 is under vlan20
  h4 = net.addHost('h4')
  #s1 is a switch
  s1 = net.addHost('s1')
  #s2 is a switch
  s2 = net.addHost('s2')
  
  Link(h1, s1)
  Link(h2, s1)
  Link(h3, s2)
  Link(h4, s2)
  Link(s1, s2)
  net.build()
  
  s1.cmd("ifconfig s1-eth0 0")
  s1.cmd("ifconfig s1-eth1 0")
  s1.cmd("ifconfig s1-eth2 0")
  s2.cmd("ifconfig s2-eth0 0")
  s2.cmd("ifconfig s2-eth1 0")
  s2.cmd("ifconfig s2-eth2 0")
  s1.cmd("vconfig add s1-eth2 10")
  s1.cmd("vconfig add s1-eth2 20")
  s2.cmd("vconfig add s2-eth2 10")
  s2.cmd("vconfig add s2-eth2 20")
  s1.cmd("ifconfig s1-eth2.10 up")
  s1.cmd("ifconfig s1-eth2.20 up")
  s2.cmd("ifconfig s2-eth2.10 up")
  s2.cmd("ifconfig s2-eth2.20 up")
  s1.cmd("brctl addbr brvlan10")
  s1.cmd("brctl addbr brvlan20")
  s1.cmd("brctl addif brvlan10 s1-eth0")
  s1.cmd("brctl addif brvlan20 s1-eth1")
  s1.cmd("brctl addif brvlan10 s1-eth2.10")
  s1.cmd("brctl addif brvlan20 s1-eth2.20")
  s2.cmd("brctl addbr brvlan10")
  s2.cmd("brctl addbr brvlan20")
  s2.cmd("brctl addif brvlan10 s2-eth0")
  s2.cmd("brctl addif brvlan20 s2-eth1")
  s2.cmd("brctl addif brvlan10 s2-eth2.10")
  s2.cmd("brctl addif brvlan20 s2-eth2.20")
  s1.cmd("ifconfig brvlan10 up")
  s1.cmd("ifconfig brvlan20 up")
  s2.cmd("ifconfig brvlan10 up")
  s2.cmd("ifconfig brvlan20 up")
  h1.cmd("ifconfig h1-eth0 10.0.10.1 netmask 255.255.255.0")
  h2.cmd("ifconfig h2-eth0 10.0.10.2 netmask 255.255.255.0")
  h3.cmd("ifconfig h3-eth0 10.0.10.3 netmask 255.255.255.0")
  h4.cmd("ifconfig h4-eth0 10.0.10.4 netmask 255.255.255.0")
  CLI(net)
  net.stop()
```

**6.py**

```
#!/usr/bin/env python
from mininet.net import Mininet
from mininet.cli import CLI
from mininet.link import Link,TCLink,Intf

if '__main__' == __name__:
  net = Mininet(link=TCLink)
  #h1 is under vlan10
  h1 = net.addHost('h1')
  #h2 is under vlan20
  h2 = net.addHost('h2')
  #h3 is under vlan10
  h3 = net.addHost('h3')
  #h4 is under vlan20
  h4 = net.addHost('h4')
  #s1 is a switch
  s1 = net.addHost('s1')
  #s2 is a switch
  s2 = net.addHost('s2')
  #s3 is a switch
  s3 = net.addHost('s3')
  #r1 is a router
  r1 = net.addHost('r1')
  
  Link(h1, s1)
  Link(h2, s1)
  Link(h3, s2)
  Link(h4, s2)
  Link(s1, s3)
  Link(s2, s3)
  Link(s3, r1)
  net.build()
  
  s1.cmd("ifconfig s1-eth0 0")
  s1.cmd("ifconfig s1-eth1 0")
  s1.cmd("ifconfig s1-eth2 0")
  s2.cmd("ifconfig s2-eth0 0")
  s2.cmd("ifconfig s2-eth1 0")
  s2.cmd("ifconfig s2-eth2 0")
  s3.cmd("ifconfig s3-eth0 0")
  s3.cmd("ifconfig s3-eth1 0")
  s3.cmd("ifconfig s3-eth2 0")
  r1.cmd("ifconfig r1-eth1 0")
  s1.cmd("vconfig add s1-eth2 10")
  s1.cmd("vconfig add s1-eth2 20")
  s2.cmd("vconfig add s2-eth2 10")
  s2.cmd("vconfig add s2-eth2 20")
  s3.cmd("vconfig add s3-eth0 10")
  s3.cmd("vconfig add s3-eth0 20")
  s3.cmd("vconfig add s3-eth1 10")
  s3.cmd("vconfig add s3-eth1 20")
  s3.cmd("vconfig add s3-eth2 10")
  s3.cmd("vconfig add s3-eth2 20")
  r1.cmd("vconfig add r1-eth0 10")
  r1.cmd("vconfig add r1-eth0 20")
  s1.cmd("ifconfig s1-eth2.10 up")
  s1.cmd("ifconfig s1-eth2.20 up")
  s2.cmd("ifconfig s2-eth2.10 up")
  s2.cmd("ifconfig s2-eth2.20 up")
  s3.cmd("ifconfig s3-eth0.10 up")
  s3.cmd("ifconfig s3-eth0.20 up")
  s3.cmd("ifconfig s3-eth1.10 up")
  s3.cmd("ifconfig s3-eth1.20 up")
  s3.cmd("ifconfig s3-eth2.10 up")
  s3.cmd("ifconfig s3-eth2.20 up")
  r1.cmd("ifconfig r1-eth0.10 up")
  r1.cmd("ifconfig r1-eth0.20 up")
  s1.cmd("brctl addbr brvlan10")
  s1.cmd("brctl addbr brvlan20")
  s1.cmd("brctl addif brvlan10 s1-eth0")
  s1.cmd("brctl addif brvlan20 s1-eth1")
  s1.cmd("brctl addif brvlan10 s1-eth2.10")
  s1.cmd("brctl addif brvlan20 s1-eth2.20")
  s2.cmd("brctl addbr brvlan10")
  s2.cmd("brctl addbr brvlan20")
  s2.cmd("brctl addif brvlan10 s2-eth0")
  s2.cmd("brctl addif brvlan20 s2-eth1")
  s2.cmd("brctl addif brvlan10 s2-eth2.10")
  s2.cmd("brctl addif brvlan20 s2-eth2.20")
  s3.cmd("brctl addbr brvlan10")
  s3.cmd("brctl addbr brvlan20")
  s3.cmd("brctl addif brvlan10 s3-eth0.10")
  s3.cmd("brctl addif brvlan10 s3-eth1.10")
  s3.cmd("brctl addif brvlan10 s3-eth2.10")
  s3.cmd("brctl addif brvlan20 s3-eth0.20")
  s3.cmd("brctl addif brvlan20 s3-eth1.20")
  s3.cmd("brctl addif brvlan20 s3-eth2.20")  
  s1.cmd("ifconfig brvlan10 up")
  s1.cmd("ifconfig brvlan20 up")
  s2.cmd("ifconfig brvlan10 up")
  s2.cmd("ifconfig brvlan20 up")
  s3.cmd("ifconfig brvlan10 up")
  s3.cmd("ifconfig brvlan20 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("ifconfig h1-eth0 192.168.10.1 netmask 255.255.255.0")
  h1.cmd("ip route add default via 192.168.10.254")
  h2.cmd("ifconfig h2-eth0 192.168.20.1 netmask 255.255.255.0")
  h2.cmd("ip route add default via 192.168.20.254")
  h3.cmd("ifconfig h3-eth0 192.168.10.2 netmask 255.255.255.0")
  h3.cmd("ip route add default via 192.168.10.254")
  h4.cmd("ifconfig h4-eth0 192.168.20.2 netmask 255.255.255.0")
  h4.cmd("ip route add default via 192.168.20.254")
  CLI(net)
  net.stop()
```

{% endhint %}

![](/files/-MYP6gph2J8g7Gaj4h11)

![](/files/-MYP6rqWN3YDVeCypMnq)

```
python 5.py
```

```
xterm s1 s2
```

{% tabs %}
{% tab title="s1" %}

```
ifconfig
```

![](/files/-MYP7OZFfPpjA3bpydkk)

```
brctl show
```

![](/files/-MYP7kYtUOTlvQ9ha6lF)
{% endtab %}

{% tab title="s2" %}

```
ifconfig
```

![](/files/-MYP7UTL6Hfn_WLCUVOo)

```
brctl show
```

![](/files/-MYP7ohK09Wca3mpWwG6)
{% endtab %}
{% endtabs %}

```
h1 ping h3 -c 5
```

```
h2 ping h4 -c 5
```

```
h1 ping h2
```

![](/files/-MYP8vOwpIPD5Vh0cXLa)

{% tabs %}
{% tab title="s1" %}

```
wireshark
```

![](/files/-MYP82c8cqZh-glLkg-o)

> #### s1-eth2

![](/files/-MYP8QPwQgHJ_Cgznbvr)

![](/files/-MYP8gpdrEe0DQ5wZ8Ey)
{% endtab %}
{% endtabs %}

```
h1 ping h3 -c 5
```

```
h2 ping h4 -c 5
```

```
exit
```

![](/files/-MYP99ckcXD9RiSg2PCm)

```
python 6.py
```

```
h1 ping h2 -c 5
```

```
h1 ping h3 -c 5
```

```
h1 ping h4 -c 5
```

```
pingall
```

![](/files/-MYP9svKgBUu-cWqPC38)

```
h1 ping h4 -c 5
```

![](/files/-MYPA1BW4rh2jycuHyMP)

```
exit
```

```
gedit hub.py
```

{% hint style="info" %}
**hub.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', mac='00:00:00:00:01:00')
  h2 = net.addHost('h2', mac='00:00:00:00:02:00')
  h3 = net.addHost('h3', mac='00:00:00:00:03:00')
  h4 = net.addHost('h4', mac='00:00:00:00:04:00')
  Link(h1, h4)
  Link(h2, h4)
  Link(h3, h4)
  net.build()
  h4.cmd("ifconfig h4-eth0 0")
  h4.cmd("ifconfig h4-eth1 0")
  h4.cmd("ifconfig h4-eth2 0")
  h4.cmd("brctl addbr br0")
  h4.cmd("brctl addif br0 h4-eth0")
  h4.cmd("brctl addif br0 h4-eth1")
  h4.cmd("brctl addif br0 h4-eth2")
  h4.cmd("brctl setageing br0 0")
  h4.cmd("ifconfig br0 up")
  CLI(net)
  net.stop()
```

{% endhint %}

![](/files/-MYPBWaPCkxGHH9LtHvd)

```
python hub.py
```

```
xterm h2
```

{% tabs %}
{% tab title="h2" %}

```
wireshark
```

> #### h2-eth0

![](/files/-MYPCObAA69AAa804X6A)
{% endtab %}
{% endtabs %}

```
h1 ping h3 -c 5
```

```
exit
```

![](/files/-MYPCWSklK5BzxmfZRra)

### Openvswitch-Switch

![](/files/-MYVJKyakdKTRYVkXhsi)

{% tabs %}
{% tab title="terminal 01" %}

```
mkdir mytest
```

```
cd mytest
```

```
mn --topo single,2
```

```
h1 ping h2 -c 5
```

```
net
```

![](/files/-MYVFNTQ69yXs7AjGzRU)
{% endtab %}

{% tab title="terminal 02" %}

```
ps -aux | grep controller
```

```
kill -9 22181
```

```
ovs-ofctl show s1
```

```
ovs-ofctl dump-flows s1
```

```
ovs-ofctl add-flow s1 in_port=1,actions=output:2
```

```
ovs-ofctl add-flow s1 in_port=2,actions=output:1
```

```
ovs-ofctl dump-flows s1
```

```
ovs-ofctl del-flows s1
```

```
ovs-ofctl dump-flows s1
```

```
ovs-ofctl add-flow s1 in_port=1,actions=output:2
```

```
ovs-ofctl add-flow s1 in_port=2,actions=output:1
```

```
ovs-ofctl del-flows s1 in_port=1
```

![](/files/-MYVFy-uFMgLuC18pbiZ)
{% endtab %}
{% endtabs %}

```
exit
```

![](/files/-MYVJaknZoex6exH3Iuc)

{% tabs %}
{% tab title="terminal 01" %}

```
mn --topo single,3
```

```
h1 ping h2 -c 5
```

```
h1 ping h2 -c 5
```

![](/files/-MYVGGGrbangz4Z2HT3c)
{% endtab %}

{% tab title="terminal 02" %}

```
ps -aux | grep controller
```

```
kill -9 23876
```

```
ps -aux | grep controller
```

```
ovs-ofctl dump-flows s1
```

```
ovs-ofctl add-flow s1 in_port=1,arp,actions=output:flood
```

```
ovs-ofctl add-flow s1 in_port=2,arp,actions=output:flood
```

```
ovs-ofctl add-flow s1 in_port=3,arp,actions=output:flood
```

```
ovs-ofctl add-flow s1 ip,nw_dst=10.0.0.1,actions=output:1
```

```
ovs-ofctl add-flow s1 ip,nw_dst=10.0.0.2,actions=output:2
```

```
ovs-ofctl add-flow s1 ip,nw_dst=10.0.0.3,actions=output:3
```

```
ovs-ofctl dump-flows s1
```

![](/files/-MYVGvoYJw8Bj4XxA68u)
{% endtab %}
{% endtabs %}
