在 Mininet 中批量执行命令

Tam*_*shi 3 bash mininet

给定一个 Mininet 网络,我想模拟流量,例如使用 iperf。为此,我可以运行以下命令:

h5 iperf3 -s -p 1337 &
h6 iperf3 -s -p 1338 &
h1 iperf3 -c h5 -n 10G -b 11M -p 1337 &
h2 iperf3 -c h6 -n 10G -b 11M -p 1338 &
Run Code Online (Sandbox Code Playgroud)

在 Mininet CLI 中,h1, h2... 代表 Mininet 拓扑上的主机。如果 Mininet 命令以主机 ID 开头,则该命令将像在该主机上一样运行。为方便起见,以后出现的任何 ID 都将替换为其 IP 地址。

这些命令有效,但我还没有找到自动化它们的方法。我可以运行一个可以调用 bash 命令的 python 脚本,但上述命令会中断,因为上下文无法理解 mininet ID。手动输入所有这些内容很烦人,即使这是一项复制粘贴工作。

有没有办法向 mininet CLI 发送一批命令?

Tam*_*shi 5

Mininet 文档中,可以使用__init__脚本使用 CLI 模式。脚本的每一行都将在 CLI 中执行,而不会因用户输入而停止。粗略浏览一下本文档即可了解 CLI 用于解释和执行命令的各个方法。

这是一个例子:

myScript = "genTraffic.sh"
CLI(net, script=myScript) # Batch mode script execution
CLI(net) # Send user in the CLI for additional manual actions
Run Code Online (Sandbox Code Playgroud)

我使用的脚本与问题中发布的脚本相同,带有前置的 Ping,让网络控制器和 Mininet 有时间“看到”彼此。

h1 ping h5 -c 3
h2 ping h6 -c 3
h5 iperf3 -s -p 1337 &
h6 iperf3 -s -p 1338 &
h1 iperf3 -c h5 -n 10G -b 11M -p 1337 &
h2 iperf3 -c h6 -n 10G -b 11M -p 1338 &
Run Code Online (Sandbox Code Playgroud)

Host 和 Switch python 对象也有一个名为 的方法cmd。这是在 Mininet CLI 中输入以 egh1或开头的命令时使用的方法。s1我相信使用一系列调用cmd相当于批处理脚本方法。

额外提示:如果您想在 CLI 中使用类似h1或 的令牌s1而不用相关 IP 地址替换它,您可以转义令牌:\h1