使用 SSH.NET 响应交互式 shell 提示

问题描述 投票:0回答:1

我想通过 ASP.NET 应用程序创建 SFTP 帐户。为了定义其密码,我需要输入两次

root@localhost:~# passwd fadwa
Enter new password:
Retype new password:
passwd: password updated successfully 

为了通过C#代码做到这一点,我在这里咨询了很多解决方案后尝试了以下方法,但它不起作用。

using (var client = new SshClient("xx.xxx.xxx.xxx", 22, "root", "********"))
{
    client.Connect();
    ShellStream shellStream = client.CreateShellStream(string.Empty, 0, 0, 0, 0, 0);
    StreamWriter stream = new StreamWriter(shellStream);
    StreamReader reader = new StreamReader(shellStream);
    stream.WriteLine("passwd fadwa"); // It displays -1
    stream.WriteLine("fadwa");
    stream.WriteLine("fadwa");
    Console.WriteLine(reader.Read());    // It displays -1     
    client.Disconnect();
}

我什至不使用

StreamWriter
而是直接尝试过:

shellStream.WriteLine("passwd fadwa\n" + "fadwa\n" + "fadwa\n");
while (true) Console.WriteLine(shellStream.Read()); 

还有

shellStream.WriteLine("passwd fadwa");
shellStream.WriteLine("fadwa");
shellStream.WriteLine("fadwa"); 
while (true) Console.WriteLine(shellStream.Read()); 

我得到了这个,它卡在那里!!

有什么建议为什么不起作用或其他解决方案吗?我想我已经尝试过第二种解决方案并且它有效,但现在不行。

.net shell ssh sftp ssh.net
1个回答
2
投票

您可能必须在提示出现后才发送输入。如果您太早发送输入,它会被忽略。

一个蹩脚的解决方案是这样的:

shellStream.WriteLine("passwd fadwa");
Thread.Sleep(100);
shellStream.WriteLine("fadwa");
Thread.Sleep(100);
shellStream.WriteLine("fadwa"); 

更好的解决方案是等待提示,然后再发送密码 -

expect
-类似:

shellStream.WriteLine("passwd fadwa");
shellStream.Expect("Enter new password:");
shellStream.WriteLine("fadwa");
shellStream.Expect("Retype new password:");
shellStream.WriteLine("fadwa");

一般来说,自动化 shell 总是容易出错,应该避免。

© www.soinside.com 2019 - 2024. All rights reserved.