0

I am currently making a File-Dialog-like form in C# that browses directories on a unix server. I have a bit of a trouble getting the 'cd ..' command to work.

Here is an example of my code

    var sshExec = new SshExec("192.x.x.x", "user", "pass");
    sshExec.Connect();
    var err = string.Empty;
    var out = string.Empty;
    sshExec.RunCommand("pwd", ref out, ref err);
    Console.Writeline(out);
    sshExec.RunCommand("cd ..");
    sshExec.RunCommand("pwd", ref out, ref err);
    Console.Writeline(out);

I've tried other formats such as cd .. or $"cd .." and yet I seem to remain in the same directory. I guess every time I use RunCommand() sshExec creates a new transaction therefore I stay in the same directory.

Anyone knows how can I make this work?

Kostya Sydoruk
  • 144
  • 2
  • 15
  • possible duplicate of [SharpSSH with Persistent ShellExec connections](http://stackoverflow.com/questions/3141570/sharpssh-with-persistent-shellexec-connections) – Eugene Mayevski 'Callback Jan 28 '13 at 07:11
  • Also see related posts like http://stackoverflow.com/questions/12280021/sshexec-of-sharpssh-disconnects-when-runcommand-method-is-used?rq=1 – Eugene Mayevski 'Callback Jan 28 '13 at 07:11
  • Woah, haven't seen that one, but my problem is almost identical. Although, none of the posts above seems to be solving my problem. – Kostya Sydoruk Jan 28 '13 at 07:13
  • Not going to lie, the SharpSSH library isn't too solid nor updated. I would look at SSH.net on Codeplex for something that's been written for .NET instead of a Java port. – MattGWagner Jan 29 '13 at 14:00

1 Answers1

0
sshExec.RunCommand("pwd", ref out, ref err);
sshExec.RunCommand("cd ..");
sshExec.RunCommand("pwd", ref out, ref err);

Each call to RunCommand() will create a separate channel which runs independently of the others. In the common case (making an ssh connection to a unix server), each channel will invoke a separate shell instance. A command like cd run in one channel won't affect subsequent commands launched in different channels.

To do what you want, you have to arrange to run the sequence of commands in the same RunCommand invocation. Assuming the remote server is a unix server invoking a shell like bash, you can use shell syntax, for example:

sshExec.RunCommand("pwd && cd .. && pwd", ref out, ref err);
Kenster
  • 23,465
  • 21
  • 80
  • 106