Prompt string not being detected by DetectPrompt

0 votes
asked Sep 29, 2014 by sujayghosh (360 points)
edited Dec 18, 2014

The terminal prompt is "login as" , without the quotes.

I am using the following code, to log into the terminal.

On executing the program , I am seeing an error , "Could not automatically detect prompt"

            var sshVar = new Ssh();
            sshVar.Connect(hostip);

            sshVar.Login("userid", "pwd");

            // create a new instance of VirtualTerminal
            // (creates an SSH shell session)
            VirtualTerminal terminal = sshVar.StartVirtualTerminal();

            Scripting script = terminal.Scripting;
            script.DetectPrompt();

Please let me know what should be done.

Sujay

3 Answers

0 votes
answered Oct 6, 2014 by Pavel Matyska (12,040 points)
edited Dec 18, 2014
 
Best answer

Hello, thank you for the log. After analyzing that log it seems that your server is not supported with the DetectPrompt() method. DetectPrompt() expects that when the ENTER is sent, the prompt appears right below the current line. Unfortunately, in your case the server sends empty line and then the prompt. This is not supported scenario yet. We might add it in the future.

For now, you can detect it with the Scripting API yourself. You may begin with this code:

private static void DetectPrompt(Scripting script)
{
    string promptLine = "";

    while (true)
    {
        string line = script.ReadUntil(">" & ScriptEvent.Delay(300));

        line = line.TrimStart();

        if (line != promptLine)
        {
            script.Send(FunctionKey.Enter);
            promptLine = line;
        }
        else
        {
            script.Prompt = "string:" + promptLine;
            break;
        }
    }
}

You may need change the delay time or add a check to break the loop after few attempts.

If your prompt can end with other characters than ">", you can use a regular expression to describe the prompt ending. E.g. if the prompt can end with ">", "$" or "#", use this code:

string line = script.ReadUntil(ScriptEvent.FromRegex("[>$#]") & ScriptEvent.Delay(300));

Scripting.DetectPrompt() uses script.Terminal.Screen.GetRegionText(...) to determine what the sent ENTER did with the terminal screen. You can try this approach as well.

commented Oct 6, 2014 by sujayghosh (360 points)
edited Oct 6, 2014

Thanks for the reply. Shall the above work for a generic SSH, I am asking this because , I am developing this for a product, using which SSH connection can be established. How do we handle this situation.

commented Oct 6, 2014 by Pavel Matyska (12,040 points)
edited Oct 6, 2014

If your generic SSH prompts will be similar to "QOSTESTFW01>" as shown in the log, then yes, it should work on any of these SSH servers.

