本文最后更新于:2025年11月19日 下午
安装软件 1 2 3 4 sudo apt install mininetsudo apt install openvswitch-testcontrollersudo apt install isc-dhcp-client
基础命令使用 先运行如下命令,创建一个两个主机连在一个交换机s1下的拓扑。
另一个终端,往s1里面添加物理接口eth1u
1 sudo ovs-vsctl add-port s1 eth1u
查看交换机状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 $ sudo ovs-vsctl show 0d592c85 -23b8 -476d-92a5-91dc52f61822 Bridge s1 Controller "ptcp:6654" Controller "tcp:127.0.0.1:6653" is_connected : true fail_mode: secure Port s1-eth1 Interface s1-eth1 Port s1 Interface s1 type: internal Port eth1u Interface eth1u Port s1-eth2 Interface s1-eth2 ovs_version: "3.3.0"
测试h1,h2主机联通性:
1 2 3 4 mininet> h1 ping h2PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data. 64 bytes from 10.0.0.2: icmp_seq =1 ttl =64 time =0.982 ms 64 bytes from 10.0.0.2: icmp_seq =2 ttl =64 time =0.258 ms
dump命令查看虚拟主机接口信息:
1 2 3 4 5 mininet > dump<Host h1: h1-eth0:10 .0.0.1 pid=883513> <Host h2: h2-eth0:10 .0.0.2 pid=883515> <OVSSwitch s1: lo:127 .0.0.1,s1-eth1:None,s1-eth2:None pid=883520> <OVSController c0: 127.0.0.1:6653 pid=883506>
给虚拟主机添加ip地址和路由,在mininet命令中运行
1 2 3 4 5 6 mininet> h1 ip addr add 192.168.2.2/24 dev h1-eth0 mininet> h1 ip r add default via 192.168.2.1 mininet> h1 ping 223.5.5.5PING 223.5.5.5 (223.5.5.5) 56(84) bytes of data. 64 bytes from 223.5.5.5: icmp_seq =1 ttl =115 time =9.10 ms 64 bytes from 223.5.5.5: icmp_seq =2 ttl =115 time =7.31 ms
除了可以在mininet命令行运行程序,还可以通过xterm直接跑某个虚拟主机
在宿主机上可以通过dump得到的pid信息,在某个host上运行程序
清理 有时候mininet非正常退出,会存在非常多的接口,可以执行如下命令进行清理
模拟128个客户端 因为客户端数量多,不方便一个个执行命令。使用python脚本创建虚拟环境更方便。github上有官方的很多例子程序 ![[client_128_dhclient.py]]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 """ 模拟128个客户端通过dhclient获取地址 """ import reimport sys,timeimport subprocessfrom sys import exit from mininet.cli import CLIfrom mininet.log import setLogLevel, info, errorfrom mininet.net import Mininetfrom mininet.link import Intffrom mininet.topo import SingleSwitchTopofrom mininet.node import OVSKernelSwitchfrom mininet.util import quietRundef checkIntf ( intf ): "Make sure intf exists and is not configured." config = quietRun( 'ifconfig %s 2>/dev/null' % intf, shell=True ) if not config: error( 'Error:' , intf, 'does not exist!\n' ) exit( 1 )if __name__ == '__main__' : setLogLevel( 'info' ) if (len (sys.argv) < 3 ): print (f"help: {sys.argv[0 ]} {{interface}} {{client-num}}" ) exit(1 ) intfName = sys.argv[1 ] client_num = int (sys.argv[2 ]) info( '*** Connecting to hw intf: %s' % intfName ) info( '*** Checking' , intfName, '\n' ) checkIntf( intfName ) info( '*** Creating network\n' ) topo = SingleSwitchTopo(k=client_num) net = Mininet(topo=topo, switch=OVSKernelSwitch, build=True , autoSetMacs=True , waitConnected=True ) switch = net.switches[ 0 ] info( '*** Adding hardware interface' , intfName, 'to switch' , switch.name, '\n' ) _intf = Intf( intfName, node=switch ) net.start() for idx,host in enumerate (net.hosts): print (f'{host} get ip...' ) output = host.cmd('dhclient 2>&1' ) print (host.params['ip' ], host.cmd("ip a | awk '/192/{print $2}'" ).strip()) output = host.cmd("ip -4 r | awk '/default/{print $3}'" ) gw = output.strip() print (f"==gw=={gw} ==" ) host.cmd(f"ping {gw} >/dev/null &" ) CLI( net ) net.stop() print ("killall dhclient..." ) subprocess.run('killall dhclient' ,shell=True )
注意 (1)mininet只是网络空间和mount进行了隔离,文件系统并没有隔离。并且mininet是以root运行的,在mininet内文件系统的修改都会反应到宿主机上。 (2)建议在虚拟机内使用,避免影响主机的网络环境。
其他 kathara应该也可以使用python脚本 来创建实验室,模拟128个客户端。但不如mininet轻量。
[[vmware 接口混杂模式]]官方文档 github例子程序
人生苦短,远离bug
Leon, 2025-03-25