Question

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?

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top