【问题标题】:How to send a command with the Python Mininet API?如何使用 Python Mininet API 发送命令?
【发布时间】:2019-01-31 11:24:18
【问题描述】:

我需要使用 Mininet Python API 在不同的主机上执行命令。

我已尝试同时使用 API 并使用 subprocess 和 pexpect 运行它,以确保 Mininet 配置没有问题。

    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')

这不会执行命令h1 ping h2(用Wireshark检查)

另一方面,这确实有效:

    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)

由于我试图实现的性质,我需要使用 API 而不是 pexpect(发送多个命令,以便不同的主机同时发送流量。在我的测试中,似乎 pexpect 只能执行一个命令一个接一个)。

为什么h1.cmd('ping h2') 不起作用?

【问题讨论】:

  • intro 示例建议您应该使用h1.cmd('ping h2'),因为该命令已被定向到 h1。
  • @meuh 好地方,但不幸的是 ping 仍然没有出现在 wireshark 中

标签: python pexpect mininet


【解决方案1】:

命令:

h1.cmd('ping h2')

返回调试错误:

Unable to resolve 'h2'

尽管文档 HERE 指出这是您格式化命令的方式。

改用以下方法解决了这个问题:

h1.cmd('ping 10.0.0.2')

我仍然不知道这是什么原因。如果有人知道我很想听。我希望这对某人有所帮助。

【讨论】:

  • 您允许 DNS 转发/解析吗?向 Wireshark 查询您的 DNS 查询的去向。
【解决方案2】:

Mininet 不知道 h1、h2 等是什么。

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')
h2 = net.get('h2')
h1.cmd('ping h2')

应该可以解决问题:)

【讨论】:

    猜你喜欢
    • 2010-10-14
    • 2015-12-21
    • 1970-01-01
    • 2011-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    相关资源
    最近更新 更多