嘗試理解一下mininet,話說mininet是基於python編寫的,代碼結構清晰,簡直清醒脫俗(~(≧▽≦)/~啦啦啦),附上連接mininet,mark一下。node
簡單記錄一下比較重要的類和方法python
Important classes, methods, functions and variables in the above code include: Topo: the base class for Mininet topologies build(): The method to override in your topology class. Constructor parameters (n) will be passed through to it automatically by Topo.__init__(). addSwitch(): adds a switch to a topology and returns the switch name addHost(): adds a host to a topology and returns the host name addLink(): adds a bidirectional link to a topology (and returns a link key, but this is not important). Links in Mininet are bidirectional unless noted otherwise. Mininet: main class to create and manage a network start(): starts your network pingAll(): tests connectivity by trying to have all nodes ping each other stop(): stops your network net.hosts: all the hosts in a network dumpNodeConnections(): dumps connections to/from a set of nodes. setLogLevel( 'info' | 'debug' | 'output' ): set Mininet's default output level; 'info' is recommended as it provides useful information.
本身寫了一個簡單的,h1~h5連接到s1, h6~h10連接到s2,而後s1和s2互聯
一開始想着SingleSwitchTopo的方法來寫LinearTopo,結果一直報錯,╮(╯_╰)╭,明明人家代碼都寫好了
SingleSwitchTopo
是Single switch connected to k hosts
,而Linear topology of k switches, with n hosts per switch
git
#!/usr/bin/python from mininet.topo import Topo from mininet.net import Mininet from mininet.util import irange,dumpNodeConnections from mininet.log import setLogLevel class LinearTopo(Topo): "" """Linear topology of k switches, with n hosts per switch.""" "" def __init__(self, k=2, n=5,**opts): """k:number of switches (and hosts)""" """hconf: host configuration options""" """lconf: ling configuration options""" super(LinearTopo, self).__init__(**opts) self.n = n self.k = k """creates 2 switchs""" switch1 = self.addSwitch('s1') switch2 = self.addSwitch('s2') """creates h1~h5 and addLink switch1""" for i in irange(1,n): host = self.addHost('h%s' %i) self.addLink(host,switch1) """creates h6~h10 and addLink switch2""" for i in irange(n+1,n+5): host =self.addHost('h%s' %i) self.addLink(host,switch2) """addLink switch1 and switch2""" self.addLink(switch1,switch2) def simpleTest(): "Create and test a simple network" topo = LinearTopo(k=2,n=5) net = Mininet(topo) #start the network net.start() print "Dumping host connections" dumpNodeConnections(net.hosts) #test the connections of the network print "Testing network connectivity" net.pingAll() #Test the bandwidth of the h* and h* print "Testing bandwidth between h1 and h2" h1, h2 = net.get('h1', 'h2') net.iperf((h1,h2)) print "Testing bandwidth between h10 and h1" h10, h1 = net.get('h10', 'h1') net.iperf((h10,h1)) #stop the network net.stop() if __name__== '__main__': # Tell mininet to print useful information setLogLevel('info') simpleTest()
命令行直接運行github
#將代碼保存到一個文件中 sudo vi test.py #運行 sudo python test.py
*** Creating network *** Adding controller *** Adding hosts: h1 h2 h3 h4 h5 h6 h7 h8 h9 h10 *** Adding switches: s1 s2 *** Adding links: (h1, s1) (h2, s1) (h3, s1) (h4, s1) (h5, s1) (h6, s2) (h7, s2) (h8, s2) (h9, s2) (h10, s2) (s1, s2) *** Configuring hosts h1 h2 h3 h4 h5 h6 h7 h8 h9 h10 *** Starting controller c0 *** Starting 2 switches s1 s2 ... Dumping host connections h1 h1-eth0:s1-eth1 h2 h2-eth0:s1-eth2 h3 h3-eth0:s1-eth3 h4 h4-eth0:s1-eth4 h5 h5-eth0:s1-eth5 h6 h6-eth0:s2-eth1 h7 h7-eth0:s2-eth2 h8 h8-eth0:s2-eth3 h9 h9-eth0:s2-eth4 h10 h10-eth0:s2-eth5 Testing network connectivity *** Ping: testing ping reachability h1 -> h2 h3 h4 h5 h6 h7 h8 h9 h10 h2 -> h1 h3 h4 h5 h6 h7 h8 h9 h10 h3 -> h1 h2 h4 h5 h6 h7 h8 h9 h10 h4 -> h1 h2 h3 h5 h6 h7 h8 h9 h10 h5 -> h1 h2 h3 h4 h6 h7 h8 h9 h10 h6 -> h1 h2 h3 h4 h5 h7 h8 h9 h10 h7 -> h1 h2 h3 h4 h5 h6 h8 h9 h10 h8 -> h1 h2 h3 h4 h5 h6 h7 h9 h10 h9 -> h1 h2 h3 h4 h5 h6 h7 h8 h10 h10 -> h1 h2 h3 h4 h5 h6 h7 h8 h9 *** Results: 0% dropped (90/90 received) Testing bandwidth between h1 and h2 *** Iperf: testing TCP bandwidth between h1 and h2 *** Results: ['35.8 Gbits/sec', '35.8 Gbits/sec'] Testing bandwidth between h10 and h1 *** Iperf: testing TCP bandwidth between h10 and h1 *** Results: ['36.4 Gbits/sec', '36.4 Gbits/sec'] *** Stopping 1 controllers c0 *** Stopping 11 links ........... *** Stopping 2 switches s1 s2 *** Stopping 10 hosts h1 h2 h3 h4 h5 h6 h7 h8 h9 h10 *** Done
遇到的問題:
一.Mininet只能使用python2使用python3不行,即便用外部庫也不行,
二.PyCharm配置root運行文件
一開始我沒有使用shell來運行,而是使用了PyCharm來跑,而後出現了一個問題,就是Mininet must run as a root,即必須以管理員身份進行,因此須要在PyCharm上設置管理員運行,具體作法:shell
sudo gedit /usr/bin/python_sudo.sh
在文件中寫入下列代碼:bash
#! /bin/bash sudo python $*
cd /usr/bin/ sudo chmod a+x python_sudo.sh
sudo visudo
%sudo ALL=NOPASSWD: /usr/bin/python
File→Settings→Project Interpreter
置換爲本身寫的python_sudo.sh,在下圖選中框右邊有個小齒輪,點那個進行Add就OK了
less
一個能用畫圖工具ide
sudo apt-get install kolourpaint4
而後搜paint就OK了工具