Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing shell variables to ssh

I read the other postings on passing variables in remote ssh, but those solutions don't seem to work for me.

Here is my situation: I'm populating the values of my variables throughout my shell script (on a local machine), which are then used on another system via ssh. I understand why this isn't working, but I'd like to find a way to use values in a local shell on another system via ssh.

BUILD_SERVER="my_server"
ANOTHER_SERVER="my_server2"
ssh $USERNAME@$BUILD_SERVER << 'ENDSSH'

echo "$BUILD_SERVER"
echo "$ANOTHER_SERVER"

ENDSSH

When I run this code, these variables are null. Is there any way to pass through the values to ssh?

like image 629
hax0r_n_code Avatar asked Apr 05 '13 16:04

hax0r_n_code


1 Answers

I figured it out. On the line where I was doing the following:

ssh $USERNAME@$BUILD_SERVER << 'ENDSSH'

I removed the single quotes and it now interprets allow of my local variables. If I want to use a variable on the remote server I just escape them with a "\"

ssh $USERNAME@$BUILD_SERVER << ENDSSH

echo $BUILD_SERVER     # echos the local server variable
LOCAL_SERVER="local_server"

echo \$LOCAL_SERVER    # echos the remote server variable

ENDSSH
like image 72
hax0r_n_code Avatar answered Sep 23 '22 13:09

hax0r_n_code