The warning message Pseudo-terminal will not be allocated because stdin is not a terminal.
is due to the fact that no command is specified for ssh
while stdin is redirected from a here document.
Due to the lack of a specified command as an argument ssh
first expects an interactive login session (which would require the allocation of a pty on the remote host) but then has to realize that its local stdin is no tty/pty. Redirecting ssh
's stdin from a here document normally requires a command (such as /bin/sh
) to be specified as an argument to ssh
- and in such a case no pty will be allocated on the remote host by default.
Since there are no commands to be executed via ssh
that require the presence of a tty/pty (such as vim
or top
) the -t
switch to ssh
is superfluous.
Just use ssh -T user@server <<EOT ...
or ssh user@server /bin/bash <<EOT ...
and the warning will go away.
If <<EOF
is not escaped or single-quoted (i. e. <<\EOT
or <<'EOT'
) variables inside the here document will be expanded by the local shell before it is executing ssh ...
. The effect is that the variables inside the here document will remain empty because they are defined only in the remote shell.
So, if $REL_DIR
should be both accessible by the local shell and defined in the remote shell, $REL_DIR
has to be defined outside the here document before the ssh
command (version 1 below); or, if <<\EOT
or <<'EOT'
is used, the output of the ssh
command can be assigned to REL_DIR
if the only output of the ssh
command to stdout is genererated by echo "$REL_DIR"
inside the escaped/single-quoted here document (version 2 below).
A third option would be to store the here document in a variable and then pass this variable as a command argument to ssh -t user@server "$heredoc"
(version 3 below).
And, last but not least, it would be no bad idea to check if the directories on the remote host were created successfully (see: check if file exists on remote host with ssh).
# version 1
unset DEP_ROOT REL_DIR
DEP_ROOT='/tmp'
datestamp=$(date +%Y%m%d%H%M%S)
REL_DIR="${DEP_ROOT}/${datestamp}"
ssh localhost /bin/bash <<EOF
if [ ! -d "$DEP_ROOT" ] && [ ! -e "$DEP_ROOT" ]; then
echo "creating the root directory" 1>&2
mkdir "$DEP_ROOT"
fi
mkdir "$REL_DIR"
#echo "$REL_DIR"
exit
EOF
scp -r ./dir1 user@server:"$REL_DIR"
scp -r ./dir2 user@server:"$REL_DIR"
# version 2
REL_DIR="$(
ssh localhost /bin/bash <<\EOF
DEP_ROOT='/tmp'
datestamp=$(date +%Y%m%d%H%M%S)
REL_DIR="${DEP_ROOT}/${datestamp}"
if [ ! -d "$DEP_ROOT" ] && [ ! -e "$DEP_ROOT" ]; then
echo "creating the root directory" 1>&2
mkdir "$DEP_ROOT"
fi
mkdir "$REL_DIR"
echo "$REL_DIR"
exit
EOF
)"
scp -r ./dir1 user@server:"$REL_DIR"
scp -r ./dir2 user@server:"$REL_DIR"
# version 3
heredoc="$(cat <<'EOF'
# -onlcr: prevent the terminal from converting bare line feeds to carriage return/line feed pairs
stty -echo -onlcr
DEP_ROOT='/tmp'
datestamp="$(date +%Y%m%d%H%M%S)"
REL_DIR="${DEP_ROOT}/${datestamp}"
if [ ! -d "$DEP_ROOT" ] && [ ! -e "$DEP_ROOT" ]; then
echo "creating the root directory" 1>&2
mkdir "$DEP_ROOT"
fi
mkdir "$REL_DIR"
echo "$REL_DIR"
stty echo onlcr
exit
EOF
)"
REL_DIR="$(ssh -t localhost "$heredoc")"
scp -r ./dir1 user@server:"$REL_DIR"
scp -r ./dir2 user@server:"$REL_DIR"