Lotfus
November 24, 2010, 9:19am
1
Hello all,
I would like to declare and use variables inside an ssh session. I have the feeling that it's not possible. Here is the situtation simpified:
#:/bin/sh
test="salut"
echo $test
ssh hudson@10.41.21.99 <<EOF
export testssh="salut"
echo testssh=$testssh
EOF
and this is my output:
salut
hudson@10.41.21.99's password:
testssh=
any ideas please?
---------- Post updated at 01:44 PM ---------- Previous update was at 01:19 PM ----------
I just found the solution:
we have to put single quotes around the EOF:
ssh hudson@10.41.21.99 <<'EOF'
export testssh="salut"
echo testssh=$testssh
EOF
..... !!!
---------- Post updated at 03:19 PM ---------- Previous update was at 01:44 PM ----------
now I have a new problem: the variables declared in the parent bash are no longer visible inside an ssh session...
so when I execute:
#:/bin/sh
test="salut"
echo $test
ssh hudson@10.41.21.99 <<'EOF'
testssh="salutssh"
echo testssh=$testssh
echo test=$test
EOF
I have
salut
hudson@10.41.21.99's password:
testssh=salutssh
test=
any help please!? what are finally the rules for using variables in an ssh session?
vbe
November 24, 2010, 9:47am
2
I suppose it explains why some people use the expect utility...
You could try to use File Descriptors, search this forum, Perderabo has posted some goodies on the subject some time ago...
Scott
November 24, 2010, 10:11am
3
Hi.
A quote from the (ksh) man page:
<<[-]word
...The resulting document, called a here-
document, becomes the standard input. If any character
of word is quoted, then no interpretation is placed upon
the characters of the document; otherwise, parameter
expansion, command substitution, and arithmetic substitu-
tion occur, \new-line is ignored, and \ must be used to
quote the characters \, $, `.
With this:
#:/bin/sh
test="salut"
echo $test
ssh hudson@10.41.21.99 <<EOF
export testssh="salut"
echo testssh=$testssh
EOF
the variable testssh is evaluated before it get's to the remote server.
See the output of:
#:/bin/sh
test="salut"
export testssh="salut_loacl"
ssh hudson@10.41.21.99 <<EOF
echo testssh=$testssh
EOF
You can escape the variables you want evaluated on the target server:
#:/bin/sh
test="salut"
echo $test
ssh hudson@10.41.21.99 <<EOF
testssh="salutssh"
echo testssh=\$testssh
echo test=$test
EOF
vbe
November 24, 2010, 10:28am
4
Thanks scottn, Ive been fooled by the code message: