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);
                }
            }
        }
3
  • 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();! Commented Apr 13, 2016 at 16:08
  • What I meant, was when I write "cd existingFolder" in console, it still doesn't change the directory Commented Apr 13, 2016 at 16:23
  • See Run and execute multiple dependent SSH commands using C#. Commented Sep 26, 2020 at 8:23

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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: i.imgur.com/a7CBfPC.png

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.