Search code examples
c#puttyssh.net

SSH.NET Expect() Unexpected Prompts


I've written my own interface into Linux with my own translation of commands and Ansible extensions. I'm using Visual Studio 2019, connecting to CentOS, running the latest Ansible, while connecting using SSH.NET.

Because this is 1000's of lines of code, this is a very high level sudo code, I'm doing this:

const string Command_Prompt = @"[\?$#]|\[.*@(.*?)\][$%#]";
var promptRegex = new Regex(Command_Prompt);
while(_lastCommand != "exit") {
    _lastCommand = Console.ReadLine();

    _shellStream.WriteLine(_lastCommand);
    var output = _shellStream.Expect(promptRegex);
    Log.Verbose(output);
}

This picks up

[root@localhost ~]#
[root@localhost ~]$
#
$
What is your first name?

My Ansible ymls have promtps and this works perfectly if the prompt has a question mark. If I'm expecting something specific it works fine as I can pass into expect anything I like. However, there are times, I run a linux command and the command has a prompt that is unexpected, this is waiting for the expected information to come back. Is there no way to do what MRemote, Putty, or VSCode terminal does with unknown prompts? I can't really allow others to use my product if I don't know what their prompts are going to look like. What does MRemote, Putty and VSCode terminal do specifically that SSH.NET clearly doesn't do or am I'm not using SSH.NET correctly?


Solution

  • I ended up, just go with the basics.

    .*\?:|[$#]|\[.*@(.*?)\][$%#]
    

    Picks up the following

    [root@localhost ~]#
    [root@localhost ~]$
    #
    $
    What is your first name?: