# 20201209 cisco VRF & OSPF

## 課堂資料

{% embed url="https://en.wikipedia.org/wiki/Virtual_routing_and_forwarding" %}

{% embed url="https://kknews.cc/zh-tw/code/4j9pp6q.html" %}

{% embed url="https://www.jannet.hk/zh-Hant/post/network-address-translation-nat/" %}

### Virtual Routing and Forwarding

Virtual Routing and Forwarding(VRF)：虛擬路由和轉發，為第三層的網路虛擬化。

## 課堂練習

### VRF

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MO4TBFUAMhrOQN-C7q2%2F-MO4UEui8D5jDKpif8LV%2Fimage.png?alt=media\&token=f02b8803-80a8-464c-8743-d5336bc4a182)

{% tabs %}
{% tab title="R1 ( Blue 01 )" %}
```
int e0/0
```

```
ip add 192.168.1.1 255.255.255.0
```

```
no shut
```

```
exit
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MO4TBFUAMhrOQN-C7q2%2F-MO4VaEwWrXEtxy5sKpu%2Fimage.png?alt=media\&token=13203a26-b62a-47b6-8b64-1b72a1e74102)
{% endtab %}

{% tab title="R2 ( Red 01 )" %}
```
int e0/0
```

```
ip add 192.168.2.1 255.255.255.0
```

```
no shut
```

```
exit
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MO4TBFUAMhrOQN-C7q2%2F-MO4VxhhK0ySApOQX2_M%2Fimage.png?alt=media\&token=395ff6ba-18dd-44e6-b184-bf192d9a9855)
{% endtab %}

{% tab title="R3 ( Blue 02 )" %}
```
int e0/0
```

```
ip add 192.168.3.1 255.255.255.0
```

```
no shut
```

```
exit
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MO4TBFUAMhrOQN-C7q2%2F-MO4WQMlEyvE9ZvFrG7d%2Fimage.png?alt=media\&token=9e30d60c-0c2a-4d6d-83aa-4f96b49f4215)
{% endtab %}

{% tab title="R4 ( Red 02 )" %}
```
int e0/0
```

```
ip add 192.168.4.1 255.255.255.0
```

```
no shut
```

```
exit
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MO4TBFUAMhrOQN-C7q2%2F-MO4WqMA4u8yYevmTljE%2Fimage.png?alt=media\&token=915fe299-642a-4b1e-8e22-2988a5c8301e)
{% endtab %}

{% tab title="R5 ( ISP )" %}
> 創建這些 VRF

```
ip vrf Blue
```

```
exit
```

```
ip vrf Red
```

```
exit
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MO4TBFUAMhrOQN-C7q2%2F-MO4XCZjOU1nwP4I4X0L%2Fimage.png?alt=media\&token=fe36f12a-8821-4031-bfd0-b418649ec8b2)

> &#x20;使用`ip vrf forwarding`命令將接口分配給正確的 VRF

```
int e0/0
```

```
ip vrf forwarding Blue
```

```
ip add 192.168.1.2 255.255.255.0
```

```
no shut
```

```
int e0/1
```

```
ip vrf forwarding Red
```

```
ip add 192.168.2.2 255.255.255.0
```

```
no shut
```

```
int e0/2
```

```
ip vrf forwarding Blue
```

```
ip add 192.168.3.2 255.255.255.0
```

```
no shut
```

```
int e0/3
```

```
ip vrf forwarding Red
```

```
ip add 192.168.4.2 255.255.255.0
```

```
no shut
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MO4TBFUAMhrOQN-C7q2%2F-MO4XgWyTzXXKP6xkZjK%2Fimage.png?alt=media\&token=9268e8e7-37a8-434c-80e8-67ed3009acde)

> 看看 ISP 路由器的全局路由表

```
do sh ip ro connected
```

> 檢查 VRF 路由表

```
do sh ip ro vrf Blue
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MO4TBFUAMhrOQN-C7q2%2F-MO4_bSg3VgO82lGHjny%2Fimage.png?alt=media\&token=858c58ca-109d-4c60-bda1-4aced36f43d6)

```
do sh ip ro vrf Red
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MO4TBFUAMhrOQN-C7q2%2F-MO4_SPnmT7ml64EXxI0%2Fimage.png?alt=media\&token=614f403e-7893-4bbb-8d45-a804513dd86c)

