env variable are missing

Aug 28, 2011 at 3:25 PM
Edited Aug 28, 2011 at 3:28 PM

Hello, 

Thanks for the great project,

I am using the ssh service and i tried to run command the command throw me an error of missing variables.

I set those variable via the export command (in terminal), the env was update as well and still once i tried to run the command using  the ssh.net the variable was missing.

so, i add those variables to etc/profile , verify using env (via the terminal)  command that those command exist, and there were exist, i run the command again from the ssh.net and still its throws me an exception that those variable are missing.

Then i run the env/set command via the ssh.net and got the env/set result, the variables were missing.

so, i run via the ssh.net the following command export var=value, no error was raised, i rerun the env/set command and still i didn;t see the new variable.

In order to refresh the env/set, i run etc/profile from the ssh.net, no change, the variable were still missing.

i run refresh from terminal session, i saw the variable but from ssh.net i saw nothing.

Can you pls help.

it is very crucial  for me

Yaronka

 

 

 

 

Coordinator
Aug 28, 2011 at 3:30 PM

I think its a common mistake that a lot of people do.

When you run the command, it runs it as a single executable unit without prior knowledge of the environment.

Since envirnoment variables exists in shell and when command executed no shell is running.

So the only solution at the moment is to either chain some commands together using pipe sign, or extract those variable some other way.

 

There is one thing I would like to implement one day, SSH protocol support ENV requests, which I didnt implement yet, I assume they deal with varaibles but I didnt get to it yet.

One time when I was playing with it it was unsuccesfull so I dropped it for now since I dont know how it works and what effect or more exactly what variables it refferes too.

 

Hope it helps,

Thanks,

Oleg

Aug 28, 2011 at 3:43 PM

Thanks for the quick replay,

how can you explaine that when i tried to execute 'env' via the ssh.net i don't get my new var's and when i tried to do it via the terminal i got it.

remember  that i define the var's in the etc/profile file, so everyone should see them.

I Even refresh the env variable in both ways.

I have also refresh the shh.net session (re-run my app).

Yaron 

Coordinator
Aug 28, 2011 at 3:49 PM

Unfortunatly I am not a UNIX/LINUX person so there are many things in UNIX I cannot explain.

But this is what I noticed over the last year.

The only reason I can think of, the reason it works with terminal is that when you running terminal you also running shell and shell loads all env variables.

 

I am not sure who and what loads env variables in unix. It looks like that during the command execution it doesnt load them. I guess I will need to expplore more about ENV channel request which I think should bring env variables back but I am not sure.

If you can, please open an issue for this so I know what to come back to, I am leaving for vacation next week so could easily forget it.

 

Thanks,

Oleg

Aug 30, 2011 at 6:21 PM

I used the shell functionality and that gave me access to the ENV variables

Sep 1, 2011 at 6:24 AM

can you pls put code example of how to  it ?

Sep 9, 2011 at 3:30 PM

Now this code is for tailing, so it keeps reading until the user cancels, but it should be modified easily enough for a single command

public void sendUnixShellCommand(string command)
{
    PasswordConnectionInfo connectionInfo = new PasswordConnectionInfo(ip, username, password);
            
    try
    {
        // Must end any command with an "enter" (\r\n)
        command += "\r\n";

        using (var client = new SshClient(connectionInfo))
        {
            client.Connect();
            var wait = new AutoResetEvent(false);
            var output = new MemoryStream();
            var shell = client.CreateShell(Encoding.ASCII, command, output, output);

            shell.Stopped += delegate(object sender, EventArgs e)
            {
                Console.WriteLine("\nDisconnected...");
                //shell.Stop();
                wait.Set();
            };
            shell.Start();
            wait.WaitOne();

            output.Seek(0, SeekOrigin.Begin);
            var result = new StreamReader(output, Encoding.ASCII).ReadToEnd();
            output.Flush();
            output.Close();
        }
    }
    catch (Exception ex)
    {
        if (ex is Renci.SshNet.Common.SshException)
        {
            string err = "There was an error while trying to connect to a2mosdv1.";
            MessageBox.Show(err, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
        }
        if (ex is Renci.SshNet.Common.SshAuthenticationException)
        {
            string err = "There was an authentication error, either the username or password is no longer valid.";
            MessageBox.Show(err, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
        }
        else
        {
            throw;
        }
    }
}

Mar 4, 2014 at 8:19 AM
I had a similar issue with missing env.
i thought about changing the default sshd provided env, but it seemed too hard to maintain.
so instead, i just modify the command i want to call, with all the exports i need first.

e.g. assume my problem is in a missing path and i need the path to be /bin:/sbin:/usr/bin:/usr/sbin:/usr/bin/X11:/usr/local/bin

then,
instead of the following code: (assume Cmd = "/usr/local/bin/myscript.sh"
string Cmd = "/usr/local/bin/myscript.sh"
vat cmd = client.CreateCommand(Cmd);
var asynch = cmd.BeginExecute();


string Cmd = "/usr/local/bin/myscript.sh"
Cmd = "export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/bin/X11:/usr/local/bin ; " + Cmd;
vat cmd = client.CreateCommand(Cmd);
var asynch = cmd.BeginExecute();


i tried this, and it works well with multiple number of exports, chained with semi-column.

one can extend that to an full env , spread as a big line with semi-column and thus compensating for the missing environment.