Class Expect


  • public class Expect
    extends com.logicmonitor.common.sse.utils.SSEResource
    This class is provided for interaction with remote devices or communication with another process. By using Expect you can:
    • Open an new SSH session and communicate with it.
    • Open a TELNET session and communicate with it.
    • Communicate with an external process.
    Example:
    1. Open an new SSH session and get our agent.conf:
     
     import com.santaba.agent.groovyapi.expect.Expect;
    
     // log into remote device
     Expect client = Expect.open("YourHost", "YourUser", "YourPass" , 30 )
     // print content of agent.conf
     client.send("cat /usr/local/logicmonitor/agent/conf/agent.conf\n")
     // exit login
     client.send("exit\n")
     // blocking for connection exit
     client.expectClose()
     println(client.stdout())
     
    2.Execute an external process to do ntp query:
     
      import com.santaba.agent.groovyapi.expect.Expect;
      Expect expect = Expect.open("../lib/ntpq.exe -nc peers 192.168.170.188")
      expect.expectClose()
      println expect.stdout()
      

    Reference: Logicmonitor support page
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String before()
      get the output between the current matched expect() and the previous matched expect()
      void close()
      close this Expect session.
      int exitValue()  
      void expect​(java.lang.String pattern)
      reads text from the connection until it finds a match in the provided regular expression
      int expect​(java.lang.String[] patterns)
      reads text from the connection until it finds a match in the provided regular expressionS.
      int expect​(java.lang.String[] patterns, int bufferSize)
      reads text from the connection until it finds a match in the provided regular expressions.
      void expect​(java.lang.String pattern, int bufferSize)
      reads text from the connection until it finds a match in the provided regular expression
      void expectClose()
      Wait until close, ignoring any output from stdout or stderr if exists
      void expectClose​(ExpectedOutputHandler stdOutHandler)
      Wait until close, ignoring any output from stderr if exists
      void expectClose​(ExpectedOutputHandler stdOutHandler, ExpectedOutputHandler stdErrHandler)
      Wait until close
      boolean isReleased()
      Indicate the current object is released or not.
      java.lang.String matched()
      get the current matched string
      static Expect open​(java.lang.String command)
      Start a new external process
      static Expect open​(java.lang.String command, int timeoutInSecond)
      Start a new process
      static Expect open​(java.lang.String host, int port, int timeoutInSeconds)
      Start a telnet session for remote host
      static Expect open​(java.lang.String host, int port, java.lang.String user, java.lang.String pass, int timeoutInSeconds, java.lang.String... lib)
      instantiate an SSH connection object to a remote CLI.
      static Expect open​(java.lang.String host, int port, java.lang.String user, java.lang.String pass, java.lang.String... lib)
      instantiate an SSH connection object to a remote CLI.
      static Expect open​(java.lang.String host, java.lang.String user, java.lang.String pass, int timeoutInSeconds, java.lang.String... lib)
      instantiate an SSH connection object to a remote CLI.
      static Expect open​(java.lang.String host, java.lang.String user, java.lang.String pass, java.lang.String... lib)
      instantiate an SSH connection object to a remote CLI.
      void release()
      Try to release the resource of current object.
      close this Expect session.
      void send​(java.lang.String msg)
      send a string to the Expect connection
      java.lang.String stderr()
      get the standard error for current Expect session
      java.lang.String stdout()
      get the standard output for current Expect session
      java.lang.String toString()  
      • Methods inherited from class com.logicmonitor.common.sse.utils.SSEResource

        register, unregister
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • isReleased

        public boolean isReleased()
        Indicate the current object is released or not.
        Specified by:
        isReleased in class com.logicmonitor.common.sse.utils.SSEResource
        Returns:
        false We will auto close related objects. so there's no need to call this manually.
      • release

        public void release()
        Try to release the resource of current object.
        close this Expect session. We will auto close related objects. so there's no need to call this manually.
        Specified by:
        release in class com.logicmonitor.common.sse.utils.SSEResource
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • open

        public static Expect open​(java.lang.String command)
                           throws java.io.IOException
        Start a new external process
        Parameters:
        command - the command
        Returns:
        the Expect object
        Throws:
        java.io.IOException
      • open

        public static Expect open​(java.lang.String command,
                                  int timeoutInSecond)
                           throws java.io.IOException
        Start a new process
        Parameters:
        command -
        timeoutInSecond - the read timeout in seconds for the current process
        Returns:
        the Expect object
        Throws:
        java.io.IOException
      • open

        public static Expect open​(java.lang.String host,
                                  java.lang.String user,
                                  java.lang.String pass,
                                  java.lang.String... lib)
                           throws java.io.IOException
        instantiate an SSH connection object to a remote CLI. this will connect to 22 port.
        Parameters:
        host - hostname to which we should initiate an ssh connection
        user - use name for the remote host
        pass - password for the remote host
        Returns:
        the Expect object if connected
        Throws:
        java.io.IOException
      • open

        public static Expect open​(java.lang.String host,
                                  int port,
                                  java.lang.String user,
                                  java.lang.String pass,
                                  java.lang.String... lib)
                           throws java.io.IOException
        instantiate an SSH connection object to a remote CLI. .
        Parameters:
        host - hostname to which we should initiate an ssh connection
        port - the port to an ssh connection
        user - use name for the remote host
        pass - password for the remote host
        Returns:
        the Expect object if connected
        Throws:
        java.io.IOException
      • open

        public static Expect open​(java.lang.String host,
                                  java.lang.String user,
                                  java.lang.String pass,
                                  int timeoutInSeconds,
                                  java.lang.String... lib)
                           throws java.io.IOException
        instantiate an SSH connection object to a remote CLI. this will connect to 22 port.
        Parameters:
        host - hostname to which we should initiate an ssh connection
        user - use name for the remote host
        pass - password for the remote host
        timeoutInSeconds - the read timeout in seconds for the remote host
        Returns:
        the Expect object if connected
        Throws:
        java.io.IOException
      • open

        public static Expect open​(java.lang.String host,
                                  int port,
                                  java.lang.String user,
                                  java.lang.String pass,
                                  int timeoutInSeconds,
                                  java.lang.String... lib)
                           throws java.io.IOException
        instantiate an SSH connection object to a remote CLI.
        Parameters:
        host - hostname to which we should initiate an ssh connection
        port - the port to an ssh connection
        user - use name for the remote host
        pass - password for the remote host
        timeoutInSeconds - the read timeout in seconds for the remote host
        Returns:
        the Expect object if connected
        Throws:
        java.io.IOException
      • open

        public static Expect open​(java.lang.String host,
                                  int port,
                                  int timeoutInSeconds)
                           throws java.io.IOException
        Start a telnet session for remote host
        Parameters:
        host - hostname to which we should telnet to
        port - the port
        timeoutInSeconds - the read timeout in seconds for the remote host
        Returns:
        the Expect object if connected
        Throws:
        java.io.IOException
      • send

        public void send​(java.lang.String msg)
                  throws java.io.IOException
        send a string to the Expect connection
        Parameters:
        msg - the message sent to the Expect connection
        Throws:
        java.io.IOException
      • expect

        public void expect​(java.lang.String pattern)
                    throws java.io.IOException,
                           TimeoutException
        reads text from the connection until it finds a match in the provided regular expression
        Parameters:
        pattern - either a regular expression string or an array of regular expression strings against which the remote text should match
        Throws:
        java.io.IOException
        TimeoutException - timeout happened here
      • expect

        public void expect​(java.lang.String pattern,
                           int bufferSize)
                    throws java.io.IOException,
                           TimeoutException
        reads text from the connection until it finds a match in the provided regular expression
        Parameters:
        pattern - either a regular expression string or an array of regular expression strings against which the remote text should matcj
        bufferSize - used to optimize the response time, default is 10240 bytes. If the response body is huge, and expect takes too long, try specify larger buffer size.
        Throws:
        java.io.IOException
        TimeoutException
      • expect

        public int expect​(java.lang.String[] patterns)
                   throws java.io.IOException,
                          TimeoutException
        reads text from the connection until it finds a match in the provided regular expressionS.
        Parameters:
        patterns -
        Returns:
        which pattern in the patterns was matched (the index start with 0.) if none matched, return -1.
        Throws:
        java.io.IOException
        TimeoutException - timeout happened here
      • expect

        public int expect​(java.lang.String[] patterns,
                          int bufferSize)
                   throws java.io.IOException,
                          TimeoutException
        reads text from the connection until it finds a match in the provided regular expressions.
        Parameters:
        patterns - regular expression strings.
        bufferSize - used to optimize the response time, default is 10240 bytes. If the response body is huge, and expect takes too long, try increase the buffer size.
        Returns:
        which pattern was matched (the index start with 0.) If none matched, return -1;
        Throws:
        java.io.IOException
        TimeoutException
      • stdout

        public java.lang.String stdout()
        get the standard output for current Expect session
        Returns:
      • stderr

        public java.lang.String stderr()
        get the standard error for current Expect session
        Returns:
      • before

        public java.lang.String before()
        get the output between the current matched expect() and the previous matched expect()
        Returns:
        the text before the most recent expect regex
      • matched

        public java.lang.String matched()
        get the current matched string
        Returns:
        the text matched by the most recent expect regex
      • close

        public void close()
        close this Expect session. We will auto close related objects. so there's no need to call this manually.