```
do ping vrf Blue 192.168.1.1
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MO4TBFUAMhrOQN-C7q2%2F-MO4YyTfTreJzhtuYcSf%2Fimage.png?alt=media\&token=4ab6f97d-a7e9-46b3-b53a-58219d490805)

> 配置靜態路由，則必須指定正確的 VRF。
>
> &#x20;R Blue 01有一個 IP 位址為 1.1.1.1 / 32 的環回接口。
>
> 我們在 ISP 路由器上創建一個靜態路由，以便我們可以訪問它

```
exit
```

```
ip route vrf Blue 1.1.1.1 255.255.255.255 192.168.1.1
```

```
do ping vrf Blue 1.1.1.1
```

```
ip route vrf Red 2.2.2.2 255.255.255.255 192.168.2.1
```

```
do ping vrf Red 2.2.2.2
```

```
ip route vrf Blue 3.3.3.3 255.255.255.255 192.168.3.1
```

```
do ping vrf Blue 3.3.3.3
```

```
ip route vrf Red 4.4.4.4 255.255.255.255 192.168.4.1
```

```
do ping vrf Red 4.4.4.4
```

```
do sh ip ro vrf Upper
```
{% endtab %}
{% endtabs %}

### OSPF

{% tabs %}
{% tab title="R1 ( Blue 01 )" %}
```
router ospf 1
```

```
network 192.168.1.0 0.0.0.255 area 0
```

```
network 1.1.1.1 0.0.0.0 area 0
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MO4TBFUAMhrOQN-C7q2%2F-MO4bvOHtQt756s_-bgV%2Fimage.png?alt=media\&token=8b0902b5-5b68-4e73-9a79-2a4135ff18d5)
{% endtab %}

{% tab title="R2 ( Red 01 )" %}
```
router ospf 1
```

```
network 192.168.2.0 0.0.0.255 area 0
```

```
network 2.2.2.2 0.0.0.0 area 0
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MO4TBFUAMhrOQN-C7q2%2F-MO4c67gqDqXjCzBI9vl%2Fimage.png?alt=media\&token=0472a97a-33fa-4a14-908b-c363cf911172)
{% endtab %}

{% tab title="R3 ( Blue 02 )" %}
```
router ospf 1
```

```
network 192.168.3.0 0.0.0.255 area 0
```

```
network 3.3.3.3 0.0.0.0 area 0
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MO4TBFUAMhrOQN-C7q2%2F-MO4cUREYSiVft2ZaRlg%2Fimage.png?alt=media\&token=9eef3b63-6894-4242-981a-3eb9b003216a)
{% endtab %}

{% tab title="R4 ( Red 02 )" %}
```
router ospf 1
```

```
network 192.168.4.0 0.0.0.255 area 0
```

```
network 4.4.4.4 0.0.0.0 area 0
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MO4TBFUAMhrOQN-C7q2%2F-MO4ceYxvoLHaDaaTKGU%2Fimage.png?alt=media\&token=4e6262d4-2958-4269-a079-a4e9d2cf4888)
{% endtab %}

{% tab title="R5 ( ISP )" %}
> Blue

```
router ospf 1 vrf Blue
```

```
network 192.168.1.0 0.0.0.255 area 0
```

```
network 192.168.3.0 0.0.0.255 area 0
```

> Red

```
router ospf 2 vrf Red
```

```
network 192.168.2.0 0.0.0.255 area 0
```

```
network 192.168.4.0 0.0.0.255 area 0
```

> ISP 路由器上的 VRF 路由表現在的樣子

```
do sh ip ro vrf Blue ospf
```

```
do sh ip ro vrf Red ospf
```
{% endtab %}
{% endtabs %}

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MOyll78ol7jjkHCKzFv%2F-MOyoiK0Nvp2YdOID4QO%2FComputer%20Network%20-%20VRF%20%26%20OSPF.jpg?alt=media\&token=f922cd89-9521-49dd-be1e-3720fce1f5c5)

* **Dynamic NAT ( DAT )：Many to Many Mopping**
* **Port NAT ( PAT ) ：Many to 1**
* **Static NAT：1 to 1**

### Part 01.

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MOypMSwwOpr3c3rfh7S%2F-MOyqUSY36lKSRGanJkF%2Fimage.png?alt=media\&token=93744bc9-192d-47fc-a0e9-a1ffc307d6d6)

{% tabs %}
{% tab title="R1" %}
```
int e0/0
```

```
ip add 12.1.1.1 255.255.255.0
```

```
no shut
```

```
int lo1
```

```
ip add 192.168.1.1 255.255.255.0
```

```
no shut
```

```
int lo2
```

```
ip add 192.168.2.1 255.255.255.0
```

```
no shut
```

```
exit
```

```
ip route 0.0.0.0 0.0.0.0 12.1.1.2
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MOyqcuzfAuxfiB31l0Y%2F-MP15uKyR9CD4G0wj-aF%2Fimage.png?alt=media\&token=1f556c09-17ec-4f14-819c-c671183284a0)

```
do ping 12.1.1.2
```

