0 votes
by (220 points)

Hi

I looking for a solution on a paging problem on Cisco IOS.

As an example, if you do show logging, you will end up with a prompt of --More-- and you are supposed to hit enter or something else to continue.

This will not be detected with scripting.DetectPrompt() and it will timeout.

1 Answer

0 votes
by (148k points)

Hi,

Basically, when using the Scripting API, you have to simulate the matching and actions a human would do. In this case, that means looking for the more "--More--" prompt, and reacting accordingly - possibly by pressing the spacebar to continue the listing, until the prompt is received instead of "--More--".

This is one possible implementation, tested with Linux more command. It might have to be adapted for the Cisco IOS if its behavior differs Linux more.

// connect to the server
var ssh = new Ssh();
ssh.Connect(...);
ssh.Login(...);

// use 80x25 terminal size
int columns = 80;
int rows = 25;

// start a terminal session
var options = new TerminalOptions();
options.Columns = columns;
options.Rows = rows;
var scripting = ssh.StartScripting(options);

// automatically detect the prompt
scripting.DetectPrompt();

// execute a command that prints "--More--" and waits for user input
scripting.SendCommand("more longlist.txt");

var cursorEvent = ScriptEvent.CursorInArea(rows -1, 0, columns-1, 1);
var moreEvent = ScriptEvent.TextAtPosition("--More--", rows - 1, 0);
var notMoreEvent = ScriptEvent.Not(moreEvent);
ScriptMatch match;
do
{
    // wait for the prompt or for "--More--" appearing at the start of last row
    match = scripting.WaitFor(ScriptEvent.Prompt, moreEvent);

    // display received data
    Console.WriteLine(scripting.ReceivedData);

    // if we matched "--More--" ...
    if (!match.IsPrompt)
    {
        // simulate pressing spacebar to continue the listing
        scripting.Send(ConsoleKey.Spacebar, 0);

        // wait for "--More--" to disappear
        match = scripting.WaitFor(ScriptEvent.Prompt, notMoreEvent);

        // display received data
        Console.WriteLine(scripting.ReceivedData);
    }

    // unless we matched the prompt, repeat this again
} while (!match.IsPrompt);
...