【问题标题】:linux shell cmd is not working for mininet hostlinux shell cmd 不适用于 mininet 主机
【发布时间】:2020-06-15 03:38:14
【问题描述】:

我正在尝试设置一个 mininet 拓扑,该拓扑由一个连接到三个主机的交换机组成。下面的代码做同样的事情,并且很好地提出了拓扑。但我明白了,host.cmd 都没有生效。如果我将命令复制粘贴到设备控制台上,那么它们可以正常工作。

下面的命令不起作用:

h1.cmd('ifconfig lo 122.1.1.1 netmask 255.255.255.255')

类似地,下面的命令配置的是mac,而不是接口上的IP。

link_h1s1.intf1.config(ip='100.168.0.1/24', mac = '00:00:00:00:00:01')

如果特定于主机,看起来像问题。 在程序结束时,我粘贴输出拓扑。

请有使用 mininet 的专家帮忙

from mininet.net import Mininet
from mininet.node import Controller, OVSKernelSwitch, UserSwitch, DefaultController
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import Link, TCLink

#reference : http://intronetworks.cs.luc.edu/current/html/mininet.html

def topology():

    net = Mininet()

    #Add Controller to the Topology
    c0 = net.addController( 'c0',
         controller=DefaultController)

    # Add hosts and switches
    h1 = net.addHost( 'h1')
    h2 = net.addHost( 'h2')
    h3 = net.addHost( 'h3')

    #Add L2 Switch
    s1 = net.addSwitch( 's1')

    #Add links
    link_h1s1 = net.addLink( h1, s1 , intfName1 = 'h1-s1', intfName2 = 's1-h1')
    link_h2s1 = net.addLink( h2, s1 , intfName1 = 'h2-s1', intfName2 = 's1-h2')
    link_h3s1 = net.addLink( h3, s1 , intfName1 = 'h3-s1', intfName2 = 's1-h3')

    link_h1s1.intf1.config(ip='100.168.0.1/24', mac = '00:00:00:00:00:01')
    link_h2s1.intf1.config(ip='100.168.0.2/24', mac = '00:00:00:00:00:02')
    link_h3s1.intf1.config(ip='100.168.0.3/24', mac = '00:00:00:00:00:03')

    link_h1s1.intf2.config(ip=None, mac = '00:00:00:00:00:04')
    link_h2s1.intf2.config(ip=None, mac = '00:00:00:00:00:05')
    link_h3s1.intf2.config(ip=None, mac = '00:00:00:00:00:06')


    #h1.setIP('122.1.1.1/32')
    h1.cmd('ifconfig h1-s1 100.168.0.1 netmask 255.255.255.0')
    h2.cmd('ifconfig h2-s1 100.168.0.2 netmask 255.255.255.0')
    h3.cmd('ifconfig h3-s1 100.168.0.3 netmask 255.255.255.0')

    #Setting lo IP addresses
    h1.cmd('ifconfig lo 122.1.1.1 netmask 255.255.255.255')
    h2.cmd('ifconfig lo 122.1.1.2 netmask 255.255.255.255')
    h3.cmd('ifconfig lo 122.1.1.3 netmask 255.255.255.255')

    #finished building the Topology
    net.build()

    #Star the Controller
    #c0.start()

    #start the Switches
    #s1.start( [c0] )

    net.start()
    print "*** Running CLI"

    CLI( net )

    print "*** Stopping network"

    net.stop()

if __name__ == '__main__':

    setLogLevel( 'info' )

    topology()

主机 h1 的输出

mininet> h1 ifconfig
h1-s1     Link encap:Ethernet  HWaddr 00:00:00:00:00:01
          inet addr:10.0.0.1  Bcast:10.255.255.255  Mask:255.0.0.0   << default IP 10.1.1.1/8 is taken
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.255.255.255                 << Default loopback addr is taken
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

