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
ConstructorDescriptionSSHSession
(ch.ethz.ssh2.Connection con) Deprecated.SSHSession
(ch.ethz.ssh2.Connection con, int readTimeout, NedTracer tracer, NedConnectionBase conn) Deprecated.SSHSession
(ch.ethz.ssh2.Connection con, int readTimeout, NedTracer tracer, NedConnectionBase conn, int width, int height) Deprecated.SSHSession
(ch.ethz.ssh2.Connection con, NedTracer tracer, NedConnectionBase conn) Deprecated.SSHSession
(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 TypeMethodDescriptionvoid
close()
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.void
flush()
Signals that the final chunk of data has be printed to the output transport stream.int
Return the readTimeout value that is used to read data from the ssh socket.ch.ethz.ssh2.Session
Needed by users that need to monitor a session for EOF .ch.ethz.ssh2.Connection
Return the underlying ssh connection objectvoid
print
(int iVal) Prints an integer (as text) to the output stream.void
Prints text to the output stream.void
println
(int iVal) Prints an integer (as text) to the output stream.void
Print text to the output stream.int
If we have readTimeout set, and an outstanding operation was timed out - the socket may still be alive.boolean
ready()
Tell whether this transport is ready to be read.boolean
ready
(int timeout) boolean
given a live SSHSession, check if the server side has closed it's end of the ssh socketvoid
setReadTimeout
(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
Deprecated.Note this method is deprecated, use SSHSession(SSHConnection) instead. 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
@Deprecated public SSHSession(ch.ethz.ssh2.Connection con, NedTracer tracer, NedConnectionBase conn) throws IOException Deprecated.Note this method is deprecated, use SSHSession(SSHConnection, NedTracer, NedConnection) instead. 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.- 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
@Deprecated public SSHSession(ch.ethz.ssh2.Connection con, int readTimeout, NedTracer tracer, NedConnectionBase conn) throws IOException Deprecated.Note this method is deprecated, use SSHSession(SSHConnection, int, NedTracer, NedConnection) instead. Constructor with an extra argument for a readTimeout timer.- Parameters:
con
-readTimeout
- Time to wait for read. (in milliseconds)- 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
-
SSHSession
@Deprecated public SSHSession(ch.ethz.ssh2.Connection con, int readTimeout, NedTracer tracer, NedConnectionBase conn, int width, int height) throws IOException Deprecated.Constructor with an extra argument for a readTimeout timer.- Parameters:
con
-readTimeout
- Time to wait for read. (in milliseconds)width
- 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:
serverSideClosed
in 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:
SSHSessionException
IOException
-
expect
Read from socket until Pattern is encountered.- Specified by:
expect
in interfaceCliSession
- Parameters:
Str
- is a regular expression pattern which is match against each line read.- Returns:
- the characters read.
- Throws:
SSHSessionException
IOException
-
expect
- Specified by:
expect
in interfaceCliSession
- Throws:
SSHSessionException
IOException
-
expect
- Specified by:
expect
in interfaceCliSession
- Throws:
SSHSessionException
IOException
-
expect
- Specified by:
expect
in interfaceCliSession
- Throws:
SSHSessionException
IOException
-
expect
Read from socket until Pattern is encountered.- Specified by:
expect
in 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:
SSHSessionException
IOException
-
expect
public String expect(String Str, int timeout, NedWorker worker) throws SSHSessionException, IOException - Specified by:
expect
in interfaceCliSession
- Throws:
SSHSessionException
IOException
-
expect
public String expect(String Str, boolean Include, int timeout) throws SSHSessionException, IOException Read from socket until Pattern is encountered.- Specified by:
expect
in 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:
SSHSessionException
IOException
-
expect
public String expect(Pattern p, boolean include, int timeout) throws SSHSessionException, IOException - Throws:
SSHSessionException
IOException
-
expect
- Specified by:
expect
in interfaceCliSession
- Throws:
SSHSessionException
IOException
-
expect
- Specified by:
expect
in interfaceCliSession
- Throws:
SSHSessionException
IOException
-
expect
public NedExpectResult expect(String[] Str, boolean include, int timeout) throws SSHSessionException, IOException - Specified by:
expect
in interfaceCliSession
- Throws:
SSHSessionException
IOException
-
expect
public NedExpectResult expect(Pattern[] p, boolean include, int timeout) throws SSHSessionException, IOException - Specified by:
expect
in interfaceCliSession
- Throws:
SSHSessionException
IOException
-
expect
public String expect(String Str, boolean Include, int timeout, NedWorker worker) throws SSHSessionException, IOException - Throws:
SSHSessionException
IOException
-
expect
public String expect(Pattern p, boolean include, int timeout, NedWorker worker) throws SSHSessionException, IOException - Throws:
SSHSessionException
IOException
-
expect
public NedExpectResult expect(String[] Str, NedWorker worker) throws SSHSessionException, IOException - Specified by:
expect
in interfaceCliSession
- Throws:
SSHSessionException
IOException
-
expect
public NedExpectResult expect(Pattern[] p, NedWorker worker) throws SSHSessionException, IOException - Specified by:
expect
in interfaceCliSession
- Throws:
SSHSessionException
IOException
-
expect
public NedExpectResult expect(String[] Str, boolean include, int timeout, NedWorker worker) throws SSHSessionException, IOException - Specified by:
expect
in interfaceCliSession
- Throws:
SSHSessionException
IOException
-
expect
public NedExpectResult expect(Pattern[] p, boolean include, int timeout, NedWorker worker) throws SSHSessionException, IOException - Specified by:
expect
in interfaceCliSession
- Throws:
SSHSessionException
IOException
-
expect
public NedExpectResult expect(Pattern[] p, boolean include, int timeout, boolean full) throws SSHSessionException, IOException - Throws:
SSHSessionException
IOException
-
expect
public NedExpectResult expect(Pattern[] p, boolean include, int timeout, boolean full, NedWorker worker) throws SSHSessionException, IOException - Throws:
SSHSessionException
IOException
-
expect
public String expect(String str, boolean include, boolean full, int timeout) throws IOException, SSHSessionException Read from socket until Pattern is encountered.- Specified by:
expect
in 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:
IOException
SSHSessionException
-
expect
public String expect(String str, boolean include, boolean full, int timeout, NedWorker worker) throws SSHSessionException, IOException - Specified by:
expect
in interfaceCliSession
- Throws:
SSHSessionException
IOException
-
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:
print
in 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:
println
in 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:
flush
in 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:
setTracer
in interfaceCliSession
-
close
public void close()Closes the SSH connection, including all sessions (only one in this case, compared to many in Netconf)- Specified by:
close
in interfaceCliSession
-