Remotely exporting envrionment variables using scripts

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.

Remotely exporting envrionment variables using scripts

Postby wnadeem » Sun Aug 01, 2004 8:39 pm

Hey! (first post..bear with me!)

GOt a problem with a remote "trusted" machine that i am connect to using rlogin/rsh etc. What i am trying to do is that on login I want to set/modify some environment variables of the remote machine pertaining to that specific user. Unfortunately, the remote access i am getting/creating is via scripts(perl / BASH ) and logging into the remote machine is just one of the tasks these scripts are doing.

Simply speaking, some way through the run of the scripts, they have to start a process remotely on that "trusted" machine. And before doing that they need to set the environment variables. Currently I am running a perl script and i am doing a system function call that does an rsh to the remote machine as :

system("rsh <ipaddress> -l <user> 'export ID=1'");

That aint working.. I tried changin the quotes from single to double, nothing! then i ran a ls -l and it did display the home directory structure of the <user>! so basically..it is in there..but doesnt set the ID to 1!?

the scripts written are to automate the process of remotely logging in, setting the variables, executing some stuff that needs that variable set and then exiting back out! no human interferance there! so...thats the issue...
needless to say..but to add..I had the same problem using "source" command..ofcourse, if there is a way to solve this...it will apply to them of both (export and source)!

I hope i didnt loose u guys somewhere..but as i said...first post..can't be nice and short!! need a little intro to get into the game u know! :wink:

cheers
WhO SwItCheD ThE LiGhTs OFf????
wnadeem
Cadet
 
Posts: 4
Joined: Sat Jul 31, 2004 11:14 am
WLM: frivolous_79er@hotmail.com
Yahoo Messenger: wasiqnadeemk@yahoo.com
Location: Germany

Postby kernel-trap » Sun Aug 01, 2004 10:52 pm

what if u put the all variable in .bash_profile which is user home directory
they will exported automaticly everytime user login.

Regards
kernel-trap
Havaldaar
 
Posts: 105
Joined: Thu Dec 25, 2003 2:31 pm

Postby Kdaemon » Sun Aug 01, 2004 11:32 pm

why don't you make a socket program
a server side listening
client send the environment variable name the value to set, you server program reads from socket and exports it
it can be on tcp/udp

kernel-trap: I think he is talking about remotely setting a evironment variable for user
reg linux user #298274
Kdaemon
Naib Subedar
 
Posts: 346
Joined: Sat Nov 30, 2002 12:22 pm
Location: Islamabad. GPS: LHR

Postby wnadeem » Mon Aug 02, 2004 9:12 pm

actually, the thing is that I am doing this through a system command..so basically i am creating a subshell within the main executing script which ofcourse is itself running in a shell...so when i throw the system function call, it launches a separate child which perhaps might even do what it is told to, but on its exit..it exits that specific child "terminal", leaving behind nothing changed for the parent/script!

i hope i have made this sound into something that makes a little sense..but that is how the issue takes a turn towards being very thought provoking. As far as using sockets is concerned..perhaps it could be an answer but it seems like a way of swating a fly with a bazooka!
:wink:

still...there is a hint here that I am working on now a days and thats using the %ENV variable present with Perl that can access environment variables. Though its feasibility with remote environments still has to be verified!

cheers and keep thinkin.... :roll:
WhO SwItCheD ThE LiGhTs OFf????
wnadeem
Cadet
 
Posts: 4
Joined: Sat Jul 31, 2004 11:14 am
WLM: frivolous_79er@hotmail.com
Yahoo Messenger: wasiqnadeemk@yahoo.com
Location: Germany

Re: Remotely exporting envrionment variables using scripts

Postby lambda » Wed Aug 04, 2004 2:56 am

wnadeem wrote:system("rsh <ipaddress> -l <user> 'export ID=1'");


do this instead:

Code: Select all

system("rsh remote-host -l user /usr/bin/env ID=1 FOO=bar BLAH=foo /some/command");


That aint working.. I tried changin the quotes from single to double, nothing! then i ran a ls -l and it did display the home directory structure of the <user>! so basically..it is in there..but doesnt set the ID to 1!?


ls doesn't care what ID is set to.
lambda
Major General
 
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Website: http://www.hungry.com/~fn/
Location: Lahore

Postby lambda » Wed Aug 04, 2004 2:58 am

