Class Expect
- java.lang.Object
-
- com.logicmonitor.common.sse.utils.SSEResource
-
- com.santaba.agent.groovyapi.expect.Expect
-
public class Expect extends com.logicmonitor.common.sse.utils.SSEResourceThis 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.
1. Open an new SSH session and get our agent.conf:
2.Execute an external process to do ntp query: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())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.Stringbefore()get the output between the current matched expect() and the previous matched expect()voidclose()close this Expect session.intexitValue()voidexpect(java.lang.String pattern)reads text from the connection until it finds a match in the provided regular expressionintexpect(java.lang.String[] patterns)reads text from the connection until it finds a match in the provided regular expressionS.intexpect(java.lang.String[] patterns, int bufferSize)reads text from the connection until it finds a match in the provided regular expressions.voidexpect(java.lang.String pattern, int bufferSize)reads text from the connection until it finds a match in the provided regular expressionvoidexpectClose()Wait until close, ignoring any output from stdout or stderr if existsvoidexpectClose(ExpectedOutputHandler stdOutHandler)Wait until close, ignoring any output from stderr if existsvoidexpectClose(ExpectedOutputHandler stdOutHandler, ExpectedOutputHandler stdErrHandler)Wait until closebooleanisReleased()Indicate the current object is released or not.java.lang.Stringmatched()get the current matched stringstatic Expectopen(java.lang.String command)Start a new external processstatic Expectopen(java.lang.String command, int timeoutInSecond)Start a new processstatic Expectopen(java.lang.String host, int port, int timeoutInSeconds)Start a telnet session for remote hoststatic Expectopen(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 Expectopen(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 Expectopen(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 Expectopen(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.voidrelease()Try to release the resource of current object.
close this Expect session.voidsend(java.lang.String msg)send a string to the Expect connectionjava.lang.Stringstderr()get the standard error for current Expect sessionjava.lang.Stringstdout()get the standard output for current Expect sessionjava.lang.StringtoString()
-
-
-
Method Detail
-
isReleased
public boolean isReleased()
Indicate the current object is released or not.- Specified by:
isReleasedin classcom.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:
releasein classcom.logicmonitor.common.sse.utils.SSEResource
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.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 connectionuser- use name for the remote hostpass- 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 connectionport- the port to an ssh connectionuser- use name for the remote hostpass- 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 connectionuser- use name for the remote hostpass- password for the remote hosttimeoutInSeconds- 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 connectionport- the port to an ssh connectionuser- use name for the remote hostpass- password for the remote hosttimeoutInSeconds- 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 toport- the porttimeoutInSeconds- 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.IOExceptionsend 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, TimeoutExceptionreads 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.IOExceptionTimeoutException- timeout happened here
-
expect
public void expect(java.lang.String pattern, int bufferSize) throws java.io.IOException, TimeoutExceptionreads 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 matcjbufferSize- 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.IOExceptionTimeoutException
-
expect
public int expect(java.lang.String[] patterns) throws java.io.IOException, TimeoutExceptionreads 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.IOExceptionTimeoutException- timeout happened here
-
expect
public int expect(java.lang.String[] patterns, int bufferSize) throws java.io.IOException, TimeoutExceptionreads 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.IOExceptionTimeoutException
-
expectClose
public void expectClose() throws java.io.IOException, ExpectJException, TimeoutExceptionWait until close, ignoring any output from stdout or stderr if exists- Throws:
ExpectJExceptionTimeoutException- if the Expect session didn't close in timeout secondsjava.io.IOException
-
expectClose
public void expectClose(ExpectedOutputHandler stdOutHandler) throws java.io.IOException, ExpectJException, TimeoutException
Wait until close, ignoring any output from stderr if exists- Parameters:
stdOutHandler- - output handler for stdout if exists- Throws:
java.io.IOExceptionExpectJExceptionTimeoutException- if the Expect session didn't close in timeout seconds
-
expectClose
public void expectClose(ExpectedOutputHandler stdOutHandler, ExpectedOutputHandler stdErrHandler) throws java.io.IOException, ExpectJException, TimeoutException
Wait until close- Parameters:
stdOutHandler- - output handler for stdout if existsstdErrHandler- - output handler for stderr if exists- Throws:
java.io.IOExceptionExpectJExceptionTimeoutException- if the Expect session didn't close in timeout seconds
-
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:
-
exitValue
public int exitValue() throws ExpectJException- Returns:
- the exit code for external process
- Throws:
ExpectJException
-
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.
-
-