Home > Net >  Executing ssh command with args using Golang os/exec leads to Exit status 127
Executing ssh command with args using Golang os/exec leads to Exit status 127

Time:09-08

In my golang program, I need to execute a command on a remote server via ssh. My computer has a private key stored to connect to the server without password.

Here's my code for running remote commands:

out, err := exec.Command("ssh", "myserver.com", "'ls'").Output()
fmt.Println(string(out), err)

This code works as expected, however, when I add arguments to the command executed on the ssh server, the I get an error with Exit Status 127

Example Code:

out, err := exec.Command("ssh", "myserver.com", "'ls .'").Output()
fmt.Println(string(out), err, exercise)

This code leads to: exit status 127

How do I have to format my ssh command in order to avoid this?

CodePudding user response:

. is another argument in your command which has to be past in the argument list separetely:

out, err := exec.Command("ssh", "myserver.com", "ls", ".").Output()

or:

out, err := exec.Command("ssh", "myserver.com", "ls", "/path/to/any/other/dir").Output()
  • Related