Kdaemon wrote:why don't you make a socket program
a server side listening
client send the environment variable name the value to set, you server program reads from socket and exports it
it can be on tcp/udp

kernel-trap: I think he is talking about remotely setting a evironment variable for user


this gets my vote for the dumbest hack ever posted on this site.
lambda
Major General
 
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Website: http://www.hungry.com/~fn/
Location: Lahore

Postby Kdaemon » Wed Aug 04, 2004 12:15 pm

lambda wrote:this gets my vote for the dumbest hack ever posted on this site.

it may be, but depends how one has interpreted :idea:
reg linux user #298274
Kdaemon
Naib Subedar
 
Posts: 346
Joined: Sat Nov 30, 2002 12:22 pm
Location: Islamabad. GPS: LHR

Postby soni » Wed Aug 04, 2004 2:49 pm

so when i throw the system function call, it launches a separate child which perhaps might even do what it is told to, but on its exit..it exits that specific child "terminal", leaving behind nothing changed for the parent/script!


Its normal behaviour of the child; it can inherit the environment of the parent but it cant change it; the chages made to the evironment by the child are local, as soon as it dies changes go out of the memory too.

Take a look at the following snippets:

script file named sccc

Code: Select all


#!/usr/bin/env bash

echo $id
export linus="fin"
env



At the command prompt do the following:

env id=linux ./sccc

Notice that the enviroment variables $id and $linus are the part of the child's(the script file sccc) environment....

So $id and $linus are the part of the parents environment?
The answer is No, check out by uing env and echo.

In my humble opinion no matter how you try to solve this problem(by implementing a listening socket, by perls system function or by manipulating the ENV hash) the parents environment stays intact.

Regards:
soni.
soni
Naik
 
Posts: 70
Joined: Sat Oct 04, 2003 1:44 pm
Website: http://www.cyberian.pk
Location: Karachi

The end to all miseries!!

Postby wnadeem » Wed Aug 04, 2004 3:43 pm

Ok...here is a solution to the problem i had posted, it came to me in the middle of the night...so it might be a little screwy...i tried it and so far it works!! anyhow..its like this

system("rsh -l <user> <ipaddress> 'export myid=1 ; <run program here needing that EnvVar(myid) set>');

and it does the job....

second way is to add within the remotely executing program to export the EnvVar. This way when ever the program runs, it would automatically do the export on the remote side... 8)

lastly, to close of the topic and just a piece of info....rsh does not return any error codes or reflex answer to a abnormal termination..so for the above task I used ssh..rather than rsh...just thought i might add this for those doing stuff like this..

Cheers you all, and till we meet again... :D
WhO SwItCheD ThE LiGhTs OFf????
wnadeem
Cadet
 
Posts: 4
Joined: Sat Jul 31, 2004 11:14 am
WLM: frivolous_79er@hotmail.com
Yahoo Messenger: wasiqnadeemk@yahoo.com
Location: Germany

Re: The end to all miseries!!

Postby lambda » Wed Aug 04, 2004 5:01 pm

wnadeem wrote:system("rsh -l <user> <ipaddress> 'export myid=1 ; <run program here needing that EnvVar(myid) set>');


that fails when the remote user doesn't use sh/ksh/bash as his/her shell. env is portable and exists to solve the problem you want to solve.

rsh does not return any error codes or reflex answer to a abnormal termination


that's incorrect.

Code: Select all

[me@sailfish /]$ rsh catfish pwd
/home/me
[me@sailfish /]$ echo $?
0
[me@sailfish /]$ rsh -l unknown catfish pwd
Permission denied.
[me@sailfish /]$ echo $?
1
[me@sailfish /]$
lambda
Major General
 
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Website: http://www.hungry.com/~fn/
Location: Lahore

Postby wnadeem » Wed Aug 04, 2004 8:57 pm

Hey...great...Thanks for the correction...now i dont know where i read this from...but yea..ur right...in both cases...

:D
WhO SwItCheD ThE LiGhTs OFf????
wnadeem
Cadet
 
Posts: 4
Joined: Sat Jul 31, 2004 11:14 am
WLM: frivolous_79er@hotmail.com
Yahoo Messenger: wasiqnadeemk@yahoo.com
Location: Germany

Postby stalactite » Tue Nov 01, 2005 12:06 pm

stalactite
Cadet
 
Posts: 4
Joined: Fri Jul 22, 2005 4:41 pm


Return to “%s” Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron