Mininet Operations
http://csie.nqu.edu.tw/smallko/sdn/mininet-operations.htm
[Descriptions]
In this lab, I will show how to configure a host as a router. How tostart a dhcp server at a router is also presented. Then I will use iptables tomake a router to own NAT ability. Finally, how to build a GRE tunnels betweentwo local networks is given.
[First Lab: configure a host as a router]
h1--h2--h3 (h2 will be configured as a router)
#!/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') Link(h1, h2) Link(h2, h3, intfName1='h2-eth1') net.build() h2.cmd('ifconfig h2-eth0 192.168.10.1 netmask 255.255.255.0') h2.cmd('ifconfig h2-eth1 192.168.20.1 netmask 255.255.255.0') h2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward") h1.cmd("ifconfig h1-eth0 0") h3.cmd("ifconfig h3-eth0 0") h1.cmd("ip address add 192.168.10.2/24 dev h1-eth0") h1.cmd("ip route add default via 192.168.10.1 dev h1-eth0") h3.cmd("ip address add 192.168.20.2/24 dev h3-eth0") h3.cmd("ip route add default via 192.168.20.1 dev h3-eth0") CLI(net) net.stop() |
[Second Lab: start a dhcp server]
h1--h2--h3 (h2 will be configured as a router.Also, a dhcp server is running at h2.)
Before theexperiment, use "sudo apt-get install isc-dhcp-server" command toinstall dhcp server in Ubuntu.
#!/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') Link(h1, h2, intfName1='h1-eth0', intfName2='h2-eth0') Link(h2, h3, intfName1='h2-eth1', intfName2='h3-eth0') net.build() h2.cmd('ifconfig h2-eth0 192.168.10.1 netmask 255.255.255.0') h2.cmd('ifconfig h2-eth1 192.168.20.1 netmask 255.255.255.0') h2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward") h2.cmd("service isc-dhcp-server restart &") h1.cmd("ifconfig h1-eth0 0") h3.cmd("ifconfig h3-eth0 0") h1.cmd("dhclient h1-eth0") h3.cmd("dhclient h3-eth0") CLI(net) net.stop() |
Before running themininet script, we have to configure the dhcp server. Edit the dhcpd.conf under/etc/dhcp
Running themininet script.
running thewireshark at h3 to monitor the traffic between h1 and h3.
From the followingfigure, we can see that the packets are transmitted between h1 (192.168.10.6)and h3 (192.168.20.6) ---- Note:Different Domains.
[Third Lab: Add NAT function at h2]
h1-h2-h3 (h2 will be configured as arouter. Also, use iptables to let h2 have the NAT function)
#!/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') Link(h1, h2, intfName1='h1-eth0', intfName2='h2-eth0') Link(h2, h3, intfName1='h2-eth1', intfName2='h3-eth0') net.build() h2.cmd('ifconfig h2-eth0 192.168.10.1 netmask 255.255.255.0') h2.cmd('ifconfig h2-eth1 192.168.20.1 netmask 255.255.255.0') h2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward") h2.cmd("iptables -t nat -A POSTROUTING -o h2-eth1 -s 192.168.10.0/24 -j MASQUERADE") h2.cmd("service isc-dhcp-server restart &") h1.cmd("ifconfig h1-eth0 0") h3.cmd("ifconfig h3-eth0 0") h1.cmd("dhclient h1-eth0") h3.cmd("dhclient h3-eth0") CLI(net) net.stop() |
running themininet script
Check thefollowing figure, we can see that h1 can ping h3 successfully. But from the wiresharkwindow, we can see that the source address of packets sent by h1 will bemodified (NAT).
[Fourth Lab: GREtunnel]
h1---h2---h3---h4
h2,h3: router h1, h4:host
h1-h2: LAN 1(10.0.0.0/24)
h3-h4: LAN 2(10.0.1.0/24)
h2-h3: LAN 3(192.168.10.0/24)
we will create aGRE tunnel between h2 and h3 (h2 will have a new ip address:10.0.2.1/30 whileh3 will have a new ip address:10.0.2.2/30)
Note: Without GREtunnel, h1 cannot ping h4.
#!/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 will be configured as a router h2 = net.addHost('h2') #h3 will be configured as a router h3 = net.addHost('h3') h4 = net.addHost('h4') Link(h1, h2, intfName1='h1-eth0', intfName2='h2-eth0') Link(h2, h3, intfName1='h2-eth1', intfName2='h3-eth0') Link(h3, h4, intfName1='h3-eth1', intfName2='h4-eth0') net.build() h2.cmd('ifconfig h2-eth0 10.0.0.1 netmask 255.255.255.0') h2.cmd('ifconfig h2-eth1 192.168.10.1 netmask 255.255.255.0') h2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward") h3.cmd('ifconfig h3-eth0 192.168.10.2 netmask 255.255.255.0') h3.cmd('ifconfig h3-eth1 10.0.1.1 netmask 255.255.255.0') h3.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward") h1.cmd("ifconfig h1-eth0 0") h4.cmd("ifconfig h4-eth0 0") h1.cmd("ip address add 10.0.0.2/24 dev h1-eth0") h1.cmd("ip route add default via 10.0.0.1 dev h1-eth0") h4.cmd("ip address add 10.0.1.2/24 dev h4-eth0") h4.cmd("ip route add default via 10.0.1.1 dev h4-eth0") #GRE Tunnel between h2 and h3 h2.cmd("ip tunnel add tunnel0 mode gre remote 192.168.10.2 local 192.168.10.1 ttl 255") h2.cmd("ip link set tunnel0 up mtu 1400") h2.cmd("ip addr add 10.0.2.1/30 dev tunnel0") h2.cmd("ip route add 10.0.1.0/24 dev tunnel0") h3.cmd("ip tunnel add tunnel0 mode gre remote 192.168.10.1 local 192.168.10.2 ttl 255") h3.cmd("ip link set tunnel0 up mtu 1400") h3.cmd("ip addr add 10.0.2.2/30 dev tunnel0") h3.cmd("ip route add 10.0.0.0/24 dev tunnel0") CLI(net) net.stop() |
Test 1: mark thered lines above.
Test 2: With GREtunnel between h2 and h3
Now, h1 can pingh4.
Dr. Chih-Heng Ke
Department of Computer Science and InformationEngineering, National Quemoy University, Kinmen, Taiwan
Email: smallko@gmail.com
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通