```
do ping 23.1.1.3
```

```
do ping 23.1.1.3 source 192.168.1.1
```

```
do ping 23.1.1.3 source 192.168.2.1
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MPM21S9cWO6LfWb6KGG%2F-MPM7uo1k4HnfQ4mPdh0%2Fimage.png?alt=media\&token=2d4ef70c-a868-4763-910e-ad994c09be69)

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MPM21S9cWO6LfWb6KGG%2F-MPM70ll2-BP4u13SeaT%2Fimage.png?alt=media\&token=5cd174dc-4368-4b0d-aa3a-c48615deb091)

```
ping 3.3.3.3 source 192.168.1.1
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MPM21S9cWO6LfWb6KGG%2F-MPMCThmqEos7ORBdHUS%2Fimage.png?alt=media\&token=4e8502a5-35b8-4310-88a8-05e4986dae44)

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MPM21S9cWO6LfWb6KGG%2F-MPMDpIznGeKQsfLZ4zk%2Fimage.png?alt=media\&token=92e83a08-ea9e-4e2b-afbd-74fe6ef497e4)
{% endtab %}

{% tab title="R2" %}
```
int e0/0
```

```
ip add 12.1.1.2 255.255.255.0
```

```
no shut
```

```
int e0/1
```

```
ip add 23.1.1.2 255.255.255.0
```

```
no shut
```

```
exit
```

```
ip route 192.168.1.0 255.255.255.0 12.1.1.1
```

```
ip route 192.168.2.0 255.255.255.0 12.1.1.1
```

```
ip route 0.0.0.0 0.0.0.0 23.1.1.3
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MOyqcuzfAuxfiB31l0Y%2F-MP16_l98zIr_5vX8pJ4%2Fimage.png?alt=media\&token=5e05df8a-2323-4f3d-b0f8-047256ac41ca)

```
do ping 23.1.1.3
```

```
do ping 3.3.3.3
```

```
do ping 8.8.8.8
```

```
access-list 1 permit 192.168.1.0 0.0.0.255
```

```
access-list 2 permit 192.168.2.0 0.0.0.255
```

```
ip nat pool DNAT 23.1.1.100 23.1.1.200 netmask 255.255.255.0
```

```
int e0/0
```

```
ip nat inside
```

```
int e0/1
```

```
ip nat outside
```

```
exit
```

```
ip nat inside source list 1 pool DNAT
```

```
ip nat inside source list 2 pool DNAT
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MOyqcuzfAuxfiB31l0Y%2F-MP197JnvxXQd74fc1_5%2Fimage.png?alt=media\&token=71a1d519-ae4a-4e89-ae5c-5d47a91a4cc5)

```
exit
```

```
sh ip nat translations
```

```
sh ip nat statistics
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MPM21S9cWO6LfWb6KGG%2F-MPM8sVn-N95g25l1_BW%2Fimage.png?alt=media\&token=b1d435ae-2322-4d4f-934b-02d71ffcf79d)

```
conf t
```

```
no ip nat inside source list 1 pool DNAT
```

```
no ip nat inside source list 2 pool DNAT
```

```
ip nat pool PAT 23.1.1.2 23.1.1.2 netmask 255.255.255.0
```

```
ip nat inside source list 1 pool DNAT overload
```

```
ip nat inside source list 2 pool DNAT overload
```

```
exit
```

```
debug ip nat
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MPM21S9cWO6LfWb6KGG%2F-MPMAwgpJ370C90Z13cc%2Fimage.png?alt=media\&token=d084d79d-4b2d-445e-976e-be8ba3bad474)

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MPM21S9cWO6LfWb6KGG%2F-MPMCY8Tsetl38BypRGk%2Fimage.png?alt=media\&token=568a39a5-0799-48e0-930f-a7f0bff82287)
{% endtab %}

{% tab title="R3" %}
```
int e0/0
```

```
ip add 23.1.1.3 255.255.255.0
```

```
no shut
```

```
int lo1
```

```
ip add 3.3.3.3 255.255.255.255
```

```
no shut
```

```
int lo2
```

```
ip add 8.8.8.8 255.255.255.255
```

```
no shut
```

```
exit
```

```
do sh ip ro
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MOyqcuzfAuxfiB31l0Y%2F-MP18-j5V7meybTMZfga%2Fimage.png?alt=media\&token=56683c0a-7ad6-46ce-bf90-a0b7ac30af70)

```
conf t
```

```
line vty 0 4
```

```
password cisco
```

```
login
```

```
transport input telnet
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MPM21S9cWO6LfWb6KGG%2F-MPMDE9zGAt16NZoFTMT%2Fimage.png?alt=media\&token=dd4aa6f0-155c-4a56-b49d-be5928d5e395)
{% endtab %}
{% endtabs %}

