如何使用来自外部的 python 脚本 运行 mininet 提示符内的命令

How to run the commands inside the mininet prompt using python scripts from outside

我是 mininet 新手。我想 运行 mininet 命令从 python 脚本 "nodes"、"dump" 到 运行。我可以创建拓扑但不能通过我的脚本使用这些命令。我正在使用 Ubuntu 14.04.

import subprocess as sb
import os
print "Single Switch and 4 Hosts per switch topology"
print "Creation of topology"
os.system(" sudo mn --topo = single,4",shell=True)
os.system("nodes")

错误:

sh: 1: nodes: not found
32512

我不想通过 python 脚本创建拓扑,我只想使用 mininet 命令。

问题在于 mininet 是一个交互式 CLI。您不能简单地调用它然后传递另一个命令,因为交互式 CLI 正在阻塞。

有很多方法可以解决这个问题。看这里:Wrapping an interactive CLI in python

并选中 pexpect 以更舒适地与交互式 CLI 进行交互。

按照 nirOs 的建议,您应该使用 Mininet Python 库。通读 https://github.com/mininet/mininet/wiki/Introduction-to-Mininet 以更好地了解如何创建拓扑。 创建后,您可以使用 hostObject.cmd('your command goes here') 。 mininet 中的每个主机都创建了自己的命名空间。 "cmd" 在主机的命名空间中执行给定的命令。 例如 : host1.cmd("ifconfig") #this returns主机的IP信息

很高兴我发现了这个问题。我有一个性质非常相似的子问题。我知道您可以使用例如从 python 向 mininet CLI 执行命令h1.cmd('ifconfig') ,但是有没有办法从 python 到 mininet> 提示做同样的事情?即不将其指向拓扑中的特定主机(我正在模拟故障,因此某些主机已断开连接)。像 net.cmd('python module.py') 这样的东西? @Abhimanyu 辛格@nir0s