Search code examples
pythonpexpectmininet

How to send a command with the Python Mininet API?


I need to use the Mininet Python API to execute commands on different hosts.

I have tried using both the API and running it using subprocess and pexpect to make sure it isn't a problem with the Mininet configuration.

    net = Mininet()

    s1 = net.addSwitch('s1')

    for n in range(1,4):
        h = net.addHost('h%s' % n)
        net.addLink(h, s1)

    net.addController('c0', controller=RemoteController, ip='127.0.0.1', 
    port=6653)

    net.start()

    h1 = net.get('h1')
    h1.cmd('ping h2')

This does not execute the command h1 ping h2 (checking with Wireshark)

This on the other hand does work:

    child = pexpect.spawn('sudo mn --topo single,3 --controller remote')
    child.expect('mininet>')
    print child.before
    time.sleep(5)

    child.sendline('h1 ping h2')
    time.sleep(60)

I need to use the API and not pexpect due to the nature of what I am trying to achieve (send multiple commands so different hosts are sending traffic at the same time. In my tests, it seems pexpect can only execute one command after eachother).

Why does h1.cmd('ping h2') not work?


Solution

  • The command:

    h1.cmd('ping h2')
    

    Was returning a debug error:

    Unable to resolve 'h2'
    

    Despite the documentation HERE stating that is how you format commands.

    This was fixed by using the following instead:

    h1.cmd('ping 10.0.0.2')
    

    I still do not know the reason for this though. If anyone knows I would love to hear. I hope this helps someone.