What is the difference to login vis Ssh class vs physically login to server?

0 votes
asked Jan 14 by hardywang (240 points)
edited Jan 14 by hardywang

I have a server with KSH (echo "$SHELL" the response is /usr/bin/ksh), if I connect to the server from MacOS's Terminal window and run command "printenv MyEnvVar" I could get a proper response.

But if I run following code in Windows (full .NET framework 4.7.2) by using same server, username, password.

class Program
    static async Task Main(string[] args)
        Licensing.Key = "==my-key==";

        using (var session = new SshSession())
            session.Authenticate("username", "password");

            string response;

            using (var ssh = new Ssh())

                response = await ssh.RunCommandAsync("printenv MyEnvVar");

Nothing is from response. So what is the real difference? What can I do to have proper environment for this credential to login?

1 Answer

+1 vote
answered Jan 15 by Pavel Matyska (13,340 points)
selected Jan 15 by hardywang
Best answer


The difference is that when you connect with your Terminal window, you are requesting also settings for the terminal connection. When you execute ssh.RunCommandAsync method, your server execute it without the settings for the terminal. So you need to inform your server that you want to use settings for the terminal. Please use our Scripting API to do that. The code can look like this:

using (var ssh = new Ssh())
    ssh.Login("username", "password");

    Scripting scripting = ssh.StartScripting();
    response = scripting.ReadUntilPrompt();

Please note that scripting does not have async methods. Think about the Scripting API as if it is a human user in case you would like to implement your async wrapper methods around it.

commented Jan 15 by Pavel Matyska (13,340 points)
For more information about our Scripting API please read our feature page here: https://www.rebex.net/terminal-emulation.net/features/scripting.aspx
commented Jan 15 by hardywang (240 points)
Thank you very much for the explanation, it works perfectly now.