0

Below is a small bash script. The expected output after connecting to the server is to print Hello World and in the next line the current month and year - like Jan 2014. For using VARIABLE1 in the 'here document', I need dollar expansion, so the terminating character ~ is not quoted.

VARIABLE1="World"
ssh username@server.domain.com <<~
        echo "Hello $VARIABLE1"
        COMMAND1=`date +%b`
        COMMAND2=$(date +%Y)
        echo "$COMMAND1 $COMMAND2"
~

The actual output that I get is this :

Pseudo-terminal will not be allocated because stdin is not a terminal.
Password:
Hello World

When ssh is run in verbose mode, here is last part of the output:

debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Trying private key: *****
debug1: Trying private key: *****
debug1: Next authentication method: keyboard-interactive
Password:
debug1: Authentication succeeded (keyboard-interactive).
debug1: Final hpn_buffer_size = *****
debug1: HPN Disabled: 0, HPN Buffer Size: *****
debug1: channel 0: new [client-session]
debug1: Enabled Dynamic Window Scaling

debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
Hello World

debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
Transferred: sent 1552, received 2360 bytes, in 0.1 seconds
Bytes per second: sent 17253.2, received 26235.6
debug1: Exit status 0

Could somebody point out the bug here? Why doesn't it print Jan 2014?

Shivanand
  • 144
  • 1
  • 1
  • 13

1 Answers1

2

There is some information here about how to force a tty, however, your script should work if you escape your characters properly:

VARIABLE1="World"
ssh username@server.domain.com <<EOF
        echo Hello $VARIABLE1
        COMMAND1=\`date +%b\`
        COMMAND2=\$(date +%Y)
        echo "\$COMMAND1 \$COMMAND2"
EOF

If you want to force the tty:

ssh -t -t user@domain.com <<EOF
        echo Hello $VARIABLE1
        COMMAND1=\`date +%b\`
        COMMAND2=\$(date +%Y)
        echo "\$COMMAND1 \$COMMAND2"
        exit
EOF

Alternatively, you could just send the script as an argument to ssh:

ssh user@domain.com "echo Hello $VARIABLE1;COMMAND1=\`date +%b\`; COMMAND2=\$(date +%Y); echo \$COMMAND1 \$COMMAND2"

EDIT: If you want to disable the message about the Pseudo-terminal allocation use "-T":

VARIABLE1="World"
ssh -T username@server.domain.com <<EOF
        echo Hello $VARIABLE1
        COMMAND1=\`date +%b\`
        COMMAND2=\$(date +%Y)
        echo "\$COMMAND1 \$COMMAND2"
EOF
Community
  • 1
  • 1
arco444
  • 22,002
  • 12
  • 63
  • 67
  • Thanks @arco444! Escaping the backticks and the dollar-sign makes it work. – Shivanand Jan 03 '14 at 12:01
  • Could you please explain why the backticks need to be escaped? – Shivanand Jan 03 '14 at 12:03
  • 1
    Sure, your local terminal interprets them first. This is why your "Hello World" works, because you set VARIABLE1 outside the ssh script and you simply ask the remote server to echo "Hello World". It is your local terminal that evaluates VARIABLE1 and sends the evaluated string. In order to make the remote server evaluate a variable, you need to prevent your terminal from reading it. Try and send "Hello \$VARIABLE1" instead and see what happens. – arco444 Jan 03 '14 at 12:07