> 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 %}

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYOtMX09fzyFSuy9ff3%2F-MYP6gph2J8g7Gaj4h11%2Fimage.png?alt=media\&token=f4d5226b-84fa-4a1f-a772-f5b10e263e1e)

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYOtMX09fzyFSuy9ff3%2F-MYP6rqWN3YDVeCypMnq%2Fimage.png?alt=media\&token=1d01be9c-6930-4129-b850-ad0045e868e8)

```
python 5.py
```

```
xterm s1 s2
```

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

```
ifconfig
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYP71eTuHWPi4WGIF1b%2F-MYP7OZFfPpjA3bpydkk%2Fimage.png?alt=media\&token=19f1751d-060d-4dc2-b622-3e9ef12462d9)

```
brctl show
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYP71eTuHWPi4WGIF1b%2F-MYP7kYtUOTlvQ9ha6lF%2Fimage.png?alt=media\&token=dcba526b-7952-43a0-8653-9bdbf32e88b1)
{% endtab %}

{% tab title="s2" %}

```
ifconfig
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYP71eTuHWPi4WGIF1b%2F-MYP7UTL6Hfn_WLCUVOo%2Fimage.png?alt=media\&token=2b0ec349-9f0e-4127-a7f4-07c36a85467e)

```
brctl show
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYP71eTuHWPi4WGIF1b%2F-MYP7ohK09Wca3mpWwG6%2Fimage.png?alt=media\&token=c8893b72-984b-4b14-9092-30ea07bf4c48)
{% endtab %}
{% endtabs %}

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

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

```
h1 ping h2
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYP71eTuHWPi4WGIF1b%2F-MYP8vOwpIPD5Vh0cXLa%2Fimage.png?alt=media\&token=63299bd9-21a3-42d1-8315-30c658a3cf66)

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

```
wireshark
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYP71eTuHWPi4WGIF1b%2F-MYP82c8cqZh-glLkg-o%2Fimage.png?alt=media\&token=6eb6bb01-a622-4023-af5c-32e0726843ea)

> #### s1-eth2

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYP71eTuHWPi4WGIF1b%2F-MYP8QPwQgHJ_Cgznbvr%2Fimage.png?alt=media\&token=5642178f-e95f-451d-a05a-9c35e727264a)

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYP71eTuHWPi4WGIF1b%2F-MYP8gpdrEe0DQ5wZ8Ey%2Fimage.png?alt=media\&token=1c834770-cdbb-4c12-b499-cceb284178cc)
{% endtab %}
{% endtabs %}

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

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

```
exit
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYP71eTuHWPi4WGIF1b%2F-MYP99ckcXD9RiSg2PCm%2Fimage.png?alt=media\&token=8fff58e6-caf7-4a29-9916-700e99ef6489)

```
python 6.py
```

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

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

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

```
pingall
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYP71eTuHWPi4WGIF1b%2F-MYP9svKgBUu-cWqPC38%2Fimage.png?alt=media\&token=9cc16813-536d-4dd3-b9a5-0c41f86b1756)

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

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYP71eTuHWPi4WGIF1b%2F-MYPA1BW4rh2jycuHyMP%2Fimage.png?alt=media\&token=cd3e23ba-b572-44af-acf9-0614f6300bea)

```
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 %}

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYPAcL-JAXdfIN_KxR7%2F-MYPBWaPCkxGHH9LtHvd%2Fimage.png?alt=media\&token=5a021d55-a9db-4152-acef-92473e12aaff)

```
python hub.py
```

```
xterm h2
```

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

```
wireshark
```

> #### h2-eth0

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYPAcL-JAXdfIN_KxR7%2F-MYPCObAA69AAa804X6A%2Fimage.png?alt=media\&token=7219cbd1-99ae-4a6b-a35d-aba50687fb10)
{% endtab %}
{% endtabs %}

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

```
exit
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYPAcL-JAXdfIN_KxR7%2F-MYPCWSklK5BzxmfZRra%2Fimage.png?alt=media\&token=b7b0eab8-f7b1-41e2-811a-78cafb51d588)

### Openvswitch-Switch

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYTzFVdZ0B1hQw76mKP%2F-MYVJKyakdKTRYVkXhsi%2Fimage.png?alt=media\&token=ab483457-a530-4bec-b8a6-aae210c05b48)

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

```
mkdir mytest
```

```
cd mytest
```

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

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

```
net
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYTzFVdZ0B1hQw76mKP%2F-MYVFNTQ69yXs7AjGzRU%2Fimage.png?alt=media\&token=d40c58e3-ae08-4b45-bf69-560b3c203f78)
{% 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
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYTzFVdZ0B1hQw76mKP%2F-MYVFy-uFMgLuC18pbiZ%2Fimage.png?alt=media\&token=835ab63d-b942-4440-97f1-232ac5980693)
{% endtab %}
{% endtabs %}

```
exit
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYTzFVdZ0B1hQw76mKP%2F-MYVJaknZoex6exH3Iuc%2Fimage.png?alt=media\&token=c40ab755-0889-4ccc-b395-1ab7f8e6d163)

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

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

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

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

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYTzFVdZ0B1hQw76mKP%2F-MYVGGGrbangz4Z2HT3c%2Fimage.png?alt=media\&token=a4ea23e0-4225-4fbd-a706-ea5b1a739a80)
{% 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
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYTzFVdZ0B1hQw76mKP%2F-MYVGvoYJw8Bj4XxA68u%2Fimage.png?alt=media\&token=c33a262a-dd62-4d8f-b77c-654d55a8bedb)
{% endtab %}
{% endtabs %}
