Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

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

I have a device I'm trying to connect to via SSH, in order to automate its configuration. Normally when you first connect to the device, it asks you to set up a local administration account. We don't want to configure this account automatically, because it defeats the purpose of automating this configuration.

Using ssh.net, I've tried to connect to the device, but authentication is refused.

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
}

Is there a way to use SSH commands to set up the admin account? It should be as simply as sending a username and password, but if you it won't authenticate, I can't send any commands.

Thanks!

question from:https://stackoverflow.com/questions/65839487/providing-input-to-prompts-that-automatically-display-upon-login-to-a-device-in

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can wait for the prompt to type username and password by using ShellStream and Expect method.

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...