How do ssh remote command line arguments get parsed

onlynone

I've seen the questions and answers about needing to double-escape the arguments to remote ssh commands. My question is: Exactly where and when does the second parsing get done?

If I run the following:

$ ssh otherhost pstree -a -p

I see the following in the output:

  |-sshd,3736
  |   `-sshd,1102
  |       `-sshd,1109
  |           `-pstree,1112 -a -p

The parent process for the remote command (pstree) is sshd, there doesn't appear to be any shell there that would be parsing the command line arguments to the remote command, so it doesn't seem as if double quoting or escaping would be necessary (but it definitely is). If instead I ssh there first and get a login shell, and then run pstree -a -p I see the following in the output:

  ├─sshd,3736
  │   └─sshd,3733
  │       └─sshd,3735
  │           └─bash,3737
  │               └─pstree,4130 -a -p

So clearly there's a bash shell there that would do command line parsing in that case. But the case where I use a remote command directly, there doesn't seem to be a shell, so why is double quoting necessary?

Gilles 'SO- stop being evil'

There is always a remote shell. In the SSH protocol, the client sends the server a string to execute. The SSH command line client takes its command line arguments and concatenates them with a space between the arguments. The server takes that string, runs the user's login shell and passes it that string. (More precisely: the server runs the program that is registered as the user's shell in the user database, passing it two command line arguments: -c and the string sent by the client. The shell is not invoked as a login shell: the server does not set the zeroth argument to a string beginning with -.)

It is impossible to bypass the remote shell. The protocol doesn't have anything like sending an array of strings that could be parsed as an argv array on the server. And the SSH server will not bypass the remote shell because that could be a security restriction: using a restricted program as the user's shell is a way to provide a restricted account that is only allowed to run certain commands (e.g. an rsync-only account or a git-only account).

You may not see the shell in pstree because it may be already gone. Many shells have an optimization where if they detect that they are about to do “run this external command, wait for it to complete, and exit with the command's status”, then the shell runs “execve of this external command” instead. This is what's happening in your first example. Contrast the following three commands:

ssh otherhost pstree -a -p
ssh otherhost 'pstree -a -p'
ssh otherhost 'pstree -a -p; true'

The first two are identical: the client sends exactly the same data to the server. The third one sends a shell command which defeats the shell's exec optimization.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to execute a remote command over ssh with arguments?

From Dev

How do I get raw VBScript command line arguments?

From Dev

How to escape quotes for remote command via ssh command line

From Dev

How to escape quotes for remote command via ssh command line

From Dev

How to get command line arguments in Ceylon?

From Dev

How to get command Line arguments in spring boot?

From Dev

Docker, how to remote ssh from command line host to a docker container?

From Dev

running remote command with arguments and shell using ssh

From Dev

command line arguments given to a C# program are parsed wrong

From Dev

How do terminal size changes get sent to command line applications though ssh or telnet?

From Dev

Using PuTTY SSH, how do I capture the remote output of a command?

From Dev

Get command line arguments as string

From Java

How do I parse command line arguments in Java?

From Java

How do I parse command line arguments in Bash?

From Java

How do you access command line arguments in Swift?

From Dev

How do I read arguments from the command line with an MPI program?

From Dev

How do I access command line arguments in SICStus Prolog?

From Dev

How do I ignore escape characters in command line arguments?

From Dev

How do I pass command line arguments to stack exec

From Dev

How do I parse command line arguments in Dart?

From Dev

How do I find out command line arguments of a running program?

From Dev

How do I check the second element of the command line arguments?

From Dev

How to get command-line arguments using a text file?

From Dev

How to get an HTA application to accept command line arguments?

From Dev

How to get command line arguments of unknown length in Fortran?

From Dev

How to get Command Line Arguments running in C for input and output files

From Dev

How to get the launch arguments while making a command line tool with swift

From Dev

How to get the command line arguments running java in IntelliJ IDEA 14.1

From Dev

How to pass multiple arguments in a command to execute an exe on remote linux machine using C/C++ and SSH

Related Related

  1. 1

    How to execute a remote command over ssh with arguments?

  2. 2

    How do I get raw VBScript command line arguments?

  3. 3

    How to escape quotes for remote command via ssh command line

  4. 4

    How to escape quotes for remote command via ssh command line

  5. 5

    How to get command line arguments in Ceylon?

  6. 6

    How to get command Line arguments in spring boot?

  7. 7

    Docker, how to remote ssh from command line host to a docker container?

  8. 8

    running remote command with arguments and shell using ssh

  9. 9

    command line arguments given to a C# program are parsed wrong

  10. 10

    How do terminal size changes get sent to command line applications though ssh or telnet?

  11. 11

    Using PuTTY SSH, how do I capture the remote output of a command?

  12. 12

    Get command line arguments as string

  13. 13

    How do I parse command line arguments in Java?

  14. 14

    How do I parse command line arguments in Bash?

  15. 15

    How do you access command line arguments in Swift?

  16. 16

    How do I read arguments from the command line with an MPI program?

  17. 17

    How do I access command line arguments in SICStus Prolog?

  18. 18

    How do I ignore escape characters in command line arguments?

  19. 19

    How do I pass command line arguments to stack exec

  20. 20

    How do I parse command line arguments in Dart?

  21. 21

    How do I find out command line arguments of a running program?

  22. 22

    How do I check the second element of the command line arguments?

  23. 23

    How to get command-line arguments using a text file?

  24. 24

    How to get an HTA application to accept command line arguments?

  25. 25

    How to get command line arguments of unknown length in Fortran?

  26. 26

    How to get Command Line Arguments running in C for input and output files

  27. 27

    How to get the launch arguments while making a command line tool with swift

  28. 28

    How to get the command line arguments running java in IntelliJ IDEA 14.1

  29. 29

    How to pass multiple arguments in a command to execute an exe on remote linux machine using C/C++ and SSH

HotTag

Archive