SSH.NET C#向Linux服务器发送Yes或No

时间:2014-02-21 15:25:55

标签: c# linux ssh

我想从C#程序在linux服务器上执行一个脚本,我有一个示例代码如下。

var ssh = new SshClient("ip", "user", "password");
var cmd = ssh .CreateCommand("./executescript");
cmd.BeginExecute();
var reader = new StreamReader(cmd.OutputStream);
var result = reader.ReadToEnd();

但是我无法完成脚本的执行,因为它会问一个问题,比如'你愿意这样做吗?是或否',如何从C#向Linux发送是或否?

有人能给我一些建议吗?或者任何提示?

1 个答案:

答案 0 :(得分:0)

试试这段代码

            SshShell shell = new SshShell(<host>, username, pass);
            shell.Connect();
            shell.RemoveTerminalEmulationCharacters = true;
            string outputt = shell.Expect(new Regex(@"[$#>]"));
            shell.WriteLine("cd <Path> ");
            outputt = shell.Expect(new Regex(@"[$#>]"));
            shell.WriteLine("nohup bash executescript.sh & ");
            outputt = shell.Expect(new Regex(@"[$#>]"));
            shell.WriteLine("exit");
            outputt = shell.Expect(new Regex(@"[$#>]"));

            shell.Close();

它对我有用。