Home > front end >  Executing a python script from Golang with arguments
Executing a python script from Golang with arguments

Time:02-22

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.

CodePudding user response:

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()
  •  Tags:  
  • go
  • Related