为 SSH.NET 中登录设备时自动显示的提示提供输入

Providing input to prompts that automatically display upon login to a device in SSH.NET

我有一个设备正在尝试通过 SSH 连接,以便自动配置。通常当你第一次连接到设备时,它会要求你设置 a local administration account. 我们不想自动配置这个帐户,因为它违背了自动配置的目的。

使用 ssh.net,我尝试连接到设备,但身份验证被拒绝。

using (SshClient client = new SshClient("10.10.10.41", 22, username, password))
{
    client.Connect();
    SshCommand command = client.CreateCommand(newUsername)
    command.execute

    command = client.CreateCommand(newPassword)
    command.execute
}

有没有办法使用 SSH 命令来设置管理员帐户?发送用户名和密码应该很简单,但是如果你不验证,我就不能发送任何命令。

谢谢!

您可以使用 ShellStreamExpect 方法等待提示输入用户名和密码。

using (SshClient client = new SshClient("10.10.10.41", 22, username, password))
{
    client.Connect();
    ShellStream shellStream = client.CreateShellStream(string.Empty, 0, 0, 0, 0, 0);
    shellStream.Expect("Please create a local administrator account."); // probably can be omitted
    shellStream.Expect("Username:"); // Expect has more parameters 
    shellStream.WriteLine(newUsername);
    shellStream.Expect("Password:"); // prompt to type password
    shellStream.WriteLine(newPassword);
    client.Disconnect();
}