[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] pass command to script

From: Greg Wooledge
Subject: Re: [Help-bash] pass command to script
Date: Thu, 25 Apr 2019 14:24:00 -0400
User-agent: Mutt/1.10.1 (2018-07-13)

On Thu, Apr 25, 2019 at 12:59:41PM -0500, Jesse Hathaway wrote:
> On Thu, Apr 25, 2019 at 10:49 AM Christof Warlich <address@hidden> wrote:
> > Is there any chance to achive something similar with a script?
> Chris, does this achieve your goal?:

I think you misunderstood the goal.

To understand the question, I think you need to start by looking at how
ssh works (which is the same as how various remote-shell implementations,
remsh or rsh, worked).

Let's say I type this:

  ssh host 'echo foo > bar'

The ssh client sends the string 'echo foo > bar' (without the outer quotes)
across the network connection.  The ssh server receives it and hands it to
my shell.  My shell parses it as a command, by breaking it into words and
so forth, and then executes it.  So far, so good.

Now let's say I type this:

  ssh host rm -f "$file"

This is where things break.

A naive reading says that this should be OK.  The ssh client takes the
argument words 'rm' and '-f' and 'somefilename' (the last was expanded by
my shell on the client before ssh was invoked) and passes them to the
server.  The server sees the string 'rm -f somefilename' and passes it to
my shell to be parsed and executed.

What happens when $file contains a space?

The ssh client recieves the three argument words 'rm' and '-f' and
'some file name'.  At that point, ssh still knows that each word is
a single argument.  However, those words can't be put back together
into a shell command -- or at least, not the *same* shell command that
was intended.

ssh doesn't attempt to understand how the various argument words
fit together as a shell command.  ssh simply passes the string
'rm -f some file name' to the server, which passes it to my shell, which
parses and executes it.  The resulting command tries to remove three
different files, not one file.

Now, this is the same problem that Christof is trying to solve.  He wants
to write something that is basically ssh, and has to deal with the same
issues that ssh deals with when someone passes a command to it.

If he manages this, he will have achieved something that rsh/remsh/ssh
did not manage to achieve.  If he does it, I hope he also invents a time
machine (which might be a simpler task), and goes back and teaches the
authors of remote-shell how it should be done.

Personally, I doubt it can be done with anything less powerful than a
complete shell syntax analyzer.  You'd need a program that can understand
the context of every single character of the input argument list and
know whether that character should be quoted or left alone, so that the
resulting string can be passed to a shell to achieve the same semantics
as the command that was presumably typed by the user to produce the
argument list seen by the program.  (Remember, the program does not see
the original command typed by the user.  There is quote removal as well
as a slew of substitutions, all of which are done before the program sees
the input.)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]