views:

606

answers:

4

How can I set Linux environment variables remotely from a Windows application?

+2  A: 

You can't directly. Environment variables are just a table of values associated with the process, and inhereted when you fork(2) a process.

If you want to set them, you need some kind of code at the Linux side doing it. The simplest is probably to run your remote programs through a shell, and set them on the command line.

Otherwise, you need a way to get your Linux-side client to put them into the environment with setenv(3).

Charlie Martin
+1  A: 

Taking your rather terse question at face-value, you could have your Windows application ftp to the linux system as the user who is going to run the linux app and modify their .cshrc or .bashrc, adding the desired environment variables to the shell script. This won't modify already-running shells, though.

At an abstract level, you're talking about inter-process communication. You have information in the Windows application you want to communicate to communicate to some Linux application. I'm not sure environment variables are the best way to communicate this data. Perhaps opening a socket between your Windows app and Linux app would be best.

veefu
A: 

Two ways I can think of - in probable order of simplicity:

  1. Write a Linux program that listens on a network socket for commands. Your Windows app sends the appropriate command. The Linux program runs the command to set the environment variable - something like system(export MY_ENV_VAR=abc). This won't be secure over the network unless you make it so.

  2. Use SSH to login to the Linux system, then run a script to set the variable. I don't know if there are SSH libraries around for Windows but I'd be surprised if there aren't. PuTTY is an excellent open-source Windows SSH client so you may be able to use code from there but you should check the license first - depending on how you plan to use your application. This option has the benefit that you can use PKI to secure the network connection, if necessary.

1.- Exporting the variable won't set it in other already running or yet to be started shells. It will just set it in the current running process (or forked processes from that point on). But the process is running the network code. Probably not what the questioner wanted.
David Rodríguez - dribeas
2.- Shares the same problem as 1. In both cases you can edit system or user configurations to set the variable for yet-to-be-started shells
David Rodríguez - dribeas
A: 

i am using redhat linux 9 and installed ns-alllinone-2.1b6 ...it is installed suscessfully , now i m installing OWns simulator in it and having a problem in setting enviroment variables in it or(Syncronize)...them can anyone help me ...

uamir