使用多个参数运行Linux命令

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.

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.

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.