kburke...@gmail.com wrote:

> 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.
>

(my previous post didn't seem to make it.  this is probably better anyway.)

I think ssh is using your login shell execute the remote command so the
<& is being interpreted as a redirect whereas when you run echo on
your local machine, it is simply being fork()/exec()ed and your
string is passed directly as a parameter without being interpreted
by a shell.

When I try your ssh command my shell (bash) gives the following
complaint:

        err: exit status 1
        bash: MsZG: ambiguous redirect

I can get it to work by escaping the '<' and '&' individually as well as
putting the entire offending argument in single quotes.

Which characters are special depends on your shell and even which mode
your shell is running in! This is trickier than it seems at first glance
and I think the ultimate solution is that you'll have to be aware of
which shell you're running on the remote machine and how to properly
escape arguments for it.

It'd be interesting to know if there is a way to get ssh to execute a
command without invoking the login shell. After a quick glance of the
man page and source (for FreeBSD at least) I couldn't find a way.

-ayan

-- 
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