0 votes
by (360 points)
edited

Hi

I am using the following code to login.

I am seeing an exception, in the foloowing code, when ReaduntilPrompt is executed

script.SendCommand("show interface"); response = script.ReadUntilPrompt();

Screenshot - http://ctrlv.in/472606 Log - http://pastebin.com/YeYTrPFN

The same code is working for other devices, the only differnce being , for the other devices I am using "term length 0" instead of "term pager lines 0"

        string response;

        var sshVar = new Ssh();
        sshVar.Connect(ipaddress);
        sshVar.Login(userid, password);

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

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

        try
        {
           DetectPrompt(script);
        }
        catch (Exception exception)
        {
    throw;
        }


        script.SendCommand("enable");

        script.ReadUntil("Password:");
        // send second level password
        script.SendCommand(password);

        script.SendCommand("term pager lines 0");


        try
        {
            script.SendCommand("show interface");
            **response = script.ReadUntilPrompt();**

        }
        catch (Exception e)
        {
            Console.WriteLine("{0}", e.Message);
            throw;
        }
by (15.2k points)
edited

Hi, what your expected prompt is after DetectPrompt(script) call? If it expect only the format when you log in, then when you change the prompt with the 'enable' command, script.ReadUntilPrompt() cannot detect it correctly, of course. You can detect new prompt after you successfuly execute 'enable & password' commands, or update your DetectPrompt(...) method to expect both prompt formats, as suggested in http://forum.rebex.net/questions/4757/prompt-string-not-being-detected-by-detectprompt .

by (360 points)
edited

I have prepared a screencast demo at http://screencast-o-matic.com/watch/c2ll27eTKt The video demonstrates the way, I can log onto the device using Putty and SSH. I am using the same DetectPrompt method as stated in the related post.

by (15.2k points)
edited

Please ignore my statement that the video is not found. The forum engine added the dot '.' at the end of your sentence to the link. It is fixed now.

1 Answer

+1 vote
by (15.2k points)
edited
 
Best answer

Hi,

I was unable to watch the wideo as it the server says: Screencast not found.

Anyway, you should change the DetectPrompt method to this:

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

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

        line = line.TrimStart();

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

Please note the change in the

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

line of code. This will detect your prompt when it changes from '>' ending char to '#' char. Also make sure that you call this DetectPrompt after you expect the prompt change. In most cases after each call of script.SendCommand(...) method you should somehow read the command response and not stack unread responses. That can lead to invalid results when expecting somethig after the last command, but it appears in one of the previous responses.

This said, you shoud end up with this method:

        string response;

    var sshVar = new Ssh();
    sshVar.Connect(ipaddress);
    sshVar.Login(userid, password);

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

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

    try
    {
       DetectPrompt(script);
    }
    catch (Exception exception)
    {
throw;
    }


    script.SendCommand("enable");

    // read response until the "Password:" prompt appears
    script.ReadUntil("Password:");
    // send second level password
    script.SendCommand(password);

    // prompt has changed, detect what it looks like now
    DetectPrompt(script);

    script.SendCommand("term pager lines 0");

    // after each SendCommand, read the response.
    script.ReadUntilPrompt();

    try
    {
        script.SendCommand("show interface");
        **response = script.ReadUntilPrompt();**

    }
    catch (Exception e)
    {
        Console.WriteLine("{0}", e.Message);
        throw;
    }

Please note that second DetectPrompt(script) call is right after the "Password:" command to detect new prompt. I also added reading response of "term pager lines 0" command.

by (360 points)
edited

That worked fine. Thanks.

...