Il giorno sabato 26 novembre 2016 19:35:28 UTC+1, kbur...@gmail.com ha 
scritto:
>
> Hi, I'd like to SSH to a remote host and run an arbitrary command. I found 
> this pretty difficult to do with existing Go libraries.
>
> The first problem I had was around escaping; commands run locally are 
> appropriately escaped but I found it difficult to get the right escaping 
> for commands run remotely. As an example, this hangs, I believe because of 
> the {<& in the middle of the command.
>
> cmd := exec.Command("ssh", "remote-host", "echo", "zdUZUKv{<&MsZG")
> bits, err := cmd.CombinedOutput()
> if err != nil {
> log.Fatal(err)
> }
> os.Stdout.Write(bits)
>
> Note exec.Command("echo", "{<&") runs just fine.
>
>
When using exec.Command, the echo program is executed directly, but when 
you use ssh, the commands are executed by the shell on the remote system, 
AFAIK.
The solution is to quote the command arguments.

The standard library does not seem to have support for shell quoting, but 
you can try to adapt the quoting support from Python:
https://docs.python.org/3/library/shlex.html#shlex.quote
https://github.com/python/cpython/blob/master/Lib/shlex.py#L310

    cmd := exec.Command("ssh", "remote-host", "echo", `"zdUZUKv{<&MsZG"`)

I have not tested it.


> [...]

Manlio

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to