Quantcast
Channel: Rebex Q&A Forum - Recent questions and answers
Viewing all articles
Browse latest Browse all 3862

Answered: Read telnet output in while

$
0
0

Hello,

I suppose you would like to write a script that behaves like you described. It can be done like this:

Telnet telnet = new Telnet(serverName);

// get the scripting object
Scripting scripting = telnet.StartScripting();

// login
scripting.ReadUntil("login:");
scripting.SendCommand(username);

// password prompt is not a regular prompt
scripting.ReadUntil("Password:");           
scripting.SendCommand(password);

// detects regular prompt (in your case '>')
scripting.DetectPrompt();

// run asynchronous command
scripting.SendCommand("command -x test.tr;");
// read prompt that appears immediately
scripting.ReadUntilPrompt();

ScriptEvent keepAlive = ScriptEvent.Delay(30 * 1000); // this event works like keep-alive in this context
ScriptEvent success = "Job completed"; // job completed successfully (Place correct message here)
ScriptEvent error = "Error occured"; // some error occured (Place correct message or unique substring of it here)

while (true)
{
    // wait for one of these script event (they are combined with OR operator)
    ScriptMatch match = scripting.WaitFor(success, error, keepAlive);
    // job completed
    if (match.IsEventMatched(success) || match.IsEventMatched(error))
    {
        // read until the end of line (read until '\n' is received);
        scripting.ReadLine();
        break;
    }
}

// I suppose after the job is completed, new prompt is received. If not, ignore the following line.
scripting.ReadUntilPrompt();

// do other jobs now

This is the code of what I have understood from your explanation, so feel free to modify it or describe the server/command behavior with more details.


Viewing all articles
Browse latest Browse all 3862

Trending Articles