How to export variables on remote machine through ssh.

View: New views
6 Messages — Rating Filter:   Alert me  

How to export variables on remote machine through ssh.

by gajendrasharma1 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Hello experts,

I need ur help on solving this problem.
I am writing a automation tool that runs several shell scripts on different remote hosts using ssh. These scripts need various pre-defined variables to run. I want to provide these variables to the script by exporting these variables after doing ssh and then run the script that use it, like this:

a=10
ssh -l $user $host "export myVar=$a; cd $myDir; ./myScript.sh"

this doesn't work, as probably the ssh shell might not understand the export command.

Can anybody please tell me how shall i solve this problem.

Thanks.
Gajendra

Re: How to export variables on remote machine through ssh.

by Joseph Spenner :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

--- gajendrasharma1 <gajendra.sharma@...> wrote:

>
> Hello experts,
>
> I need ur help on solving this problem.
> I want to provide these variables to the script
> by exporting these
> variables after doing ssh and then run the script
> that use it, like this:
>
> a=10
> ssh -l $user $host "export myVar=$a; cd $myDir;
> ./myScript.sh"
>

Can you modify your script to take arguments on the
command line?  You could then pass your variables on
the command line.  ie:

ssh -l $user $host $myDir/myScript.sh myVar

Then your script needs to contain something like

$myVar = $1




      ____________________________________________________________________________________
Be a better pen pal.
Text or chat with friends inside Yahoo! Mail. See how.  http://overview.mail.yahoo.com/

Re: How to export variables on remote machine through ssh.

by william estrada :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Try this:

    ssh -l $user $host 'export myVar='$a'; cd '$myDir'; ./myScript.sh'


William Estrada
MrUmunhum@...
Mt-Umunhum-Wireless.net ( http://Mt-Umunhum-Wireless.net )
Ymessenger: MrUmunhum



gajendrasharma1 wrote:

> Hello experts,
>
> I need ur help on solving this problem.
> I am writing a automation tool that runs several shell scripts on different
> remote hosts using ssh. These scripts need various pre-defined variables to
> run. I want to provide these variables to the script by exporting these
> variables after doing ssh and then run the script that use it, like this:
>
> a=10
> ssh -l $user $host "export myVar=$a; cd $myDir; ./myScript.sh"
>
> this doesn't work, as probably the ssh shell might not understand the export
> command.
>
> Can anybody please tell me how shall i solve this problem.
>
> Thanks.
> Gajendra

Re: How to export variables on remote machine through ssh.

by Greg Wooledge :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

On Tue, Nov 27, 2007 at 06:53:36AM -0800, gajendrasharma1 wrote:
> a=10
> ssh -l $user $host "export myVar=$a; cd $myDir; ./myScript.sh"

myDir isn't defined...?

> this doesn't work, as probably the ssh shell might not understand the export
> command.

That depends on what shell $user has.  If $user's shell is bash or ksh,
the syntax you have here looks OK (assuming myDir is defined somewhere
on the ssh CLIENT system, and you simply neglected to show us).

If $user's shell is /bin/sh then "export foo=bar" is not necessarily
going to work; it depends on which /bin/sh the system has.  You're safer
with "foo=bar; export foo".

If $user's shell is csh or tcsh, then you need entirely different syntax.
(And $user needs a swift kick, but that's a different story....)
Something like "setenv myVar $a" should do.  Note the lack of an "=" sign.

Re: How to export variables on remote machine through ssh.

by Jan Pechanec-2 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

On Tue, 27 Nov 2007, gajendrasharma1 wrote:

>a=10
>ssh -l $user $host "export myVar=$a; cd $myDir; ./myScript.sh"
>
>this doesn't work, as probably the ssh shell might not understand the export
>command.

        hi, this should definitely work since ssh knows nothing about that,
it's just about the remote shell that is run with -c "<the code>"; if you
expand the variable locally (which you did in the example above) it's ok.

        however, you can also use SendEnv/AcceptEnv with OpenSSH.

        this just works for me:

$ cat test.sh
#!/bin/sh

echo $a

$ ssh localhost "export a=xx; ./test.sh"
xx

        the same with 'ssh localhost "a=xx ./test.sh"'

        J.

--
Jan Pechanec

Re: How to export variables on remote machine through ssh.

by gajendrasharma1 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Hello greg,

Your solution works. Thanks a lot.
Thanks all for your replying and for your nice suggestions.
Thanks a lot.

Best regards.
Gajendra