I think it has the same problem as the out of the box Scripting.DetectPrompt(). You can encounter a server that will not behave as you expect and then you need to adapt to it. You may also consider prompts in this form: "user@server working-dir#" and its derivates (i.e. enclosed in parentheses: [prompt]). In this case, it is better to use prompt as regex: `script.Prompt = "regex:user@server [^#]*# ?";

commented Oct 6, 2014 by sujayghosh (360 points)
edited Oct 6, 2014

How do I adopt a server , which I am unaware of. I dont know what the end users shall use. Is there a generic way, of solving this.

commented Oct 7, 2014 by Lukas Pokorny (104,330 points)
edited Oct 7, 2014

Unfortunately, there is no generic way that would work with all the possible servers. Even though it might be easily possible to automatically detect the prompt in 99 % of cases, one can always make a server use such a strange prompt that even a human might have problems determining what the prompt is... Fortunately, such servers are not going to be very common. This said, if you do encounter a server whose prompt our DetectPrompt method can't detect, please let us know and we will try our best to add support for it!

commented Oct 9, 2014 by sujayghosh (360 points)
edited Oct 9, 2014

Thanks Lukas, it works now . As said the prompts can be different , so if the prompt is # , shall I replace the > character with the # character. The code does not work when the prompt is #. I changed the code to string line = script.ReadUntil("#" & ScriptEvent.Delay(300)); to make it work with the # sign , but it does not.

commented Oct 15, 2014 by sujayghosh (360 points)
edited Oct 15, 2014

Hi , Do you have any update on my earlier comment.

commented Oct 17, 2014 by Lukas Matyska (55,470 points)
edited Oct 17, 2014

Sorry, we missed your earlier comment.

However, replacing the > character with the # character should work for the "#" server prompt.

Please, ensure that the server prompt is really "#".

If the servers prompt is really "#", please send us the verbose communication log for analysis. It can be produced like:

sshVar.LogWriter = new Rebex.FileLogWriter(@"c:\rebexlog\log.txt", Rebex.LogLevel.Verbose);

commented Dec 18, 2014 by Pavel Matyska (12,040 points)
edited Dec 18, 2014

Hello, we just released new version in which Scripting.DetectPrompt() supports your server behavior. It can now detect servers, that behaves like this:

Prompt: <send enter>
<receive empty line>
Prompt:

in addition to:

Prompt: <send enter>
Prompt:
0 votes
answered Sep 30, 2014 by Pavel Matyska (12,040 points)
edited Oct 16, 2014

Hello, the code you post is in most cases correct. It depends on the server you are connecting to. Just for clarification, "login as" prompt is shown when you connect directly with terminal to the server without calling SSH login method. This scenarion you can see when you use Putty. Then cames "password" prompt and after successful login the real prompt follows. It can look like this: "user@host pwd$ ". This real prompt should script.DetectPrompt() method detect. Since you are logged in using SSH login method, you don't get the "login as" prompt.

What is your "real" prompt look like when you successfully connect and login to the server?

commented Sep 30, 2014 by sujayghosh (360 points)
edited Sep 30, 2014

The real prompt shows as "QOSTESTFW01 >" without the quotes , but I get that when I use Putty .

I have specified the user id and passwrod when connecting programatically; but I am seeinng the error message I posted.

commented Sep 30, 2014 by Pavel Matyska (12,040 points)
edited Sep 30, 2014

Hello, has the server some delay after sending welcome message and showing the real prompt? If yes, try to call script.WaitFor(ScriptEvent.AnyText) before calling script.DetectPrompt(). If not, please create a log and send it to us on our support mail: support@rebex.net

How to create a log please see: http://www.rebex.net/kb/logging/default.aspx

commented Oct 1, 2014 by sujayghosh (360 points)
edited Oct 1, 2014

The folder https://www.dropbox.com/sh/7tr987kz4obuc4r/AADC4JSlWROXTKwkAjk6Fyg8a?dl=0 has the code, the exception and the log

commented Oct 2, 2014 by Pavel Matyska (12,040 points)
edited Oct 16, 2014

Please create the log using this code. Assignig a new LogWriter after each command overrides created log with the previous asignment. And I simplify the process of getting the Scripting instance. Here is the code:

var sshVar = new Ssh();
sshVar.Connect("ipaddres");
sshVar.Login("uid", "pwd");

sshVar.LogWriter = new Rebex.FileLogWriter(@"c:\rebexlog\log.txt", Rebex.LogLevel.Debug);

Scripting script = sshVar.StartScripting();
script.WaitFor(ScriptEvent.AnyText);

try
{
    script.DetectPrompt();
}
catch (Exception exception)
{
    MessageBox.Show(exception.Message);
    throw;
}
commented Oct 2, 2014 by sujayghosh (360 points)
edited Oct 2, 2014

I used the above mentioned code , and the log is as below. I am seeing the same exception.

The log is at http://pastebin.com/1TiyKYTV

commented Oct 2, 2014 by Pavel Matyska (12,040 points)
edited Oct 2, 2014

I am very sorry, I did not noticed the Debug log level I need LogLevel.Verbose to see the communication log. Is is possible to create another log with LogLevel.Verbose. Again, I am very sorry.

commented Oct 4, 2014 by sujayghosh (360 points)
edited Oct 4, 2014

Updated - The verbose log is at http://pastebin.com/U2h15YHV

0 votes
answered Oct 31, 2014 by Andrey Rudakov (360 points)
edited Dec 18, 2014

I have same problem with my cisco switch (WaitFor(ScriptEvent.AnyText) + DetectPrompt()), but i can't reproduce it at 100% cases. Sometimes works perfectly, sometimes i am seeing an error: Could not automatically detect prompt.

"Success" log: http://pastebin.com/E9SkTNLU

2014-10-31 11:18:17.043 VERBOSE Ssh(1)[9] SSH: Sending packet SSH_MSG_CHANNEL_DATA (10 bytes).
 0000 |5E-00-00-00-03-00-00-00 01-0D                  | ^.........
...
2014-10-31 11:18:17.051 VERBOSE Ssh(1)[9] SSH: Received packet SSH_MSG_CHANNEL_DATA (11 bytes).
 0000 |5E-00-00-00-00-00-00-00 02-0D-0A               | ^..........
2014-10-31 11:18:17.051 VERBOSE Ssh(1)[9] SSH: Received packet SSH_MSG_CHANNEL_DATA (13 bytes).
 0000 |5E-00-00-00-00-00-00-00 04-53-57-32-3E         | ^........SW2>

1 WaitFor(ScriptEvent.AnyText) + 1 DetectPrompt() = 1 Sending + 1 Recevied('\r\n') + 1 Recevied('SW2>') - OK!

"Failure" log: http://pastebin.com/0YyrR4EJ

2014-10-31 10:58:08.819 VERBOSE Ssh(1)[9] SSH: Sending packet SSH_MSG_CHANNEL_DATA (10 bytes).
 0000 |5E-00-00-00-03-00-00-00 01-0D                  | ^.........
2014-10-31 10:58:08.820 DEBUG Ssh(1)[9] Info: Sent 1 bytes of data.
2014-10-31 10:58:08.820 VERBOSE Ssh(1)[9] Info: Sent data:
 0000 |0D                                             | .
2014-10-31 10:58:08.921 VERBOSE Ssh(1)[9] SSH: Sending packet SSH_MSG_CHANNEL_DATA (10 bytes).
 0000 |5E-00-00-00-03-00-00-00 01-0D                  | ^.........
2014-10-31 10:58:08.921 DEBUG Ssh(1)[9] Info: Sent 1 bytes of data.
2014-10-31 10:58:08.921 VERBOSE Ssh(1)[9] Info: Sent data:
 0000 |0D                                             | .
2014-10-31 10:58:09.047 VERBOSE Ssh(1)[9] SSH: Sending packet SSH_MSG_CHANNEL_DATA (10 bytes).
 0000 |5E-00-00-00-03-00-00-00 01-0D                  | ^.........
2014-10-31 10:58:09.047 DEBUG Ssh(1)[9] Info: Sent 1 bytes of data.
2014-10-31 10:58:09.048 VERBOSE Ssh(1)[9] Info: Sent data:
 0000 |0D                                             | .
2014-10-31 10:58:09.187 VERBOSE Ssh(1)[9] SSH: Sending packet SSH_MSG_CHANNEL_DATA (10 bytes).
 0000 |5E-00-00-00-03-00-00-00 01-0D                  | ^.........
2014-10-31 10:58:09.189 DEBUG Ssh(1)[9] Info: Sent 1 bytes of data.
2014-10-31 10:58:09.189 VERBOSE Ssh(1)[9] Info: Sent data:
 0000 |0D                                             | .
2014-10-31 10:58:09.339 VERBOSE Ssh(1)[9] SSH: Sending packet SSH_MSG_CHANNEL_DATA (10 bytes).
 0000 |5E-00-00-00-03-00-00-00 01-0D                  | ^.........
2014-10-31 10:58:09.339 DEBUG Ssh(1)[9] Info: Sent 1 bytes of data.
2014-10-31 10:58:09.339 VERBOSE Ssh(1)[9] Info: Sent data:
 0000 |0D                                             | .
2014-10-31 10:58:09.511 VERBOSE Ssh(1)[9] SSH: Sending packet SSH_MSG_CHANNEL_DATA (10 bytes).
 0000 |5E-00-00-00-03-00-00-00 01-0D                  | ^.........
...
2014-10-31 10:58:09.537 VERBOSE Ssh(1)[9] SSH: Received packet SSH_MSG_CHANNEL_DATA (11 bytes).
 0000 |5E-00-00-00-00-00-00-00 02-0D-0A               | ^..........
2014-10-31 10:58:09.537 VERBOSE Ssh(1)[9] SSH: Received packet SSH_MSG_CHANNEL_DATA (13 bytes).
 0000 |5E-00-00-00-00-00-00-00 04-53-57-32-3E         | ^........SW2>
2014-10-31 10:58:09.537 VERBOSE Ssh(1)[9] SSH: Received packet SSH_MSG_CHANNEL_DATA (11 bytes).
 0000 |5E-00-00-00-00-00-00-00 02-0D-0A               | ^..........
2014-10-31 10:58:09.537 VERBOSE Ssh(1)[9] SSH: Received packet SSH_MSG_CHANNEL_DATA (13 bytes).
 0000 |5E-00-00-00-00-00-00-00 04-53-57-32-3E         | ^........SW2>
2014-10-31 10:58:09.537 VERBOSE Ssh(1)[9] SSH: Received packet SSH_MSG_CHANNEL_DATA (11 bytes).
 0000 |5E-00-00-00-00-00-00-00 02-0D-0A               | ^..........
2014-10-31 10:58:09.537 VERBOSE Ssh(1)[9] SSH: Received packet SSH_MSG_CHANNEL_DATA (13 bytes).
 0000 |5E-00-00-00-00-00-00-00 04-53-57-32-3E         | ^........SW2>
2014-10-31 10:58:09.537 VERBOSE Ssh(1)[9] SSH: Received packet SSH_MSG_CHANNEL_DATA (11 bytes).
 0000 |5E-00-00-00-00-00-00-00 02-0D-0A               | ^..........
2014-10-31 10:58:09.537 VERBOSE Ssh(1)[9] SSH: Received packet SSH_MSG_CHANNEL_DATA (13 bytes).
 0000 |5E-00-00-00-00-00-00-00 04-53-57-32-3E         | ^........SW2>
2014-10-31 10:58:09.537 VERBOSE Ssh(1)[9] SSH: Received packet SSH_MSG_CHANNEL_DATA (11 bytes).
 0000 |5E-00-00-00-00-00-00-00 02-0D-0A               | ^..........
2014-10-31 10:58:09.537 VERBOSE Ssh(1)[9] SSH: Received packet SSH_MSG_CHANNEL_DATA (13 bytes).
 0000 |5E-00-00-00-00-00-00-00 04-53-57-32-3E         | ^........SW2>
2014-10-31 10:58:09.537 VERBOSE Ssh(1)[9] SSH: Received packet SSH_MSG_CHANNEL_DATA (11 bytes).
 0000 |5E-00-00-00-00-00-00-00 02-0D-0A               | ^..........
2014-10-31 10:58:09.537 VERBOSE Ssh(1)[9] SSH: Received packet SSH_MSG_CHANNEL_DATA (13 bytes).
 0000 |5E-00-00-00-00-00-00-00 04-53-57-32-3E         | ^........SW2>

1 WaitFor(ScriptEvent.AnyText) + 1 DetectPrompt() = 6 Sending + 6 Recevied('\r\n') + 6 Recevied('SW2>') - EXCEPTION!

commented Oct 31, 2014 by Pavel Matyska (12,040 points)
edited Dec 18, 2014

Update: The enhancements are now part of Rebex Terminal Emulation 2014 R3.

Hello, we made some fixes which will be present in next release. What happen to you is that the server have some larger delay in response to detect prompt algorithm (send enter and see what it get), and the detect prompt was not expected that large delay.

If you want a beta and give it a try please let us know.

...