Rimantas Radžiūnas
Rimantas Radžiūnas

Reputation: 257

Cannot change directory with C# SSH.NET

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);
                }
            }
        }

Upvotes: 4

Views: 6865

Answers (2)

Praveen
Praveen

Reputation: 61

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.

Upvotes: 6

Ingenioushax
Ingenioushax

Reputation: 718

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.

Upvotes: 1

Related Questions