Package com.tailf.ned
Class SSHSession
Object
com.tailf.ned.SSHSession
- All Implemented Interfaces:
CliSession
A SSH transport.
This class uses the Ganymed SSH implementation.
(
http://www.ganymed.ethz.ch/ssh2/)
Example:
SSHConnection c = new SSHConnection("127.0.0.1", 22);
c.authenticateWithPassword("ola", "secret");
SSHSession ssh = new SSHSession(c);
-
Constructor Summary
ConstructorsConstructorDescriptionSSHSession(SSHConnection con) Constructor for SSH session object.SSHSession(SSHConnection con, int readTimeout, NedTracer tracer, NedConnectionBase conn) Constructor with an extra argument for a readTimeout timer.SSHSession(SSHConnection con, int readTimeout, NedTracer tracer, NedConnectionBase conn, int width, int height) Constructor with extra terminal width and height argumentsSSHSession(SSHConnection con, NedTracer tracer, NedConnectionBase conn) Constructor for SSH session object. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the SSH connection, including all sessions (only one in this case, compared to many in Netconf)Read from socket until Pattern is encountered.Read from socket until Pattern is encountered.Read from socket until Pattern is encountered.Read from socket until Pattern is encountered.Read from socket until Pattern is encountered.voidflush()Signals that the final chunk of data has be printed to the output transport stream.intReturn the readTimeout value that is used to read data from the ssh socket.ch.ethz.ssh2.SessionNeeded by users that need to monitor a session for EOF .ch.ethz.ssh2.ConnectionReturn the underlying ssh connection objectvoidprint(int iVal) Prints an integer (as text) to the output stream.voidPrints text to the output stream.voidprintln(int iVal) Prints an integer (as text) to the output stream.voidPrint text to the output stream.intIf we have readTimeout set, and an outstanding operation was timed out - the socket may still be alive.booleanready()Tell whether this transport is ready to be read.booleanready(int timeout) booleangiven a live SSHSession, check if the server side has closed it's end of the ssh socketvoidsetReadTimeout(int readTimeout) Set the read timeoutvoid
-
Constructor Details
-
SSHSession
Constructor for SSH session object. This method creates a a new SSh channel on top of an existing connection. SSHSession objects implement the Transport interface and they are passed into the constructor of the NetconfSession class.- Parameters:
con- an established and authenticated SSH connection- Throws:
IOException
-
SSHSession
Constructor for SSH session object. This method creates a a new SSH channel on top of an existing connection. SSHSession objects implement the Transport interface and they are passed into the constructor of the NetconfSession class.- Parameters:
con- an established and authenticated SSH connectiontracer- Ned tracerconn- NedConnection object- Throws:
IOException
-
SSHSession
public SSHSession(SSHConnection con, int readTimeout, NedTracer tracer, NedConnectionBase conn) throws IOException Constructor with an extra argument for a readTimeout timer.- Parameters:
con- an established and authenticated SSH connectionreadTimeout- timeout in millisecondstracer- Ned Tracerconn- NedConnection object- Throws:
IOException
-
SSHSession
public SSHSession(SSHConnection con, int readTimeout, NedTracer tracer, NedConnectionBase conn, int width, int height) throws IOException Constructor with extra terminal width and height arguments- Parameters:
con- an established and authenticated SSH connectionreadTimeout- timeout in millisecondstracer- Ned Tracerconn- NedConnection objectwidth- Terminal width (in characters)height- Terminal height (in characters)- Throws:
IOException
-
-
Method Details
-
getSSHConnection
public ch.ethz.ssh2.Connection getSSHConnection()Return the underlying ssh connection object -
getReadTimeout
public int getReadTimeout()Return the readTimeout value that is used to read data from the ssh socket. If a read doesn't complete within the stipulated timeout an INMException is thrown * -
setReadTimeout
public void setReadTimeout(int readTimeout) Set the read timeout- Parameters:
readTimeout- timeout in milliseconds The readTimeout parameter affects all read operations. If a timeout is reached, an INMException is thrown. The socket is not closed.
-
ready
Tell whether this transport is ready to be read.- Returns:
- true if there is something to read, false otherwise. This function can typically be used to poll a socket and see there is data to be read. The function will also return true if the server side has closed its end of the ssh socket. To explicitly just check for that, use the serverSideClosed() method.
- Throws:
IOException
-
ready
- Throws:
IOException
-
serverSideClosed
public boolean serverSideClosed()given a live SSHSession, check if the server side has closed it's end of the ssh socket- Specified by:
serverSideClosedin interfaceCliSession
-
readUntilWouldBlock
public int readUntilWouldBlock()If we have readTimeout set, and an outstanding operation was timed out - the socket may still be alive.- Returns:
- number of discarded characters
-
expectStr
public String expectStr(String str, boolean include, int timeout) throws SSHSessionException, IOException Read from socket until Pattern is encountered.- Parameters:
str- is a string which is match against each line read.include- controls if the pattern should be include in the returned string or not.- Returns:
- the characters read.
- Throws:
SSHSessionExceptionIOException
-
expect
Read from socket until Pattern is encountered.- Specified by:
expectin interfaceCliSession- Parameters:
str- is a regular expression pattern which is match against each line read.- Returns:
- the characters read.
- Throws:
SSHSessionExceptionIOException
-
expect
- Specified by:
expectin interfaceCliSession- Throws:
SSHSessionExceptionIOException
-
expect
- Specified by:
expectin interfaceCliSession- Throws:
SSHSessionExceptionIOException
-
expect
- Specified by:
expectin interfaceCliSession- Throws:
SSHSessionExceptionIOException
-
expect
Read from socket until Pattern is encountered.- Specified by:
expectin interfaceCliSession- Parameters:
str- is a regular expression pattern which is match against each line read.timeout- indicates the read timeout- Returns:
- the characters read.
- Throws:
SSHSessionExceptionIOException
-
expect
public String expect(String str, int timeout, NedWorker worker) throws SSHSessionException, IOException - Specified by:
expectin interfaceCliSession- Throws:
SSHSessionExceptionIOException
-
expect
public String expect(String str, boolean include, int timeout) throws SSHSessionException, IOException Read from socket until Pattern is encountered.- Specified by:
expectin interfaceCliSession- Parameters:
str- is a regular expression pattern which is match against each line read.include- controls if the pattern should be include in the returned string or not.- Returns:
- the characters read.
- Throws:
SSHSessionExceptionIOException
-
expect
public String expect(Pattern p, boolean include, int timeout) throws SSHSessionException, IOException - Throws:
SSHSessionExceptionIOException
-
expect
- Specified by:
expectin interfaceCliSession- Throws:
SSHSessionExceptionIOException
-
expect
- Specified by:
expectin interfaceCliSession- Throws:
SSHSessionExceptionIOException
-
expect
public NedExpectResult expect(String[] str, boolean include, int timeout) throws SSHSessionException, IOException - Specified by:
expectin interfaceCliSession- Throws:
SSHSessionExceptionIOException
-
expect
public NedExpectResult expect(Pattern[] p, boolean include, int timeout) throws SSHSessionException, IOException - Specified by:
expectin interfaceCliSession- Throws:
SSHSessionExceptionIOException
-
expect
public String expect(String str, boolean include, int timeout, NedWorker worker) throws SSHSessionException, IOException - Throws:
SSHSessionExceptionIOException
-
expect
public String expect(Pattern p, boolean include, int timeout, NedWorker worker) throws SSHSessionException, IOException - Throws:
SSHSessionExceptionIOException
-
expect
public NedExpectResult expect(String[] str, NedWorker worker) throws SSHSessionException, IOException - Specified by:
expectin interfaceCliSession- Throws:
SSHSessionExceptionIOException
-
expect
public NedExpectResult expect(Pattern[] p, NedWorker worker) throws SSHSessionException, IOException - Specified by:
expectin interfaceCliSession- Throws:
SSHSessionExceptionIOException
-
expect
public NedExpectResult expect(String[] str, boolean include, int timeout, NedWorker worker) throws SSHSessionException, IOException - Specified by:
expectin interfaceCliSession- Throws:
SSHSessionExceptionIOException
-
expect
public NedExpectResult expect(Pattern[] p, boolean include, int timeout, NedWorker worker) throws SSHSessionException, IOException - Specified by:
expectin interfaceCliSession- Throws:
SSHSessionExceptionIOException
-
expect
public NedExpectResult expect(Pattern[] p, boolean include, int timeout, boolean full) throws SSHSessionException, IOException - Throws:
SSHSessionExceptionIOException
-
expect
public NedExpectResult expect(Pattern[] p, boolean include, int timeout, boolean full, NedWorker worker) throws SSHSessionException, IOException - Throws:
SSHSessionExceptionIOException
-
expect
public String expect(String str, boolean include, boolean full, int timeout) throws IOException, SSHSessionException Read from socket until Pattern is encountered.- Specified by:
expectin interfaceCliSession- Parameters:
str- is a regular expression pattern which is match against each line read.include- controls if the pattern should be include in the returned string or not.full- controls if the pattern should be matched against the entire line, or if a positive match is accepted whenever the pattern matches any substring on a line.- Returns:
- the characters read.
- Throws:
IOExceptionSSHSessionException
-
expect
public String expect(String str, boolean include, boolean full, int timeout, NedWorker worker) throws SSHSessionException, IOException - Specified by:
expectin interfaceCliSession- Throws:
SSHSessionExceptionIOException
-
print
public void print(int iVal) Prints an integer (as text) to the output stream.- Parameters:
iVal- Text to send to the stream.
-
print
Prints text to the output stream.- Specified by:
printin interfaceCliSession- Parameters:
s- Text to send to the stream.
-
println
public void println(int iVal) Prints an integer (as text) to the output stream. A newline char is appended to end of the output stream.- Parameters:
iVal- Text to send to the stream.
-
println
Print text to the output stream. A newline char is appended to end of the output stream.- Specified by:
printlnin interfaceCliSession- Parameters:
s- Text to send to the stream.
-
flush
public void flush()Signals that the final chunk of data has be printed to the output transport stream. This method furthermore flushes the transport output stream buffer.- Specified by:
flushin interfaceCliSession
-
getSession
public ch.ethz.ssh2.Session getSession()Needed by users that need to monitor a session for EOF . This will return the underlying Ganymed SSH Session object. The ganymed Session object has a method waitForCondition() that can be used to check the connection state of an ssh socket. Assuming a A Session object s:int conditionSet = ChannelCondition.TIMEOUT ;amp ChannelCondition.CLOSED ;amp ChannelCondition.EOF; conditionSet = s.waitForCondition(conditionSet, 1); if (conditionSet != ChannelCondition.TIMEOUT) { // We know the server closed it's end of the ssh // socket -
setTracer
- Specified by:
setTracerin interfaceCliSession
-
close
public void close()Closes the SSH connection, including all sessions (only one in this case, compared to many in Netconf)- Specified by:
closein interfaceCliSession
-