Setting environment variables remotely? - linux

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

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).

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.

Two ways I can think of - in probable order of simplicity:
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.
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.

Related

Standalone program for accessing and developing in Linux in web browser

Is there any software working like this?
Runs as a standalone program. No install is needed. Thus, can be used as an Ansible module.
After running the program in a remote Linux machine, I can open up a web browser, then open a web page provided by the program. The program provides features similar to file explorer, IDE-level code editor, debugger, etc. In terms of debugger, there is already similar one; gdbgui.
There is another way such as Gnome, KDE or X11. However, these requires much packages to be installed. I don't want they be installed, because my Linux machines are kept to be small and secure.
You might consider having some terminal emulator running inside a browser. Such things exist, e.g. libonion has oterm as an example application. Then you can do all the things that a command line interface thru a unix shell provides (of course, you won't be able to run GUI applications, e.g. X11 clients such as GTK or Qt applications).
You could also consider some webmin like stuff.
Notice that you don't need to have a desktop environment on a remote Linux machine. Most of them (e.g. internet servers) have only command line interface.
Learn more about X11: you could have an X11 server on your laptop (e.g. under Windows if so needed) and run remotely X11 clients (that is GUI applications) with ssh -X on your remote Linux system.
However, these requires much packages to be installed. I don't want they be installed, because my Linux machines are kept to be small and secure.
I don't understand that requirement. On my VPS, running in some OVH datacenter, I do have X11 client applications (notably emacs). I don't believe that lowers the security of my system, and the disk space consumption for X11 applications and libraries is small enough these days. And of course I use standard commands (like cp(1), mv(1), rm(1), grep(1), find(1), less(1), file(1), sed(1) ....) to manage files. Any graphical file manager is useless (and I never use them, while using Unix since 1986)
You really should learn how to use the command line on Linux. It is incredibly powerful.

Remote commands execution and file acces in Linux and Windows

Suppose I have a server application installed in a few Linux and Windows machines. Now I would like to control it remotely. That is, I would like to start and stop the application, update the application configuration files, reads the logs remotely.
It looks to be easy in the Linux worlds. I can use ssh, scp, and probably nfs to execute commands in remote Linux machines and access files there. The problem is that I would like to execute commands and access files in remote Linux and Windows machines uniformly from a Windows machine. I need also some scripting capabilities too.
What is the best way to do that?
You can try to use cygwin http://www.cygwin.com/ on your windows machines and install a ssh server with it.
If you need graphical tools, use VNC
there is an OpenSS implementation targeting Windows at http://sshwindows.sourceforge.net/download/
since there is (besides Cygwin/MinGW) no possibility to run bash code (ksh/csh/tcsh code as well) you should make yourself familiar with the windows scripting host.

Running Virtual Machine through a Browser (as CGI)

I have some knowledge of CGI scripts and how they work and came to thinking that, in theory, it should be possible to run VirtualBox, or any type of virtualization as a CGI script. Understandably, this would be no easy task. For one thing, it's output (the screen of the virtual OS) would somehow have to be converted so that the browser could read it, show it to the user, and then somehow update it without refreshing the page (which would mean JavaScript would be involved, probably).
Does anyone have any suggestions on how one would start a project such as this one? Has this been done/attempted before?
First of all, running a virtual machine as a CGI script is highly unpractical, as it would stop the machine if the connection is broken. Instead, the CGI script should be able to start, stop and control the virtual machine, which would be running in the background.
As for interacting with the virtual machine, VirtualBox has support for remote desktops (through the VNC protocol), which is probably the most suitable method of displaying and controlling it. However, while you can use a combination of server-side and client-side scripting to make this work, this would probably become very slow and use a lot of bandwidth. Instead, you could use something like a Java applet to connect directly to the virtual machine via VNC, which would save much of the hasstle on the server side.
There is also a similar idea called phpvirtualbox, which I haven't look into myself, but you might find it interesting.

Testing a Linux daemon on an embedded system

I have written a daemon in linux for doing dhcp for an embedded system. This platform only has a linux kernel running on it and have no CLI support. What is the best way for me to test my daemon? How do I write a program that will call the main function in this daemon and verify if its working fine?
Appreciate the answers.
When I've been in a situation like this, I've written a second daemon (or had a second listener in the existing daemon) to take the place of a CLI, listening at a particular port and responding to a very limited command set of your own choosing.
In this case, all you really care about is triggering the function on demand, so you could even have it trigger when you connect to this second port, and then report results back to the socket.
I strongly recommend, by the way, making sure your embedded system has some more generic mechanism for logging information to persistent storage and retrieving that log. It doesn't have to be syslog or anything so complicated. But you will want that ability in the future to enable forensic analysis of problems in the field.
You will want to write and debug your daemon in a full featured environment first, then install it on the embedded system at the end when you are sure it works properly.
If you can build a dhcp server for the embedded system you can surely build a simple shell for it also. Try building BusyBox or ash or dash.
You could also try using GDB remote debugging. I found an article about it.

Call external script within chroot environment

I use a chroot development environment for developing software for devices. The chroot dev environment isolates the rest of my system from my build-system hacking. The chroot environment is text-based, but I prefer to use a graphical text editor. Right now, I keep one terminal chrooted into the dev environment to build packages and one terminal pointed at the chroot environment from the outside to edit files.
I'm tired of constantly switching back and forth between these terminals, but I don't want to install X and Gnome on my compact dev environment for obvious reasons. I need a way to forward certain commands to the exterior environment, but I can't think of a simple solution. How can I execute a command on the exterior system from within a chroot environment?
Yes, SSH can be used, but without X forwarding, because you want to run the app outside of chroot, not inside. This means you have to tell the app where is its X server, because SSH won't do it for you. It is done by setting DISPLAY environment variable prior running any X app to the same value as your non-chrooted terminal has, usually it is:
export DISPLAY=:0
You could SSH into your own system, enabling X forwarding, and set it up with keys so no password is required. At minimum, something like:
ssh localhost -c my-graphical-editor
When an account is chrooted, everything that you need has to exist in the chroot / environment. That means /usr, /opt/, etc., has to be "local" and populated with whatever code is required. Graphical interfaces typically require a boatload of support code.
You may personally prefer a graphical interface but is it necessary? Or more correctly will it compromise the jail; make it easier to break out of jail?
You can su back and forth pretty quickly...

Resources