mininet-wifi examples 官方例子详解(二)
本博客总结了examples文件下面的例子的用法,需要注意的事项以及今后编码需要用到的技巧。有些例子经过了自己的改动。
例4 associationControl
这个例子是让接入点进行移动,然后在移动的过程中使用ssf选择信号最强的接入
#!/usr/bin/python
'Setting mechanism to optimize the use of APs'
from mininet.node import Controller
from mininet.log import setLogLevel, info
from mn_wifi.cli import CLI_wifi
from mn_wifi.net import Mininet_wifi
from time import sleep
def topology():
"Create a network."
net = Mininet_wifi(controller=Controller)
info("*** Creating nodes\n")
net.addStation('sta1', mac='00:00:00:00:00:02', ip='10.0.0.2/8')
net.addStation('sta2', mac='00:00:00:00:00:03', ip='10.0.0.3/8')
sta3 = net.addStation('sta3', mac='00:00:00:00:00:04', ip='10.0.0.4/8')
net.addStation('sta4', mac='00:00:00:00:00:05', ip='10.0.0.5/8')
net.addStation('sta5', mac='00:00:00:00:00:06', ip='10.0.0.6/8')
net.addStation('sta6', mac='00:00:00:00:00:07', ip='10.0.0.7/8')
net.addStation('sta7', mac='00:00:00:00:00:08', ip='10.0.0.8/8')
net.addStation('sta8', mac='00:00:00:00:00:09', ip='10.0.0.9/8')
net.addStation('sta9', mac='00:00:00:00:00:10', ip='10.0.0.10/8')
net.addStation('sta10', mac='00:00:00:00:00:11', ip='10.0.0.11/8')
ap1 = net.addAccessPoint('ap1', ssid='ssid-ap1', mode='g', channel='1',
position='50,50,0')
ap2 = net.addAccessPoint('ap2', ssid='ssid-ap2', mode='g', channel='6',
position='70,50,0', range=30)
ap3 = net.addAccessPoint('ap3', ssid='ssid-ap3', mode='g', channel='11',
position='90,50,0')
c1 = net.addController('c1')
net.setPropagationModel(model="logDistance", exp=5)
info("*** Configuring wifi nodes\n")
net.configureWifiNodes()
info("*** Associating and Creating links\n")
net.addLink(ap1, ap2)
net.addLink(ap2, ap3)
net.plotGraph(max_x=120, max_y=120)
net.setMobilityModel(time=0, model='RandomWayPoint', max_x=120, max_y=120,
min_v=0.3, max_v=0.5, seed=1, ac_method='ssf')
info("*** Starting network\n")
net.build()
c1.start()
ap1.start([c1])
ap2.start([c1])
ap3.start([c1])
while True:
info('%r\n' % sta3.cmd('iw dev sta3-wlan0 link'))
sleep(2)
info("*** Running CLI\n")
CLI_wifi(net)
info("*** Stopping network\n")
net.stop()
if __name__ == '__main__':
setLogLevel('info')
topology()
可以看到,sta3首先从无信号的地方进入到ap3的范围,这个时候,sta3从无连接转入到连接sta3.
sta3继续移动,可以看到,当进入ap2范围后,随着ap2的信号越来越大,从ap3的连接转入到ap2的连接。
- 注意随机移动函数的写法。
- 如何在终端输入接入点信息。
例5 authentication:wifi密码验证
#!/usr/bin/python
'This example shows how to work with authentication'
#wifi密码验证实例
from mininet.log import setLogLevel, info
from mn_wifi.cli import CLI_wifi
from mn_wifi.net import Mininet_wifi
def topology():
"Create a network."
net = Mininet_wifi()
info("*** Creating nodes\n")
sta1 = net.addStation('sta1', passwd='123456789a', encrypt='wpa2')
sta2 = net.addStation('sta2', passwd='123456789a', encrypt='wpa2')
ap1 = net.addAccessPoint('ap1', ssid="simplewifi", mode="g", channel="1",
passwd='123456789a', encrypt='wpa2',
failMode="standalone", datapath='user')
info("*** Configuring wifi nodes\n")
net.configureWifiNodes()
info("*** Associating Stations\n")
net.addLink(sta1, ap1)
net.addLink(sta2, ap1)
info("*** Starting network\n")
net.build()
ap1.start([])
info("*** Running CLI\n")
CLI_wifi(net)
info("*** Stopping network\n")
net.stop()
if __name__ == '__main__':
setLogLevel('info')
topology()
可以看到,ap有一个加密,然后sta通过密码连接上wifi
例6 handover
handover.py shows how to create a simple mobility scenario where a station moves past two access points, causing the station to hand off from one to the other.
#!/usr/bin/python
'Example for Handover'
'handover.py shows how to create a simple mobility scenario where a station moves past two access points,' \
' causing the station to hand off from one to the other.'
from mininet.node import Controller
from mininet.log import setLogLevel, info
from mn_wifi.cli import CLI_wifi
from mn_wifi.net import Mininet_wifi
from time import sleep
def topology():
"Create a network."
net = Mininet_wifi(controller=Controller)
info("*** Creating nodes\n")
sta1 = net.addStation('sta1', mac='00:00:00:00:00:02', ip='10.0.0.2/8',
range=50)
sta2 = net.addStation('sta2', mac='00:00:00:00:00:03', ip='10.0.0.3/8',
range=20)
ap1 = net.addAccessPoint('ap1', ssid='ssid-ap1', mode='g', channel='1',
position='15,30,0', range=30)
ap2 = net.addAccessPoint('ap2', ssid='ssid-ap2', mode='g', channel='6',
position='55,30,0', range=30)
c1 = net.addController('c1')
net.setPropagationModel(model="logDistance", exp=5)
info("*** Configuring wifi nodes\n")
net.configureWifiNodes()
info("*** Creating links\n")
net.addLink(ap1, ap2)
net.plotGraph(max_x=100, max_y=100)
net.startMobility(time=0)
net.mobility(sta1, 'start', time=1, position='10,30,0')
net.mobility(sta2, 'start', time=2, position='10,40,0')
net.mobility(sta1, 'stop', time=10, position='60,30,0')
net.mobility(sta2, 'stop', time=10, position='25,40,0')
net.stopMobility(time=11)
info("*** Starting network\n")
net.build()
c1.start()
ap1.start([c1])
ap2.start([c1])
time = 0
while time <= 10:
info('%r\n' % sta1.cmd('iw dev sta1-wlan0 link'))
sleep(1)
time += 1
info("*** Running CLI\n")
CLI_wifi(net)
info("*** Stopping network\n")
net.stop()
if __name__ == '__main__':
setLogLevel('info')
topology()
这个是通过range来确定通信范围的,可以看到移动之后,sta1从没连接到连接到ap1,最后从ap1转换到ap2