|
SSH Factory | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.jscape.inet.ssh.Ssh
public class Ssh
Implements the basic functionality of a SSH2 (Secure Shell) client.
Example Usage:
public class SshExample implements SshListener { // state of SSH connection private boolean connected = false; public SshExample() { String hostname = null; String username = null; String password = null; Ssh ssh = null; try { BufferedReader bin = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter SSH hostname: "); hostname = bin.readLine(); System.out.print("Enter SSH username: "); username = bin.readLine(); System.out.print("Enter SSH password: "); password = bin.readLine(); // create new SshParameters instance SshParameters sshParams = new SshParameters(hostname,username,password); // create new Ssh instance ssh = new Ssh(sshParams); // register to capture events ssh.addSshListener(this); System.out.println("Connecting please wait..."); // connect ssh.connect(); // get output stream for writing data to SSH server OutputStream out = ssh.getOutputStream(); // holds line entered at console String line = null; // read data from console while (connected && (line = bin.readLine()) != null) { // send line with LF to SSH server line += "\n"; try { out.write(line.getBytes()); out.flush(); } catch(Exception ioe){ connected = false; } } } catch (Exception e) { e.printStackTrace(); } finally { try { if(connected) { ssh.disconnect(); } } catch(Exception e) { } } } // captures SshConnectedEvent public void connected(SshConnectedEvent ev) { System.out.println("Connected: " + ev.getHost()); connected = true; } // captures SshDataReceivedEvent public void dataReceived(SshDataReceivedEvent ev) { // send data received to console System.out.print(ev.getData()); } // captures SshDisconnectedEvent event public void disconnected(SshDisconnectedEvent ev) { System.out.println("Disconnected: " + ev.getHost() + ". Press Enter to exit"); connected = false; } }
Constructor Summary | |
---|---|
Ssh(SshParameters sshParams)
Constructs a new Ssh instance |
|
Ssh(SshParameters sshParams,
SshConfiguration config)
Constructs a new Ssh instance. |
|
Ssh(java.lang.String host,
int port,
java.lang.String user)
Constructs a new Ssh instance. |
|
Ssh(java.lang.String host,
int port,
java.lang.String user,
java.lang.String password)
Constructs a new Ssh instance. |
|
Ssh(java.lang.String host,
int port,
java.lang.String user,
java.lang.String password,
SshConfiguration config)
Constructs a new Ssh instance. |
|
Ssh(java.lang.String host,
java.lang.String user)
Constructs a new Ssh instance. |
|
Ssh(java.lang.String host,
java.lang.String user,
java.lang.String password)
Consructs a new Ssh instance. |
Method Summary | |
---|---|
void |
addSshListener(SshListener listener)
Add SSH event listener. |
void |
connect()
Connects to SSH server. |
void |
disconnect()
Closes open connection. |
java.lang.String |
getCharacterSet()
Gets the character set |
boolean |
getDebug()
Gets whether debugging is enabled. |
java.io.PrintStream |
getDebugStream()
Gets PrintStream used in reporting debug statements. |
boolean |
getEcho()
Returns echo flag. |
SshHostKeys |
getHostKeys()
Returns SSH host keys for further use. |
java.lang.String |
getHostname()
Return server hostname. |
java.io.InputStream |
getInputStream()
Returns connection input stream. |
java.io.OutputStream |
getOutputStream()
Returns connection output stream. |
int |
getPort()
Return server port. |
long |
getReadTimeout()
Returns current read timeout value. |
long |
getTimeout()
Gets the timeout for opening connection to SSH server. |
boolean |
isConnected()
Checks if the current client is connected. |
boolean |
isInitReader()
Gets if reader should be initialized automatically or if will be created by user. |
void |
removeSshListener(SshListener listener)
Remove SSH event listener. |
void |
requestExec(java.lang.String command)
Method requestExec request the SSH server to start the execution of the given command. |
void |
requestTerminalWindowChange(int widthChars,
int heightChars,
int widthPixels,
int heightPixels)
Requests terminal window change on the server side. |
void |
setCharacterSet(java.lang.String encoding)
Sets character set used when reading data using ssh |
void |
setDebug(boolean debug)
Enable debugging output to debugging stream. |
void |
setDebugStream(java.io.PrintStream debugStream)
Sets PrintStream used in reporting debug statements. |
void |
setEcho(boolean echo)
Sets server echo flag for the next connection. |
void |
setInitReader(boolean initReader)
Sets if reader should be initialized automatically or if will be created by user. |
void |
setReadTimeout(long readTimeout)
Sets read timeout value. |
void |
setTerminalType(java.lang.String terminalType)
Sets terminal type emulation to use for this SSH session. |
void |
setTimeout(long timeout)
Sets the timeout for opening connection to SSH server. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int MSG_DISCONNECT
public static final int MSG_IGNORE
public static final int MSG_UNIMPLEMENTED
public static final int MSG_DEBUG
public static final int MSG_SERVICE_REQUEST
public static final int MSG_SERVICE_ACCEPT
public static final int MSG_KEXINIT
public static final int MSG_NEWKEYS
public static final int MSG_KEXDH_INIT
public static final int MSG_KEXDH_REPLY
public static final int MSG_USERAUTH_REQUEST
public static final int MSG_USERAUTH_FAILURE
public static final int MSG_USERAUTH_SUCCESS
public static final int MSG_USERAUTH_BANNER
public static final int MSG_USERAUTH_PK_OK
public static final int MSG_GLOBAL_REQUEST
public static final int MSG_REQUEST_SUCCESS
public static final int MSG_REQUEST_FAILURE
public static final int MSG_CHANNEL_OPEN
public static final int MSG_CHANNEL_OPEN_CONFIRMATION
public static final int MSG_CHANNEL_OPEN_FAILURE
public static final int MSG_CHANNEL_WINDOW_ADJUST
public static final int MSG_CHANNEL_DATA
public static final int MSG_CHANNEL_EXTENDED_DATA
public static final int MSG_CHANNEL_EOF
public static final int MSG_CHANNEL_CLOSE
public static final int MSG_CHANNEL_REQUEST
public static final int MSG_CHANNEL_SUCCESS
public static final int MSG_CHANNEL_FAILURE
public static final int DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT
public static final int DISCONNECT_PROTOCOL_ERROR
public static final int DISCONNECT_KEY_EXCHANGE_FAILED
public static final int DISCONNECT_RESERVED
public static final int DISCONNECT_MAC_ERROR
public static final int DISCONNECT_COMPRESSION_ERROR
public static final int DISCONNECT_SERVICE_NOT_AVAILABLE
public static final int DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED
public static final int DISCONNECT_HOST_KEY_NOT_VERIFIABLE
public static final int DISCONNECT_CONNECTION_LOST
public static final int DISCONNECT_BY_APPLICATION
public static final int DISCONNECT_TOO_MANY_CONNECTIONS
public static final int DISCONNECT_AUTH_CANCELLED_BY_USER
public static final int DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE
public static final int DISCONNECT_ILLEGAL_USER_NAME
public static final java.lang.String SERVICE_USERAUTH
public static final java.lang.String SERVICE_CONNECTION
public static final java.lang.String AUTH_METHOD_KBI
public static final java.lang.String AUTH_METHOD_PUBLICKEY
public static final java.lang.String AUTH_METHOD_PASSWORD
public static final java.lang.String AUTH_METHOD_HOSTBASE
public static final java.lang.String AUTH_METHOD_NONE
public static final java.lang.String CHANNEL_TYPE_SESSION
public static final java.lang.String CHANNEL_TYPE_X11
public static final java.lang.String CHANNEL_TYPE_FORWARDED_TCPIP
public static final java.lang.String CHANNEL_TYPE_DIRECT_TCPIP
public static final java.lang.String PROTOCOL_REQUEST_TCPIP_FORWARD
public static final java.lang.String PROTOCOL_REQUEST_TCPIP_FORWARD_CANCEL
public static final java.lang.String CHANNEL_REQUEST_PTY_REQ
public static final java.lang.String CHANNEL_REQUEST_X11_REQ
public static final java.lang.String CHANNEL_REQUEST_ENV
public static final java.lang.String CHANNEL_REQUEST_SHELL
public static final java.lang.String CHANNEL_REQUEST_EXEC
public static final java.lang.String CHANNEL_REQUEST_SUBSYSTEM
public static final java.lang.String CHANNEL_REQUEST_WINDOW_CHANGE
public static final java.lang.String CHANNEL_REQUEST_XON_XOFF
public static final java.lang.String CHANNEL_REQUEST_SIGNAL
public static final java.lang.String CHANNEL_REQUEST_EXIT_STATUS
public static final java.lang.String CHANNEL_REQUEST_EXIT_SIGNAL
public static final java.lang.String KEY_EXCHANGE_DIFFIE_HELLMAN
public static final java.lang.String ENCRYPTION_ALG_3DES_CBC
public static final java.lang.String ENCRYPTION_ALG_BLOWFISH_CBC
public static final java.lang.String ENCRYPTION_ALG_TWOFISH256_CBC
public static final java.lang.String ENCRYPTION_ALG_TWOFISH_CBC
public static final java.lang.String ENCRYPTION_ALG_TWOFISH192_CBC
public static final java.lang.String ENCRYPTION_ALG_TWOFISH128_CBC
public static final java.lang.String ENCRYPTION_ALG_AES256_CBC
public static final java.lang.String ENCRYPTION_ALG_AES192_CBC
public static final java.lang.String ENCRYPTION_ALG_AES128_CBC
public static final java.lang.String ENCRYPTION_ALG_SERPENT256_CBC
public static final java.lang.String ENCRYPTION_ALG_SERPENT192_CBC
public static final java.lang.String ENCRYPTION_ALG_SERPENT128_CBC
public static final java.lang.String ENCRYPTION_ALG_ARCFOUR
public static final java.lang.String ENCRYPTION_ALG_IDEA_CBC
public static final java.lang.String ENCRYPTION_ALG_CAST128_CBC
public static final java.lang.String ENCRYPTION_ALG_NONE
public static final java.lang.String ENCRYPTION_ALG_DES_CBC
public static final java.lang.String MAC_ALG_HMAC_SHA1
public static final java.lang.String MAC_ALG_HMAC_SHA1_96
public static final java.lang.String MAC_ALG_HMAC_MD5
public static final java.lang.String MAC_ALG_HMAC_MD5_96
public static final java.lang.String MAC_ALG_NONE
public static final java.lang.String PUB_KEY_ALG_SSH_DSS
public static final java.lang.String PUB_KEY_ALG_SSH_RSA
public static final java.lang.String PUB_KEY_ALG_X509V3_SIGN_RSA
public static final java.lang.String PUB_KEY_ALG_X509V3_SIGN_DSS
public static final java.lang.String PUB_KEY_ALG_SPKI_SIGN_RSA
public static final java.lang.String PUB_KEY_ALG_SPKI_SIGN_DSS
public static final java.lang.String PUB_KEY_ALG_PGP_SIGN_RSA
public static final java.lang.String PUB_KEY_ALG_PGP_SIGN_DSS
public static final java.lang.String COMPRESSION_ALG_NONE
public static final java.lang.String COMPRESSION_ALG_ZLIB
public static final java.lang.String DEFAULT_TYPE
Constructor Detail |
---|
public Ssh(SshParameters sshParams, SshConfiguration config)
sshParams
- the SSH connection parametersconfig
- the SSH protocol configurationpublic Ssh(SshParameters sshParams)
sshParams
- SSH connection parameterspublic Ssh(java.lang.String host, int port, java.lang.String user, java.lang.String password, SshConfiguration config)
host
- the SSH server hostnameport
- the SSH server portuser
- the SSH server usernamepassword
- the SSH server passwordconfig
- the SSH protocol configurationpublic Ssh(java.lang.String host, int port, java.lang.String user, java.lang.String password)
host
- the SSH server hostnameport
- the SSH server portuser
- the SSH server usernamepassword
- the SSH server passwordpublic Ssh(java.lang.String host, int port, java.lang.String user)
host
- SSH hostport
- SSH portuser
- SSH usernamepublic Ssh(java.lang.String host, java.lang.String user, java.lang.String password)
host
- the SSH hostuser
- the SSH userpassword
- the SSH passwordpublic Ssh(java.lang.String host, java.lang.String user)
host
- SSH hostuser
- SSH usernameMethod Detail |
---|
public java.lang.String getHostname()
public int getPort()
public boolean isInitReader()
connect()
.
public void setInitReader(boolean initReader)
connect()
.
initReader
- true to initialize reader, false otherwisepublic void addSshListener(SshListener listener)
listener
- a SshListenerSshListener
public void removeSshListener(SshListener listener)
listener
- a SshListenerSshListener
public void setDebug(boolean debug)
System.out
.
debug
- true
if debugging is enabledsetDebugStream(java.io.PrintStream)
public boolean getDebug()
System.out
.
true
if debugging is enabled, false
otherwisesetDebugStream(PrintStream)
public java.io.PrintStream getDebugStream()
PrintStream
used in reporting debug statements. Default PrintStream
is System.out
.
PrintStream
used in reporting debug statementsPrintStream
public void setDebugStream(java.io.PrintStream debugStream)
PrintStream
used in reporting debug statements. Default PrintStream
is System.out
.
debugStream
- the PrintStream
to send debug statements toPrintStream
public void setTerminalType(java.lang.String terminalType)
terminalType
- the terminal typepublic boolean getEcho()
true
if server echo is enabled;
false
otherwisepublic void setEcho(boolean echo)
echo
- if true
server echo will
be enabled for the next connection.public long getTimeout()
public void setTimeout(long timeout)
timeout
- the timeout in millisecondspublic long getReadTimeout()
public void setReadTimeout(long readTimeout)
readTimeout
- read timeout value in milliseconds.public SshHostKeys getHostKeys()
null
if keys are not availablepublic void setCharacterSet(java.lang.String encoding)
encoding
- the encoding namepublic java.lang.String getCharacterSet()
public boolean isConnected()
true
if the client is connected;
false
otherwisepublic void connect() throws SshException
SshException
- if an I/O error occurs.public void disconnect()
public java.io.InputStream getInputStream()
public java.io.OutputStream getOutputStream()
public void requestExec(java.lang.String command) throws SshException
command
- String command for execution.
SshException
- if error occurspublic void requestTerminalWindowChange(int widthChars, int heightChars, int widthPixels, int heightPixels) throws SshException
widthChars
- window width in charsheightChars
- window heigth in charswidthPixels
- window width in pixelsheightPixels
- window height in pixels
SshException
- if an error occurs
|
SSH Factory | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |