1

I'm trying to execute commands on a remote machine via ssh, and I need the script to wait until the ssh password is provided (if necessary). This my code snippet:

ssh -T ${remote} << EOF
if [ ! -d $HOME/.ssh ]; then
    mkdir $HOME/.ssh
    touch $HOME/.ssh/authorized_keys
    chmod 0600 $HOME/.ssh/authorized_keys
fi;
EOF

The problem is, commands between EOFs start executing on the local machine without waiting for the pass to be provided. Is there any way to wait for the pass before continuing with the script?

1
  • 2
    Please when you put code, paste the code, not some fantasy approximation of it. Those backticks can't be there in your code. Commented Jul 4, 2013 at 20:33

1 Answer 1

3

This that simple as :

ssh -T ${remote} << 'EOF'
if [ ! -d $HOME/.ssh ]; then
        `mkdir $HOME/.ssh`
        `touch $HOME/.ssh/authorized_keys`
        `chmod 0600 $HOME/.ssh/authorized_keys`
else
EOF

Note the ' single quotes around EOF.

But I recommend you to use $( ) form instead of backticks : the backquote (`)

is used in the old-style command substitution, e.g.

foo=`command`

The

foo=$(command)

syntax is recommended instead. Backslash handling inside $() is less surprising, and $() is easier to nest. See http://mywiki.wooledge.org/BashFAQ/082


If you need to pass variables :

var=42
ssh -T ${remote} << EOF
if [ ! -d \$HOME/.ssh ]; then
        \`mkdir \$HOME/.ssh\`
        \`touch \$HOME/.ssh/authorized_keys\`
        \`chmod 0600 \$HOME/.ssh/authorized_keys\`
else
    echo $var
EOF
Sign up to request clarification or add additional context in comments.

2 Comments

What's not that easy, is to find the good keywords to find this by yourself on a search engine if you dunno.
Do you by any chance know how to pass variables to the part between EOF? The ones I set before are all resolved as empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.