【问题讨论】:

    标签: networking tcp routes mininet


    【解决方案1】:

    你应该在运行命令之前构建网络

    from mininet.net import Mininet
    from mininet.node import Controller, OVSKernelSwitch, UserSwitch, DefaultController
    from mininet.cli import CLI
    from mininet.log import setLogLevel, info
    from mininet.link import Link, TCLink
    
    #reference : http://intronetworks.cs.luc.edu/current/html/mininet.html
    
    def topology():
    
        net = Mininet()
    
        #Add Controller to the Topology
        c0 = net.addController( 'c0',
             controller=DefaultController)
    
        # Add hosts and switches
        h1 = net.addHost( 'h1')
        h2 = net.addHost( 'h2')
        h3 = net.addHost( 'h3')
    
        #Add L2 Switch
        s1 = net.addSwitch( 's1')
    
        #Add links
        link_h1s1 = net.addLink( h1, s1 , intfName1 = 'h1-s1', intfName2 = 's1-h1')
        link_h2s1 = net.addLink( h2, s1 , intfName1 = 'h2-s1', intfName2 = 's1-h2')
        link_h3s1 = net.addLink( h3, s1 , intfName1 = 'h3-s1', intfName2 = 's1-h3')
    
        link_h1s1.intf1.config(ip='100.168.0.1/24', mac = '00:00:00:00:00:01')
        link_h2s1.intf1.config(ip='100.168.0.2/24', mac = '00:00:00:00:00:02')
        link_h3s1.intf1.config(ip='100.168.0.3/24', mac = '00:00:00:00:00:03')
    
        link_h1s1.intf2.config(ip=None, mac = '00:00:00:00:00:04')
        link_h2s1.intf2.config(ip=None, mac = '00:00:00:00:00:05')
        link_h3s1.intf2.config(ip=None, mac = '00:00:00:00:00:06')
        net.build()
        net.start()
    
        #h1.setIP('122.1.1.1/32')
        h1.cmd('ifconfig h1-s1 100.168.0.1 netmask 255.255.255.0')
        h2.cmd('ifconfig h2-s1 100.168.0.2 netmask 255.255.255.0')
        h3.cmd('ifconfig h3-s1 100.168.0.3 netmask 255.255.255.0')
    
        #Setting lo IP addresses
        h1.cmd('ifconfig lo 122.1.1.1 netmask 255.255.255.255')
        h2.cmd('ifconfig lo 122.1.1.2 netmask 255.255.255.255')
        h3.cmd('ifconfig lo 122.1.1.3 netmask 255.255.255.255')
    
    
        #Star the Controller
        #c0.start()
    
        #start the Switches
        #s1.start( [c0] )
    
        print "*** Running CLI"
    
        CLI( net )
    
        print "*** Stopping network"
    
        net.stop()
    
    if __name__ == '__main__':
    
        setLogLevel( 'info' )
    
        topology()
    

    输出:

    root@grisou-50:~/mininet# python test.py
    *** Configuring hosts
    h1 h2 h3
    *** Starting controller
    c0
    *** Starting 1 switches
    s1 ...
    *** Running CLI
    *** Starting CLI:
    mininet> ifconfig
    *** Unknown command: ifconfig
    mininet> h1 ifconfig
    h1-s1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 100.168.0.1  netmask 255.255.255.0  broadcast 100.168.0.255
            inet6 fe80::200:ff:fe00:1  prefixlen 64  scopeid 0x20<link>
            ether 00:00:00:00:00:01  txqueuelen 1000  (Ethernet)
            RX packets 19  bytes 1626 (1.6 KB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 9  bytes 806 (806.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 122.1.1.1  netmask 255.255.255.255
            inet6 ::1  prefixlen 128  scopeid 0x10<host>
            loop  txqueuelen 1000  (Local Loopback)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    mininet>
    

    最干净的解决方案是直接在 addHost 函数中设置主机的 Ip。在这种情况下,如果您从 CLI 调用 dump,您应该会看到 Ips 设置正确。

    from mininet.net import Mininet
    from mininet.node import Controller, OVSKernelSwitch, UserSwitch, DefaultController
    from mininet.cli import CLI
    from mininet.log import setLogLevel, info
    from mininet.link import Link, TCLink
    
    #reference : http://intronetworks.cs.luc.edu/current/html/mininet.html
    
    def topology():
    
        net = Mininet()
    
        #Add Controller to the Topology
        c0 = net.addController( 'c0',
             controller=DefaultController)
    
        # Add hosts and switches
        h1 = net.addHost( 'h1', ip='100.168.0.1/24', mac = '00:00:00:00:00:01')
        h2 = net.addHost( 'h2', ip='100.168.0.2/24', mac = '00:00:00:00:00:02')
        h3 = net.addHost( 'h3', ip='100.168.0.3/24', mac = '00:00:00:00:00:03')
    
        #Add L2 Switch
        s1 = net.addSwitch( 's1')
    
        #Add links
        link_h1s1 = net.addLink( h1, s1 , intfName1 = 'h1-s1', intfName2 = 's1-h1')
        link_h2s1 = net.addLink( h2, s1 , intfName1 = 'h2-s1', intfName2 = 's1-h2')
        link_h3s1 = net.addLink( h3, s1 , intfName1 = 'h3-s1', intfName2 = 's1-h3')
    
        link_h1s1.intf2.config(ip=None, mac = '00:00:00:00:00:04')
        link_h2s1.intf2.config(ip=None, mac = '00:00:00:00:00:05')
        link_h3s1.intf2.config(ip=None, mac = '00:00:00:00:00:06')
        net.build()
        net.start()
    
    
        #Setting lo IP addresses
        h1.cmd('ifconfig lo 122.1.1.1 netmask 255.255.255.255')
        h2.cmd('ifconfig lo 122.1.1.2 netmask 255.255.255.255')
        h3.cmd('ifconfig lo 122.1.1.3 netmask 255.255.255.255')
    
    
        #Star the Controller
        #c0.start()
    
        #start the Switches
        #s1.start( [c0] )
    
        print "*** Running CLI"
    
        CLI( net )
    
        print "*** Stopping network"
    
        net.stop()
    
    if __name__ == '__main__':
    
        setLogLevel( 'info' )
    
        topology()
    

    【讨论】:

    • 谢谢,这行得通,但我仍然看到一个问题。虽然 h1 ifconfig 显示正确的输出,但在 mininet shell 上,运行 dump 显示默认 IP。你能试试看吗?
    • 另外,没有使用link_h1s1.intf1.config(ip='100.168.0.1/24', mac = '00:00:00:00:00:01')设置IP,只有mac生效。
    • dump显示之前的ip是正常的,因为不是动态检查网络,而是检查mininet中的数据结构。 ifconfig 命令不会修改它。对于奥德部分,我可以问你你想做什么吗?
    • @AbhishekSagar 我添加了最干净的解决方案,也许它可以完全解决您的问题。
    • 好的,感谢您澄清cmd 完成的更新不会显示在dump 输出中。您的解决方案效果很好。但是我在使用h1 = net.addHost( 'h1', ip='100.168.0.1/24', mac = '00:00:00:00:00:01') 时遇到问题,此行不告诉h1 的哪个接口将配置此IP 和mac。如果h1 有多个接口,我将如何在 mininet 拓扑中为它们配置 IP 和 mac(不使用cmd)。 @朱塞佩
    猜你喜欢
    • 2013-03-15
    • 2014-10-06
    • 2014-07-04
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    相关资源
    最近更新 更多