0

I am traying to access Oracle Linux Server 7 via SSH and connecting successfully. Whenever want to get all enviroment variables via powershell its work fine , but i am getting fewer enviroment variables when i use SSH.NET (.NET SSH Library)

Here is my code

static void Main(string[] args)
{
    var host = $"host";
    var user = "username";
    var pwd = "password";
 
    ConnectionInfo ConnNfo = new ConnectionInfo(host, 22, user,
        new AuthenticationMethod[]{
            // Pasword based Authentication
            new PasswordAuthenticationMethod(user,pwd),
            new PrivateKeyAuthenticationMethod("rsa.key"),

        }
    );
    using (var sshclient = new SshClient(ConnNfo))
    {
        sshclient.Connect();
        if (sshclient.IsConnected)
        {
            RunCommand(sshclient, "printenv");
            sshclient.Disconnect();
        }

    }

    Console.ReadKey();
}

I was tried some solutions in this question but i didn't get any result.So what can i do in this situation?

khayyam
  • 33
  • 1
  • 10
  • The most likely explanation is connecting via powershell is creating an interactive session (you don't show the command you ran so this is an educated guess) while your .NET program is creating a non-interactive session. If you do `ssh $host printenv` from powershell do you see the same set of env vars you see when running your .NET program? If yes then that's the reason. – Kurtis Rader Nov 04 '20 at 18:58
  • @KurtisRader i know reason caused by that but how can i solve this ? – khayyam Nov 05 '20 at 10:21
  • Your original question makes it quite clear you did not know the reason for the difference in behavior. You "solve this" by either configuring your .NET program to establish an interactive SSH session, or you reconfigure the remote shell to set the desired env vars for non-interactive shells, or you configure SSH to pass the desired env vars from the client to server machine. – Kurtis Rader Nov 05 '20 at 20:21

1 Answers1

0

It seems that the ssh login don't load the user profile.

I get the complete Environment Variables of printenv by executing the profile first.

//RunCommand(sshclient, "printenv");
var result = sshclient.RunCommand(". .bash_profile && printenv");
 Console.WriteLine((result.Result));
M.Hassan
  • 10,282
  • 5
  • 65
  • 84