使用Golang从Python脚本执行带参数的操作。

huangapple go评论77阅读模式
英文:

Executing a python script from Golang with arguments

问题

我正在尝试从Golang执行一个Python脚本(用于访问远程机器并运行命令),但出现了"exit status 2"的错误。

out, err := exec.Command("/usr/local/opt/bin/python3.7", "/users/test.py -i 12.13.14.15 --cmd \"uptime && date\"").Output()

if err != nil {
    fmt.Printf("%s", err)
} else {
    fmt.Println("Command Successfully Executed")
    output := string(out[:])
    fmt.Println(output)
}

输出结果为:

exit status 2

谢谢。

英文:

I am trying to execute a python script ( which is used for accessing remote machines and run commands ) from Golang, it errors with "exit status 2"

out, err := exec.Command("/usr/local/opt/bin/python3.7", "/users/test.py -i 12.13.14.15 --cmd \"uptime && date\"").Output()


if err != nil {
	fmt.Printf("%s", err)
} else {
	fmt.Println("Command Successfully Executed")
	output := string(out[:])
	fmt.Println(output)
}

Output

exit status 2

thank you.

答案1

得分: 1

你正在将所有内容作为单个参数传递给可执行文件。相反,你需要单独传递每个参数:

out, err := exec.Command("/usr/local/opt/bin/python3.7", "/users/test.py", "-i", "12.13.14.15", "--cmd", "uptime && date").Output()
英文:

You are passing a single argument to the executable containing everything. Instead, you have to pass each argument separately:

out, err := exec.Command("/usr/local/opt/bin/python3.7", "/users/test.py", "-i", "12.13.14.15", "--cmd", "uptime && date").Output()

huangapple
  • 本文由 发表于 2022年2月22日 03:41:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/71212143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定