使用Golang的crypto / ssh在两个VM上运行iperf3以测量吞吐量。
这里是对手动过程的描述。我正在用golang做同样的事情。
server
身份运行iperf# Login to VM1
ssh [email protected]
# Run iperf server on VM1
iperf3 -s -p 5001
您可以从上图中看到服务器正坐在那里监听。相同的命令将如下所示挂起golang代码。
client
身份运行iperf# Login to VM2
ssh [email protected]
# Run client tests on VM2
iperf3 -c 168.61.222.12 -p 5001 -i 1 -t 3
所有这些在命令行中都很好用。以下是使用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))
}
我是否使用通道异步运行代码?解决此问题的最佳方法是什么?
感谢任何指导。
在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)
}