博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
初试mininet(可选PyCharm)
阅读量:6173 次
发布时间:2019-06-21

本文共 5317 字,大约阅读时间需要 17 分钟。

目录

0x00 Mininet

尝试理解一下mininet,话说mininet是基于python编写的,代码结构清晰,简直清醒脱俗(~(≧▽≦)/~啦啦啦),附上链接,mark一下。

0x01 Important classes, methods, functions

简单记录一下比较重要的类和方法

Important classes, methods, functions and variables in the above code include:Topo: the base class for Mininet topologiesbuild(): 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 nameaddHost(): adds a host to a topology and returns the host nameaddLink(): 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 networkstart(): starts your networkpingAll(): tests connectivity by trying to have all nodes ping each otherstop(): stops your networknet.hosts: all the hosts in a networkdumpNodeConnections(): 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.

0x02 Sample

自己写了一个简单的,h1~h5链接到s1, h6~h10链接到s2,然后s1和s2互联

一开始想着SingleSwitchTopo的方法来写LinearTopo,结果一直报错,╮(╯_╰)╭,明明人家代码都写好了
SingleSwitchTopoSingle switch connected to k hosts,而Linear topology of k switches, with n hosts per switch

#!/usr/bin/pythonfrom mininet.topo import Topofrom mininet.net import Mininetfrom mininet.util import irange,dumpNodeConnectionsfrom mininet.log import setLogLevelclass 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()

0x04 run in shell

命令行直接运行

#将代码保存到一个文件中sudo vi test.py#运行sudo python test.py

0x05 Output

*** 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 hostsh1 h2 h3 h4 h5 h6 h7 h8 h9 h10 *** Starting controllerc0 *** Starting 2 switchess1 s2 ...Dumping host connectionsh1 h1-eth0:s1-eth1h2 h2-eth0:s1-eth2h3 h3-eth0:s1-eth3h4 h4-eth0:s1-eth4h5 h5-eth0:s1-eth5h6 h6-eth0:s2-eth1h7 h7-eth0:s2-eth2h8 h8-eth0:s2-eth3h9 h9-eth0:s2-eth4h10 h10-eth0:s2-eth5Testing network connectivity*** Ping: testing ping reachabilityh1 -> 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 controllersc0 *** Stopping 11 links...........*** Stopping 2 switchess1 s2 *** Stopping 10 hostsh1 h2 h3 h4 h5 h6 h7 h8 h9 h10 *** Done

0x06 Problems

遇到的问题:

一.Mininet只能使用python2使用python3不行,即使用外部库也不行
二.PyCharm配置root运行文件
一开始我没有使用shell来运行,而是使用了PyCharm来跑,然后出现了一个问题,就是Mininet must run as a root,即必须以管理员身份进行,所以需要在PyCharm上设置管理员运行,具体做法:

-1.在/usr/bin/目录下新建文件 python_sudo.sh :

shell sudo gedit /usr/bin/python_sudo.sh
在文件中写入下列代码:

#! /bin/bash sudo python $*

-2. 给sh文件赋予权限:

cd /usr/bin/sudo chmod a+x python_sudo.sh

-3. 编辑visudo(其他工具有点问题):

sudo visudo

-4.在最后一行输入,然后保存:

%sudo ALL=NOPASSWD: /usr/bin/python

-5. 将pycharm 中的

File→Settings→Project Interpreter
置换为自己写的python_sudo.sh,在下图选中框右边有个小齿轮,点那个进行Add就OK了
1165691-20190228225125327-2443585.png

-6. 同理可以对python3等进行设定,只要在将python改成对应版本就可以(前提是在这个目录下已安装)

0x07 Paint

一个能用画图工具

sudo apt-get install kolourpaint4

然后搜paint就OK了

转载于:https://www.cnblogs.com/FlyerBird/p/10453772.html

你可能感兴趣的文章
【云宏大讲坛】超融合,融合的不仅是基础架构
查看>>
pytnon入门的一些小实例
查看>>
ubuntu下的dock工具
查看>>
饿了么被上海市市场监督局予以警告处分
查看>>
Java项目读取配置文件时,找不到指定的文件???
查看>>
lua/luajit and tcc
查看>>
前端安全即JS代码安全,前端源码安全探讨!
查看>>
如何快速实现异地不同网络打印机共享
查看>>
openinstall免费服务对App推广有哪些作用?
查看>>
基于Docker的微服务CI CD流水线
查看>>
学好SEO需要掌握哪些知识要点?
查看>>
JetBrains GoLand macv2019.1.2中文版如何换成无牵引模式?
查看>>
电气火灾监控系统工作原理
查看>>
中使馆驳斥《金融时报》“中国网络威胁论”
查看>>
【挨踢人物传】茶乡浪子:“传奇”职场路,一生感谢情(第12期)
查看>>
我的友情链接
查看>>
c#关于数据库连接操作的案例
查看>>
聊聊最近接触的媒体查询!
查看>>
HAproxy指南之haproxy重定向应用(案例篇)
查看>>
学习 HTTP协议挺不错的一个类
查看>>