4

when I connect to server with SSH.NET library, the default folder is /mif/stud3/2014/rira1874. When I execute

res = ssh.CreateCommand("cd existingFolder").Execute();
Console.WriteLine(res);

it still stays in default connection folder. What's wrong here?

full code:

public void ConnectWithPassword(string username, string password, string domain, int port)
        {
            bool i = true;
            using (var ssh = new SshClient(CreatePasswordConnectionInfo(username, password, domain)))
            {
                try
                {
                    ssh.Connect();
                    if (ssh.IsConnected)
                    {
                        while(i == true)
                        {
                            string res = Regex.Replace(ssh.CreateCommand("pwd").Execute(), @"\r\n?|\n", "");
                            Console.Write(res + ": ");
                            res = ssh.CreateCommand(Console.ReadLine()).Execute();
                            Console.WriteLine(res);
                        }
                    }
                    else {
                        Console.WriteLine("Not connected");
                    }
                    ssh.Disconnect();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception caught: {0}", e);
                }
            }
        }
  • I don't see the question asked is the same as in your code ---> your code has `res = ssh.CreateCommand(Console.ReadLine()).Execute();`, whereas you're talking about `res = ssh.CreateCommand("cd existingFolder").Execute();`! – Am_I_Helpful Apr 13 '16 at 16:08
  • What I meant, was when I write "cd existingFolder" in console, it still doesn't change the directory – Rimantas Radžiūnas Apr 13 '16 at 16:23
  • See [Run and execute multiple dependent SSH commands using C#](https://stackoverflow.com/q/56434268/850848). – Martin Prikryl Sep 26 '20 at 08:23

2 Answers2

5

Try to do the following string cmdstr = "cd /etc; ls -la"; ssh.Connect(); var cmd = ssh.RunCommand(cmdstr); var result = cmd.Result;

Basically the ssh.net does not allow/support (i guess). The alternate is you can change to the directory and execute the respective command you need. If you need to execute some file just use the above example "cd /etc; ./yourFile". Make sure you include the semicolon ";" so you can continue execution.

Praveen
  • 51
  • 1
  • 4
1

Have you checked the directory listing to make sure you're actually still in "/mif/stud3/2014/rira1874"? It looks like where ever you're SSHing in to is a *nix box; making this assumption based on the line ssh.CreateCommand("pwd").Execute(). If this is the case, some times the directory listing that is returned/displayed on the console will not change. For example, if I have a console with the following PWD /user/me/home: $ and I try and change directories to /developer/, my console may still show that I am still at /user/me/home when really I am in /user/me/home/developer, but an ls will show otherwise.

Basically, find a way to make sure that you're not actually changing directories and the returned value from the command isn't just throwing you off.

Ingenioushax
  • 718
  • 5
  • 20
  • 1
    Thank you for the answer, although after I change directory to, let say "Documents", an existing folder, and type in `ls`, it will show not `/mif/stud3/2014/rira1874/Documents` content, but the same folder as I connected to. Unless I do `cd Documents && ls` . But still the client returns to default folder. Here's the picture: http://i.imgur.com/a7CBfPC.png – Rimantas Radžiūnas Apr 14 '16 at 09:00