【问题标题】:C# ssh.net library, cd command does not work ( cannot send "/" )C# ssh.net 库,cd 命令不起作用(无法发送“/”)
【发布时间】:2018-07-02 12:23:38
【问题描述】:

其他所有命令似乎都可以正常工作,但似乎我无法在 ssh.net 库上使用 RunCommand() 发送“/”。我需要更改我的工作目录以使用“cd /home/debian”运行程序。但是当我发送这条线时,似乎什么也没发生。我仍然留在主目录中。我该如何解决这个问题?

// start the connection
        var client = new SshClient (host, user,password);
        client.Connect();

        command = textBoxCommand.Text;   //taking the command from textbox

        if (command != "") //unless the command is empty
        {
            SshCommand sc = client.CreateCommand(command); 
            sc.Execute(); //run command
            textBoxRecieved.AppendText(command);
            textBoxRecieved.AppendText("\n");

            string answer = sc.Result;
            answer = answer.Replace("\n", "  ");
            textBoxRecieved.AppendText(sc.Error.Replace("\n", "   "));
            textBoxRecieved.AppendText(answer);

            textBoxRecieved.AppendText("\n");
            textBoxCommand.Clear();
        }

    }

【问题讨论】:

  • stackoverflow.com/help/mcve - 提供一个最小可行的完整示例..人们需要更多信息
  • "似乎什么都没有发生。我仍然留在主目录中。"你怎么会知道这事?您是否从问题中包含的代码中收到错误?

标签: c# ssh ssh.net


【解决方案1】:

实际上,cd 命令可以工作,但问题是每个单独的命令都是从主目录执行的。因此,当我使用 pwd 命令检查我的目录时,我发现我仍在主目录中,因为新命令是在主目录中执行的。

对于遇到此问题的任何人,您可以通过以下方式简单地发送需要一起连续执行的命令:

SshCommand sc = client.CreateCommand("cd /home/debian && pwd"); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 2016-02-06
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    相关资源
    最近更新 更多