Reputation: 3449
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.
Upvotes: 1
Views: 1031
Reputation: 111
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.
Upvotes: -1
Reputation: 9509
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.
Upvotes: 7