user2970916
user2970916

Reputation: 1186

SharpSsh Command Execution

I am trying to create a SSH client using C#. I am using the Tamir.SharpSsh library. I am having issues sending the command and getting an appropriate response from the server.

The way I have been testing it is by sending over the ls command, followed by cd.. command, followed by ls command again -- to see whether or not the cd.. command is being executed properly. However, there is no difference between the first ls command and the second ls command. I am not entirely sure what I am doing wrong. Perhaps I am using the wrong Ssh type. I have provided the code and the output I am getting. I have also added the output that I expect.

using Tamir.SharpSsh;

namespace SSHNetExample
{
    class Program
    {
        static void Main(string[] args)
        {
            SshExec ssh = null;
            try
            {
                Console.Write("-Connecting...");
                ssh = new SshExec(host, username, password);

                ssh.Connect();

                Console.WriteLine("OK ({0}/{1})", ssh.Cipher, ssh.Mac);
                Console.WriteLine("Server version={0}, Client version={1}", ssh.ServerVersion, ssh.ClientVersion);
                Console.WriteLine("-Use the 'exit' command to disconnect.");
                Console.WriteLine();

                while(true)
                {
                    string command = Console.ReadLine();

                    if(command == "exit")
                        break;
                    string data = ssh.RunCommand(command);

                    Console.WriteLine(data);
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }

            if(ssh != null)
            {
                Console.WriteLine("Disconnecting...");
                ssh.Close();
                Console.WriteLine("OK");
            }
        }
    }
}

Output: enter image description here

Expected Output: enter image description here

Upvotes: 0

Views: 1934

Answers (1)

Richard
Richard

Reputation: 30618

The SshExec class is for executing a single command. It logs into the server for each command, and executes the command you asked it to. It's similar (but not the same) as you logging in, typing cd .., logging out again, logging in again and typing ls - when you logged in the second time you were redirected to the home directory.

If you want to continue to use the SshExec, you should combine your commands so you can just execute a single command. In this instance, for example, you would execute ls ...

If you want to be able to issue multiple commands like an interactive session, look at the SshShell class instead of SshExec.

Upvotes: 1

Related Questions