### Part 02.

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MPM21S9cWO6LfWb6KGG%2F-MPMKii0F0wcYE2Uznl6%2Fimage.png?alt=media\&token=5a534163-de70-400f-8f1f-5205c1274450)

{% tabs %}
{% tab title="Linux" %}
```
ifconfig eth0 192.168.3.2/24
```

```
ip addr add 192.168.3.2/24 brd + dev eth0
```

```
ip route add default via 192.168.3.1
```

```
ping 192.168.3.1
```

```
ping 12.1.1.1
```

```
ping 12.1.1.2
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MPM21S9cWO6LfWb6KGG%2F-MPMPaU-37wVGgEh4V_w%2Fimage.png?alt=media\&token=c0c84f5f-4e23-47be-9494-ed90fc5d677b)

```
ping 23.1.1.2
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MPM21S9cWO6LfWb6KGG%2F-MPMPgTmgTKbGD-daasF%2Fimage.png?alt=media\&token=cfe8b20f-6770-4488-b766-e351de2dc697)

```
ip route add default 192.168.3.2/24 brd + dev eth0
```

```
wireshark
```

```
telnet 23.1.1.3
```
{% endtab %}

{% tab title="R1" %}
```
conf t
```

```
int e0/1
```

```
ip add 192.168.3.1 255.255.255.0
```

```
no shut
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MPM21S9cWO6LfWb6KGG%2F-MPMOLgUzY9t6r6J3TK8%2Fimage.png?alt=media\&token=095471b9-30e0-40e0-9de2-e04f1b679f71)
{% endtab %}

{% tab title="R2" %}
```
conf t
```

```
ip route 192.168.3.0 255.255.255.0 12.1.1.1
```

```
access-list 3 permit 192.168.3.0 0.0.0.255
```

```
ip nat inside source list 3 pool DNAT overload
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MPM21S9cWO6LfWb6KGG%2F-MPMOgI2HjDpISExDdgI%2Fimage.png?alt=media\&token=587edc6f-40cd-4a34-8193-a40a44e59d81)
{% endtab %}

{% tab title="R3" %}
![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MPM21S9cWO6LfWb6KGG%2F-MPMSUHJIHA1_rNlw5KH%2Fimage.png?alt=media\&token=6f29c783-99be-4fab-bb95-fb03b3e0db1b)
{% endtab %}
{% endtabs %}

### Part 03.

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MPMSmEHaWJdRoVhK11R%2F-MPMZqG_FC4oXKPPh3VN%2Fimage.png?alt=media\&token=9cc86b2b-4a4e-41c6-a2a1-14524eb77448)

{% tabs %}
{% tab title="R1" %}
```
int e0/0
```

```
ip add 12.1.1.1 255.255.255.0
```

```
no shut
```

```
exit
```

```
ip route 0.0.0.0 0.0.0.0 12.1.1.2
```

```
line vty 0 4
```

```
password cisco
```

```
login
```

```
transport input telnet
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MPMSmEHaWJdRoVhK11R%2F-MPM_nkDdDi2OlhshXNJ%2Fimage.png?alt=media\&token=468ea6e5-0579-4059-ab2b-aacbd257090f)
{% endtab %}

{% tab title="R2" %}
```
int e0/0
```

```
ip add 12.1.1.2 255.255.255.0
```

```
no shut
```

```
int e0/1
```

```
ip add 23.1.1.2 255.255.255.0
```

```
no shut
```

```
exit
```

```
ip route 0.0.0.0 0.0.0.0 23.1.1.3
```

```
telnet 12.1.1.1
```

```
exit
```

```
int e0/0
```

```
ip nat inside
```

```
int e0/1
```

```
ip nat outside
```

```
exit
```

```
ip nat inside source static 12.1.1.1 23.1.1.10
```

```
do sh ip nat translations
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MPMSmEHaWJdRoVhK11R%2F-MPMcJ54Ii9VbEAI328Q%2Fimage.png?alt=media\&token=0e01b248-0b70-4b6c-91a0-979c88b0a983)
{% endtab %}

{% tab title="R3" %}
```
int e0/0
```

```
ip add 23.1.1.3 255.255.255.0
```

```
no shut
```

```
exit
```

```
do ping 23.1.1.2
```

```
telnet 23.1.1.10
```

![](https://1182807199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHJUVmIukmM2g9Ai7CY%2F-MPMSmEHaWJdRoVhK11R%2F-MPMd7Ru1PJfsXtFZlkk%2Fimage.png?alt=media\&token=f600e382-a388-46b3-976c-2f1ef11c72d5)
{% endtab %}
{% endtabs %}
