in reply to Re: Strange SSH Behavior via system or exec
in thread Strange SSH Behavior via system or exec

++choroba

That works! Thanks.

However, shouldn't both work, or am I missing something?

-Craig

  • Comment on Re^2: Strange SSH Behavior via system or exec

Replies are listed 'Best First'.
Re^3: Strange SSH Behavior via system or exec
by salva (Canon) on Sep 18, 2014 at 15:26 UTC
    perl -e 'system("ssh", "-lusr", "remotehost", "date")'
    works but...
    perl -e 'system("ssh", "-l usr", "remotehost", "date")'
    doesn't because it tries to log as user " usr"!
      If this were the case, wouldn't the second case fail as well?
      $ perl -e 'system("ssh -l usr remotehost date")’ # works

        As soon as you give system more than one arg, it does not invoke the shell (see also exec), meaning that the arguments such as "-l usr" are passed on to ssh without interpretation by the shell, usually causing the command to interpret them literally.

        In your example, there is only one argument to system, so whitespace is interpreted as it would normally be by the shell (more precisely, if there are shell metacharacters, the command is passed to the shell, if there aren't, the command is split on whitespace and executed directly).

Re^3: Strange SSH Behavior via system or exec
by Anonymous Monk on Sep 18, 2014 at 16:57 UTC

    Either you give system a single argument, which is the equivalent of giving that string to the shell to be executed (e.g. /bin/sh -c 'foo bar quz'), or you give system multiple arguments, in which case you are giving that command its argument list directly, without interpretation by the shell. One of the things the shell does is split arguments on whitespace (except Windows), which no longer happens when you give it "-l usr" as a single string. (While some commands may do their own whitespace splitting of their argv, I'd assume that's rare. Except on Windows, hence the need for something like Win32::ShellQuote.)