使用非阻塞命令生成结构 2 流量

fabric 2 traffic generation with non-blocking commands

我需要 运行 使用具有不同客户端和服务器命令的流量生成器进行一些测试。我想将其滚动到 fabric2 脚本中,该脚本在 cd 进入 /root.

时执行流量生成命令

我在 iperf 机器上有 public-密钥验证。我如何运行 在 fabric2 下测试流量生成?

获得 运行ning 有点有趣,因为 fabric2 文档没有包含太多关于 run() 参数的信息...您需要查看 invoke Runner.run() 文档查看所有面料 run() 关键字。

在这种情况下使 iperf 工作的关键是在我 运行 iperf 服务器命令时设置 pty=Trueasynchronous=True。如果我没有 运行 异步的 iperf 服务器,它会阻止 iperf 客户端命令的执行。

# Save this script as run_iperf.py and run with "python run_iperf.py"
from getpass import getuser
import os

#from fabric import Config, SerialGroup, ThreadingGroup, exceptions, runners
#from fabric.exceptions import GroupException
from fabric import Connection

server_vm = "10.1.0.1"
client_vm = "10.2.0.1"

# This matters because my user .ssh/id_rsa.pub is authorized on the remote sytems
assert getuser()=="mpenning"

hosts = list()
conn1 = Connection(host=client_vm, user="root",
    connect_kwargs={"key_filename": os.path.expanduser("~/.ssh/id_rsa")})
conn2 = Connection(host=server_vm, user="root",
    connect_kwargs={"key_filename": os.path.expanduser("~/.ssh/id_rsa")})
hosts.append(conn1)
hosts.append(conn2)

iperf_udp_client_cmd = "nice -19 iperf3 --plus-more-client-commands"
iperf_udp_server_cmd = "nice -19 iperf3 --plus-more-server-commands"

# ThreadingGroup is optional for this use case, but the iperf commands
#     definitely require pty and asynchronous (server-side)...
#     ThreadingGroup() is required for concurrent fabric commands.
#
# Uncomment below to use ThreadingGroup()...
# t_hosts = ThreadingGroup.from_connections(hosts)
#
# also ref invoke Runner.run() for more run() args:
#     -> https://github.com/pyinvoke/invoke/blob/master/invoke/runners.py
with conn2.cd("/root"):
    conn2.run(iperf_udp_server_cmd, pty=True, asynchronous=True, disown=False, echo=True)
    
with conn1.cd("/root"):
    conn1.run("sleep 1;%s" % iperf_udp_client_cmd, pty=True, asynchronous=False, echo=True)

此脚本大致基于此答案: