1

After some digging I can run Linux commands from go like this:

func main() {
    lsCmd := exec.Command("ls")
    lsOut, err := lsCmd.Output()
    if err != nil {
        panic(err)
    }
    fmt.Println(">ls")
    fmt.Println(string(lsOut))
}

What I want to do is, running following command in remote machine:

ssh -p $someport $someuser@$someip 'ls'

I can do this successfully from my terminal, but when I try to run it within Go, I get following error:

panic: exec: "ssh -p $someport $someuser@$someip 'ls'": executable file not found in $PATH

Update: I updated the question for convenience.

2 Answers 2

7

As per the doc about the exec package, program name and arguments are differents parameters of the Command method. You should do something like that :

exec.Command("ssh", "-p port", "user@ip", "'ls'")

If you need something more elaborate, you could also look at the go.crypto/ssh package.

Sign up to request clarification or add additional context in comments.

Comments

-1

If you want run multiple commands on a remote machine the below trick might help you achieve this.

Ssh username@ip < EOf

ls -I

Pwd

Uname

Eof

Please note that it doesn't pass any special characters like . , etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.