# 20210322 Bridge ( 一 )

## 課堂資料

{% embed url="<https://www.youtube.com/user/smallko2007/videos>" %}

## 課堂練習

```
cd mininet
```

```
mkdir mytest
```

```
cd mytest
```

### 4.py

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYZST5Es1dJhAGAuAwY%2F-MYZcZ7dt8aXAVhHK3RO%2Fimage.png?alt=media\&token=56f3b044-59b5-4b14-a84c-2ac5b7c77aad)

```
gedit 4.py
```

{% hint style="info" %}
**4.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()
```

{% endhint %}

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYE8Si5HiRvUGFivDi-%2F-MYE9bBGEQcBoi6LQ4hN%2Fimage.png?alt=media\&token=81c50968-01c7-402d-b941-eeda1fbe8897)

```
python 4.py
```

```
xterm r1 r2 
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYE8Si5HiRvUGFivDi-%2F-MYE9v27OxWXH4iDYmoK%2Fimage.png?alt=media\&token=a9502379-4246-43a5-8596-0faf6d76acbc)

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

```
ip ro sh
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYE8Si5HiRvUGFivDi-%2F-MYEADtsjJQhBPId7KuJ%2Fimage.png?alt=media\&token=e860d065-7d7a-4d10-bba6-8fdd8aba1228)
{% endtab %}
{% endtabs %}

```
exit
```

```
python 4.py
```

```
xterm h1 h2 
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYE8Si5HiRvUGFivDi-%2F-MYEB4rEJoPFdKHhowPV%2Fimage.png?alt=media\&token=0cd60b58-7c0a-4269-b1b1-648dd448658e)

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

```
ping 192.168.2.1
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYE8Si5HiRvUGFivDi-%2F-MYEAzq5bMhV_4-BjcDq%2Fimage.png?alt=media\&token=a0fa3b4d-57b1-47b3-a011-174c12fe6702)
{% endtab %}
{% endtabs %}

```
exit
```

### 5.py

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYE8Si5HiRvUGFivDi-%2F-MYEKa2h3rP_xDOmCRvg%2Fimage.png?alt=media\&token=2aa70566-6ffe-4b7c-b02f-3dab8e90a024)

```
gedit 5.py
```

{% hint style="info" %}
**5.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()
```

{% endhint %}

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYE8Si5HiRvUGFivDi-%2F-MYELgymUZA2utkXTmJa%2Fimage.png?alt=media\&token=1b1c48c5-608b-4b32-9439-8b36cfdda04a)

```
python 5.py
```

```
xterm h1 r1 r1
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYE8Si5HiRvUGFivDi-%2F-MYEOpOkzrnckXlC8QAF%2Fimage.png?alt=media\&token=08c36c32-0a77-47f8-8a9a-b8e740cc127b)

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

```
wireshark
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYE8Si5HiRvUGFivDi-%2F-MYEP2Ggfil_WF1qdI_O%2Fimage.png?alt=media\&token=7e95bb86-7e5f-4274-b114-6f7a3f352104)
{% endtab %}

{% tab title="r1 - 2" %}

```
wireshark
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYE8Si5HiRvUGFivDi-%2F-MYEPAB2EvxS3q2AX0py%2Fimage.png?alt=media\&token=5745913c-5848-4a1d-afe0-bac590b939e5)
{% endtab %}

{% tab title="h1" %}

```
 ping 22.1.1.2 -c 5
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYE8Si5HiRvUGFivDi-%2F-MYEOj4BEwUDWf-DBF_z%2Fimage.png?alt=media\&token=39768bf7-6153-4a50-ae34-980463a50160)
{% endtab %}
{% endtabs %}

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

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYEQFy-YD6f3Z68CTU6%2F-MYEQIClAHmbJjGMyK2n%2Fimage.png?alt=media\&token=2944d360-9c8e-4cca-8b9d-b6d622578b7a)

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

```
ping 127.0.0.1 -c 5
```

```
ping 192.168.1.1 -c 5
```

```
ping 192.168.1.254 -c 5
```

