C# 为登录到SSH.NET中的设备时自动显示的提示提供输入
我有一个我正试图通过SSH连接的设备,以便自动化其配置。通常,当您第一次连接到设备时,它会要求您进行设置。我们不希望自动配置此帐户,因为它无法实现自动配置此帐户的目的 使用,我尝试连接到设备,但身份验证被拒绝C# 为登录到SSH.NET中的设备时自动显示的提示提供输入,c#,ssh,ssh.net,C#,Ssh,Ssh.net,我有一个我正试图通过SSH连接的设备,以便自动化其配置。通常,当您第一次连接到设备时,它会要求您进行设置。我们不希望自动配置此帐户,因为它无法实现自动配置此帐户的目的 使用,我尝试连接到设备,但身份验证被拒绝 using (SshClient client = new SshClient("10.10.10.41", 22, username, password)) { client.Connect(); SshCommand command = client
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命令来设置管理员帐户?它应该像发送用户名和密码一样简单,但是如果你不验证它,我就不能发送任何命令
谢谢 您可以使用
ShellStream
和Expect
方法等待输入用户名和密码的提示
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();
}
仅针对访问此页面的其他人:这仅适用于“设备”,这些设备通常具有有限的SSH实现,这不允许使用更好的方法。对于功能齐全的SSH服务器,使用“shell”通道/流不是正确的方法+谢谢!这解决了我的问题。