0 votes
by (8.4k points)
edited

There was added new method public Match VirtualTerminal.Expect(Regex pattern, int maximumWaitTime) in the build 4086.

What it is good for and how to use it?

1 Answer

0 votes
by (8.4k points)
edited
 
Best answer

This method processes any available incoming data from a server until a response is received that matches the specified regular expression pattern. If no such response is received within the specified maximumWaitTime limit the Match.Empty is returned, otherwise an Match object that contains information about the match is returned.

Please note that also new property public string ReceivedData was added. It contains data received by last call of the Expect() and Process() methods. Using these two members you are able to easily script an interactive commands.

Following example shows how to programmatically script this scenario:

  1. Connect to an SSH server
  2. Run the ‘telnet’ command
  3. Login to a Telnet server
  4. Run some commands within the Telnet session and display the output
  5. Exit the Telnet session
  6. Exit the SSH session

using System;
using System.Text.RegularExpressions;

using Rebex.Net;
using Rebex.TerminalEmulation;

static void Main(string[] args)
{
    string sshServerName = "ssh-server";
    string sshUserName = "ssh-username";
    string sshPassword = "ssh-password";
    // in our case the SSH prompt is like "username@server:path$ "
    string sshPrompt = string.Format("{0}[@]{1}:.*[$] ", sshUserName, sshServerName);

    string telnetServerName = "telnet-server";
    string telnetUserName = "telnet-username";
    string telnetPassword = "telnet-password";
    // in our case the Telnet prompt is like "username@server:path$ "
    string telnetPrompt = string.Format("{0}[@]{1}:.*[$] ", telnetUserName, telnetServerName);

    // connect to an SSH server
    Ssh ssh = new Ssh();
    ssh.Connect(sshServerName);
    ssh.Login(sshUserName, sshPassword);

    // start VirtualTerminal
    VirtualTerminal term = ssh.StartVirtualTerminal();

    // process SSH server's welcome message = wait for sshPrompt to appear
    string sshWelcome = Expect(term, sshPrompt, "SSH Prompt not found.");

    // send the 'telnet' command
    Send(term, "telnet " + telnetServerName);

    // wait for the Telnet server's login prompt
    Expect(term, "[Ll]ogin: ", "Telnet login prompt not found.");
    Send(term, telnetUserName);

    // wait for the Telnet server's password prompt
    Expect(term, "[Pp]assword: ", "Telnet password prompt not found.");
    Send(term, telnetPassword);

    // process Telnet server's welcome message = wait for telnetPrompt to appear
    string telnetWelcome = Expect(term, telnetPrompt, "Telnet Prompt not found.");

    // send various commands in Telnet session and display the output
    Send(term, "uname -a");
    string commandResponse = Expect(term, telnetPrompt, "Telnet Prompt not found.");
    Console.WriteLine("OS:");
    Console.WriteLine(commandResponse);

    Send(term, "uptime");
    commandResponse = Expect(term, telnetPrompt, "Telnet Prompt not found.");
    Console.WriteLine("Uptime:");
    Console.WriteLine(commandResponse);

    Send(term, "df");
    commandResponse = Expect(term, telnetPrompt, "Telnet Prompt not found.");
    Console.WriteLine("Disk space:");
    Console.WriteLine(commandResponse);

    // exit the Telnet session
    Send(term, "exit");
    Expect(term, sshPrompt, "SSH Prompt not found.");

    // exit the SSH session and wait for the session end
    Send(term, "exit");
    while (term.Process(1000) != TerminalState.Disconnected) ;
}

/// <summary>
/// Sends the specified command to a server.
/// </summary>
public static void Send(VirtualTerminal term, string command)
{
    term.SendToServer(command + '\n');
    bool found = term.Expect("\n", 5000);
    //Console.Write(term.ReceivedData);
    if (!found)
        throw new ApplicationException("No command response.");
}

/// <summary>
/// Process any available output until the specified regexPattern appears.
/// </summary>
/// <returns>Received data up to the expected patter.</returns>
public static string Expect(VirtualTerminal term, string regexPattern, string notFoundMessage)
{
    Match m = term.Expect(new Regex(regexPattern), 5000);
    //Console.Write(term.ReceivedData);
    if (!m.Success)
        throw new ApplicationException(notFoundMessage);
    return term.ReceivedData.Substring(0, m.Index);
}
...