Tobi696
Tobi696

Reputation: 458

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

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?

Upvotes: 2

Views: 919

Answers (1)

h0ch5tr4355
h0ch5tr4355

Reputation: 2192

. is another argument in your command which has to be passed in the argument list separately:

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

or:

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

Upvotes: 4

Related Questions