【问题标题】:Running iperf3 with crypto/ssh and golang使用 crypto/ssh 和 golang 运行 iperf3
【发布时间】:2020-10-01 18:26:49
【问题描述】:

目标

使用 Golang 的 crypto/ssh 在两个虚拟机上运行 iperf3 来测量吞吐量。

手动流程

这里是对有效的手动过程的描述。我正在尝试在 golang 中做同样的事情。

手动方法:从 jumpbox 连接到 VM1 并以 server 运行 iperf

# Login to VM1
ssh testadmin@168.61.222.12
# Run iperf server on VM1
iperf3 -s -p 5001

您可以从上图中看到服务器正坐在那里监听。相同的命令会挂起 golang 代码,如下所述。

手动方法:从 jumpbox 连接到 VM2 并以 client 运行 iperf

# Login to VM2
ssh testadmin@23.101.124.159

# Run client tests on VM2
iperf3 -c 168.61.222.12 -p 5001 -i 1 -t 3

所有这些都可以在命令行中很好地工作。下面是使用Golang的解释,但是代码挂了。

问题:golang 代码挂起

golang 代码挂在下面一行。查看函数:

putVM1IntoServerMode()

代码挂了:

putVM1IntoServerMode() {}
    # Code hangs here
    out, err := 
      VMConnectServer.hostSession.CombinedOutput(VMConnectServer.commands[0])
      ...
}

这是意料之中的,因为您在手动过程中看到了什么:

主要代码

type VMCONNECT struct {
    hostConnection *ssh.Client
    hostSession    *ssh.Session
    user           string
    hostPort       string
    commands       []string
    password       string
}

var VMConnectServer = VMCONNECT{
    nil,
    nil,
    "testadmin",
    "169.61.222.12:22",
    []string{"iperf3 -s -p 5001"},
    "????",
}
/*************************************
Logic
  connect to server
  put vm into server mode
  connect to client
  run iperf3 tests
  close client
  close server
**************************************/

func main() {

    connectToServer()
    putVM1IntoServerMode()
    // other code ommitted for brevity
}    

# This works fine, no issues
func connectToServer() {
    var err error
    VMConnectServer.hostConnection, VMConnectServer.hostSession, err = connectToHost(VMConnectServer.user, VMConnectServer.hostPort)
    if err != nil {
        panic(err)
    }
}

# Code hangs here
func putVM1IntoServerMode() {
    # Code hangs here
    out, err := VMConnectServer.hostSession.CombinedOutput(VMConnectServer.commands[0])
    if err != nil {
        panic(err)
    }
    fmt.Println(string(out))
}

如何解决

我是否使用通道异步运行代码? 解决此问题的最佳方法是什么?

感谢任何指导。

【问题讨论】:

    标签: go performance-testing channel iperf3


    【解决方案1】:

    成功了!

    在 iperf 的服务器端运行时,非常棘手。

    func runIperfTestsFromVM2() string {
        fmt.Println(VMConnectClient.commands[0])
        out, err := VMConnectClient.hostSession.CombinedOutput("sh -c 'nohup iperf3 -s -p 5001 > /dev/null 2>&1 &'")
        if err != nil {
            panic(err)
        }
        fmt.Println(string(out))
        return string(out)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-04
      • 1970-01-01
      • 2016-01-07
      • 2021-09-22
      • 2021-09-16
      • 2015-10-22
      • 1970-01-01
      • 2018-06-09
      相关资源
      最近更新 更多