ssh runs remote commands as what remote user?? - SSH

This is a discussion on ssh runs remote commands as what remote user?? - SSH ; Anomalous (??) behavior of: ssh student20@192.168.3.12 "(echo `whoami`; sleep 20 ; echo `whoami`)" As expected, "sleep 20" runs as the remote machine user's student20 (per "ps" executed at the remote machine during the 20-second interval) Unexpectedly however, when the whoami ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: ssh runs remote commands as what remote user??

  1. ssh runs remote commands as what remote user??


    Anomalous (??) behavior of:

    ssh student20@192.168.3.12 "(echo `whoami`; sleep 20 ; echo `whoami`)"


    As expected, "sleep 20" runs as the remote machine user's student20
    (per "ps" executed at the remote machine during the 20-second
    interval)

    Unexpectedly however, when the whoami commands run on the remote
    machine, the user name they return is that of the user at the local
    machine who initiated the ssh command. (A user who by name doesn't
    even exist on the remote.)

    I expected whoami to return "student20"

    What am I missing?

  2. Re: ssh runs remote commands as what remote user??

    taihaiteki@dslextreme.com wrote in
    news:trk9p1h68figd49ljkjdqp1chhfoq00flp@4ax.com:
    > ssh student20@192.168.3.12 "(echo `whoami`; sleep 20 ; echo `whoami`)"
    >
    > Unexpectedly however, when the whoami commands run on the remote
    > machine, the user name they return is that of the user at the local
    > machine who initiated the ssh command. (A user who by name doesn't
    > even exist on the remote.)


    The whoami commands don't run on the remote machine -- your (local)
    shell sees the backquote chars and runs whoami, substituting the result.
    If you want whoami to run on the rmove machine, you need to escape the
    backquote chars, or put the command sin single quotes instead of double
    quotes, which has the same effect. Altrnately, just do:

    ssh student20@192.168.3.12 "(whoami; sleep 20 ; whoami)"

    Chris Dodd
    cdod@acm.org

  3. Re: ssh runs remote commands as what remote user??

    wrote:
    >ssh student20@192.168.3.12 "(echo `whoami`; sleep 20 ; echo `whoami`)"
    >Unexpectedly however, when the whoami commands run on the remote
    >machine, the user name they return is that of the user at the local
    >machine who initiated the ssh command. (A user who by name doesn't
    >even exist on the remote.)


    Nope. The whoami commands are NOT running on the remote machine. Your
    backticks are causing the local shell to expand the commands before executing
    ssh. Try the following and see what you get:

    ssh student20@192.168.3.12 "(hostname ; echo `hostname`)"

    >I expected whoami to return "student20"


    Any of these should do the trick. Either don't use backticks (which the
    current shell evaluates before running the ssh command), or protect the
    backticks from your shell by escaping them or putting them in single quotes

    ssh student20@192.168.3.12 "(whoami ; sleep 20 ; whoami)"
    ssh student20@192.168.3.12 '(echo `whoami` ; sleep 20 ; echo `whoami`)'
    ssh student20@192.168.3.12 "(echo \`whoami\` ; sleep 20 ; echo \`whoami\`)"

    In the last two, your local shell will eat the quotes or backslashes, and pass
    the backticks to the remote shell, which can then execute the command.
    --
    Mark Rafn dagon@dagon.net


  4. Re: ssh runs remote commands as what remote user??

    > ssh student20@192.168.3.12 "(whoami ; sleep 20 ; whoami)"
    > ssh student20@192.168.3.12 '(echo `whoami` ; sleep 20 ; echo `whoami`)'
    > ssh student20@192.168.3.12 "(echo \`whoami\` ; sleep 20 ; echo \`whoami\`)"
    >
    > In the last two, your local shell will eat the quotes or backslashes, and pass
    > the backticks to the remote shell, which can then execute the command.
    > --
    > Mark


    Putting it another way,
    Double quotes and meta characters are consumed and intrepreted locally
    single quotes are stripped and the internal string executed remotely

+ Reply to Thread