variables inside an ssh session


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variables inside an ssh session
# 1  
Old 11-24-2010
Data variables inside an ssh session

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:

Code:
#:/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:

Code:
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:
Code:
 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:

Code:
#:/bin/sh

test="salut"

echo $test

ssh hudson@10.41.21.99 <<'EOF'

testssh="salutssh"

echo testssh=$testssh
echo test=$test

EOF

I have
Code:
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?
# 2  
Old 11-24-2010
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...
# 3  
Old 11-24-2010
Hi.

A quote from the (ksh) man page:
Code:

 <<[-]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:
Code:
#:/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:
Code:
#:/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:
Code:
#:/bin/sh

test="salut"

echo $test

ssh hudson@10.41.21.99 <<EOF

testssh="salutssh"

echo testssh=\$testssh
echo test=$test

EOF

# 4  
Old 11-24-2010
Thanks scottn, Ive been fooled by the code message:
Quote:
salut
hudson@10.41.21.99's password:
# 5  
Old 11-24-2010
MySQL

Hello scottn,

yes indeed, it works.

thanks a lot.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to exit ssh session

Hi , I am running a script on multiple servers.When I run that script on one server,I want to exit from that server and want to go to next server.PFB script: Suppose there are 3 servers:server1,server2,server3.I am currently in server1. for h in {2..3} do ssh username@server$h <<EOF ... (4 Replies)
Discussion started by: sonu141
4 Replies

2. Shell Programming and Scripting

SSH session in perl.

Is it possible to ssh to a server in perl and execute multiple commands without doing them all at once? I know I can potentially do something like system( 'ssh user@server "cd directory; grep blah *; curl someurl.com"'); However, I'd like the functionality where I don't have to keep SSHing... (3 Replies)
Discussion started by: mrwatkin
3 Replies

3. UNIX for Dummies Questions & Answers

SSH session

I ran a job using SSH and then used screen command followed by space bar. Then I ran the job. But after some hours, the internet connection was lost.This had happened to me before. I used to run the whole job again. Today I came across the fact that I could restart the jobs without having to do it... (2 Replies)
Discussion started by: nellierd
2 Replies

4. Shell Programming and Scripting

SSH session saving

Hi, I want to execute some script on the remote session and transfer the processed file on remote machine to local machine. I tried with ssh-keygen and ssh-copy-id but seems that this has been disable on the remote machines, Is there any possiblity that I can save the remote username and... (7 Replies)
Discussion started by: maruthavanan
7 Replies

5. HP-UX

ssh session getting hung (smilar to hpux telnet session is getting hung after about 15 minutes)

Our network administrators implemented some sort of check to kill idle sessions and now burden is on us to run some sort of keep alive. Client based keep alive doesn't do a very good job. I have same issue with ssh. Does solution 2 provided above apply for ssh sessions also? (1 Reply)
Discussion started by: yoda9691
1 Replies

6. HP-UX

SSh session hang

Hello, When I connect to a HP-UX 11.23 and/or 11.31 after a time without activity, the session hang. There is not TMOUT stablished, and the session NOT finish, always keep hang after about 20min of inactivity period. Anyone knows if this problem is related to HP-UX or network... (1 Reply)
Discussion started by: RuBiCK
1 Replies

7. Shell Programming and Scripting

could not send commands SSH session with Net::SSH::Expect

I am using Net::SSH::Expect to connect to the device(iLO) with SSH. After the $ssh->login() I'm able to view the prompt, but not able to send any coommands. With the putty I can connect to the device and execute the commands without any issues. Here is the sample script my $ssh =... (0 Replies)
Discussion started by: hansini
0 Replies

8. Solaris

mbx_bad_handle during ssh -X session

We have a Solaris 10 server that we're migrating an old Cadre Teamwork application to. We find that when users on Sun workstations use ssh -X <username>@<new server> and run Teamwork on this new server it is fine. When cygwin users and linux users run ssh -X to get to this new platform they... (0 Replies)
Discussion started by: pzxkys
0 Replies

9. Shell Programming and Scripting

looping a array inside inside ssh is not working, pls help

set -A arr a1 a2 a3 a4 # START ssh -xq $Server1 -l $Username /usr/bin/ksh <<-EOS integer j=0 for loop in ${arr} do printf "array - ${arr}\n" (( j = j + 1 )) j=`expr j+1` done EOS # END ========= this is not giving me correct output. I... (5 Replies)
Discussion started by: reldb
5 Replies

10. Shell Programming and Scripting

sqlplus session being able to see unix variables session within a script

Hi there. How do I make the DB connection see the parameter variables passed to the unix script ? The code snippet below isn't working properly. sqlplus << EOF user1@db1/pass1 BEGIN PACKAGE1.perform_updates($1,$2,$3); END; EOF Thanks in advance, Abrahao. (2 Replies)
Discussion started by: 435 Gavea
2 Replies
Login or Register to Ask a Question