```
ping 12.1.1.1 -c 5
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYEQFy-YD6f3Z68CTU6%2F-MYERdoIm5unopFbVKOW%2Fimage.png?alt=media\&token=e28b3077-a99c-470f-a7a7-ecccbf9fe292)
{% endtab %}
{% endtabs %}

```
exit
```

```
cd ..
```

```
cd ..
```

### Bridge

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOaKFMj3BI85rb5EoV%2Fimage.png?alt=media\&token=38a07c20-d649-4b78-91e3-6106f081cb97)

```
mkdir test-mininet
```

```
cd test-mininet
```

```
mkdir bridge
```

```
cd bridge
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYERqJnZOgs4ZCjO8RH%2F-MYEZeO49PgU9g8X2x97%2Fimage.png?alt=media\&token=f3e1e877-74c5-44cc-b62c-2f8baf1da399)

```
apt install bridge-utils
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOWD2EA2RoqgQGyoKy%2Fimage.png?alt=media\&token=8b616884-882b-4266-9b6b-d1f1b0844d5c)

```
gedit 1.py
```

{% hint style="info" %}
**1.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()
```

{% endhint %}

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYERqJnZOgs4ZCjO8RH%2F-MYEhfuSp7QUB3RxizlN%2Fimage.png?alt=media\&token=6d8a2afb-a6ab-45dd-b778-8ce4e2b6d377)

```
python 1.py
```

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

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

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOWUzrafOLa62_0GB5%2Fimage.png?alt=media\&token=7a76f7bc-9040-4425-8ecc-56f635d4ce11)

```
exit
```

```
python 1.py
```

```
xterm br1
```

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

```
ifconfig
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOWyjbhjuXpVCs1W6S%2Fimage.png?alt=media\&token=f7828c3d-0e0a-4c2c-b0c5-f533a48144f5)

```
brctl addbr mybr
```

```
brctl show
```

```
brctl addif mybr br1-eth0
```

```
brctl show
```

```
brctl addif mybr br1-eth1
```

```
brctl addif mybr br1-eth2
```

```
brctl show
```

```
ifconfig -a
```

```
ifconfig mybr up
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOYqs5Q5kuTLpVagdA%2Fimage.png?alt=media\&token=156d6640-c172-4654-985a-a661f7eab171)

```
ifconfig
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOZ-yVc2KnAxvVWHaT%2Fimage.png?alt=media\&token=4566b97f-b71a-47f4-a067-9064bdbcfe70)
{% endtab %}
{% endtabs %}

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

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

```
h2 ping h3 -c 5
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOZI6Xu4Jq0aW2wjzF%2Fimage.png?alt=media\&token=aa9d00e5-9129-432c-a5f1-cb1b41a2eee4)

```
xterm h2
```

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

```
tcpdump -i h2-eth0
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOZye2dS9FoXWWWUrj%2Fimage.png?alt=media\&token=7afe4871-4e2d-4477-97e8-2232fc334851)
{% endtab %}
{% endtabs %}

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

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOZeba3SBUyYdmciyv%2Fimage.png?alt=media\&token=f391d2d7-71c8-492d-9d1e-be0b6d1c55f1)

```
exit
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOcbLDVCFmR_ODrNwI%2Fimage.png?alt=media\&token=b1f93161-e46d-474f-876d-e24b00bcc700)

```
gedit 2.py
```

{% hint style="info" %}
**2.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()
```

{% endhint %}

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOc07DOwUKjE_-WJN2%2Fimage.png?alt=media\&token=e46ff9c8-14a0-4970-b0f7-26233c385d1a)

```
python 2.py
```

```
br1 ifconfig
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOeD1sdxYGlAWBi4Hc%2Fimage.png?alt=media\&token=473306db-6d50-4d00-8b34-32b7771891ac)

```
br1 brctl show
```

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

```
h1 ping h3
```

```
h1 ping h4
```

```
h3 ping h4 -c 5
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOeY8WeIMlvPAt3b-d%2Fimage.png?alt=media\&token=6e502ce5-98fb-407f-97c1-b267fbb02014)

```
exit
```

```
gedit 2.py
```

{% hint style="info" %}
**2.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()
```

{% endhint %}

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOdwvgKZ8FN-WNmrBK%2Fimage.png?alt=media\&token=9fb338a6-bfe5-48c3-beb8-a91de79165c1)

```
python 2.py
```

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

```
h1 ping h3
```

```
h1 ping h4
```

```
h3 ping h4
```

```
h3 ifconfig
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOfvVt44_bgUoGbRyG%2Fimage.png?alt=media\&token=d0f26c85-6585-440b-b59a-39114a347ec8)

```
h4 ifconfig
```

```
h4 ifconfig h4-eth0 0
```

```
h4 ifconfig h4-eth0 192.168.20.2/24
```

```
h3 ping h4 -c 5
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOgAvVeoYip3wZtMZE%2Fimage.png?alt=media\&token=694f1193-2b48-4f2a-986d-3a452c344fbf)

```
exit
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOhYJYpeUGly3HSew1%2Fimage.png?alt=media\&token=a63acb0e-3154-4ade-8332-873dc2a730c7)

```
gedit 3.py
```

{% hint style="info" %}
**3.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()
```

{% endhint %}

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOhp0OuOUFbQyTRtyj%2Fimage.png?alt=media\&token=8c164590-3858-4f58-a217-fb23d7c08523)

```
python 3.py
```

```
net
```

```
br1 brctl show
```

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

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

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOiaClHX-cg6KxGV9w%2Fimage.png?alt=media\&token=c9c9eed2-b30b-40cb-9e57-aa9c6895cd06)

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

```
exit
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOilL6O6ioufsDQU01%2Fimage.png?alt=media\&token=7bf516de-43d6-4810-ae1b-49a898f7eead)

```
apt install vlan
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOj1W2jwtr2zrPRjNr%2Fimage.png?alt=media\&token=7f1edd73-14fa-4ed1-88b9-d1c4c9d5f8b0)

```
gedit 4.py
```

{% hint style="info" %}
**4.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()
```

{% endhint %}

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOoO62bzFqAR8DCyV0%2Fimage.png?alt=media\&token=88203099-a720-4cab-b8d5-0f52a7b6bfe2)

```
python 4.py
```

```
xterm br1 r1
```

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

```
ifconfig
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOprTLHMMBp8VXmZNN%2Fimage.png?alt=media\&token=e01d6488-0db4-4519-bc50-865af7162d4b)
{% endtab %}

{% tab title="r1" %}

```
ifconfig
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOpvkZRE5nYvAEOHF5%2Fimage.png?alt=media\&token=b6e1ac71-7ea0-434f-9d67-bf5525186c64)
{% endtab %}
{% endtabs %}

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

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

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

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOq8AbbwE0l59i5Dbg%2Fimage.png?alt=media\&token=5b985c11-b367-4f1d-af16-225e71e5dada)

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

```
wireshark
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOqN2M6KwWql2HzCmR%2Fimage.png?alt=media\&token=87c0c44f-ac9a-44eb-a286-793b096364fa)
{% endtab %}
{% endtabs %}

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

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOqf8PS7mwVubPqjbE%2Fimage.png?alt=media\&token=3c9e02b5-0bc3-4384-a1a5-fa59d11fd4be)

```
exit
```

```
gedit test.py
```

{% hint style="info" %}
**test.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()
```

{% endhint %}

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOresIgAdulBM0albf%2Fimage.png?alt=media\&token=16c462a8-ad81-4bb9-813a-cc6a99062677)

```
sed -i '/^$/d' test.py
```

* `將原來的所有空行刪除並在每一行後面增加一空行`
* `這樣在輸出的文本中每一行後面將有且只有一空行`

```
gedit test.py
```

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOrz5bvdDEpMeqZYx8%2Fimage.png?alt=media\&token=fe470cb9-b6fa-4254-80d2-99beac3dd960)

![](https://1624492921-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MU7EdDCmxVnXY6VZf4s%2F-MYORD9Mp4TmVruVggcB%2F-MYOsywTY3Co31N5EZY9%2Fimage.png?alt=media\&token=a820ec8e-c975-4def-928e-5b3caf7eb245)
