public class Ssh
extends java.lang.Object
implements java.lang.AutoCloseable
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); // set key exchanges, ciphers, macs and compressions if needed SshConfiguration sshConfiguration = SshConfiguration.configurationFor(new ComponentClientKeyExchangeFactory(ClientKeyExchanges.DH_GROUP_SHA256, ClientKeyExchanges.DH_GROUP_SHA1), new JceEncryptionFactory(JceEncryptions.AES256_CBC, JceEncryptions.BLOWFISH_CBC), new JceMacFactory(JceMacs.HMAC_SHA2_512, JceMacs.HMAC_SHA1), new ComponentCompressionFactory(Compressions.NONE)); // create new Ssh instance ssh = new Ssh(sshParams, sshConfiguration); // 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 and Description |
---|
Ssh(SshParameters parameters)
Constructs a new Ssh instance
|
Ssh(SshParameters parameters,
SshConfiguration configuration)
Constructs a new Ssh instance.
|
Ssh(SshParameters parameters,
SshConfiguration configuration,
java.util.logging.Logger logger)
Constructs a new Ssh instance.
|
Ssh(SshParameters parameters,
SshConfiguration configuration,
java.lang.String terminalType,
int terminalWidthCharacters,
int terminalHeightCharacters,
int terminalWidthPixels,
int terminalHeightPixels,
boolean echoModeRequired,
boolean readerThreadRequired,
java.lang.String characterSet,
java.util.logging.Logger logger)
Creates a new Ssh client 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 configuration)
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.
|
Modifier and Type | Method and Description |
---|---|
void |
addSshListener(SshListener listener)
Add SSH event listener.
|
void |
close() |
Ssh |
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.net.InetSocketAddress |
getLocalSocketAddress()
Returns local socket address.
|
java.util.logging.Level |
getLogLevel()
Returns current log level.
|
java.io.OutputStream |
getOutputStream()
Returns connection output stream.
|
int |
getPort()
Return server port.
|
long |
getReadTimeout()
Returns current read timeout value.
|
java.net.InetSocketAddress |
getRemoteSocketAddress()
Returns remote socket address.
|
java.lang.String |
getTerminalType()
Sets the terminal type.
|
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 value)
Sets character set used when reading data using ssh
|
void |
setDebug(boolean value)
Enable debugging output to debugging stream.
|
void |
setDebugStream(java.io.PrintStream value)
Sets
PrintStream used in reporting debug statements. |
void |
setEcho(boolean value)
Sets server echo flag for the next connection.
|
void |
setInitReader(boolean value)
Sets if reader should be initialized automatically or if will be created by user.
|
void |
setLogLevel(java.util.logging.Level level)
Sets current log level.
|
void |
setReadTimeout(long value)
Sets read timeout value.
|
void |
setTerminalType(java.lang.String value)
Sets terminal type emulation to use for this SSH session.
|
void |
setTimeout(long value)
Sets the timeout for opening connection to SSH server.
|
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_TERMINAL_TYPE
public Ssh(SshParameters parameters, SshConfiguration configuration, java.lang.String terminalType, int terminalWidthCharacters, int terminalHeightCharacters, int terminalWidthPixels, int terminalHeightPixels, boolean echoModeRequired, boolean readerThreadRequired, java.lang.String characterSet, java.util.logging.Logger logger)
parameters
- SSH connection parametersconfiguration
- SSH configurationterminalType
- target terminal typeterminalWidthCharacters
- terminal width in charactersterminalHeightCharacters
- terminal height in charactersterminalWidthPixels
- terminal width in pixelsterminalHeightPixels
- terminal height in pixelsechoModeRequired
- echo mode flagreaderThreadRequired
- separated reader thread flagcharacterSet
- character setlogger
- loggerpublic Ssh(SshParameters parameters, SshConfiguration configuration, java.util.logging.Logger logger)
parameters
- the SSH connection parametersconfiguration
- the SSH protocol configurationlogger
- the loggerpublic Ssh(SshParameters parameters, SshConfiguration configuration)
parameters
- the SSH connection parametersconfiguration
- the SSH protocol configurationpublic Ssh(SshParameters parameters)
parameters
- SSH connection parameterspublic Ssh(java.lang.String host, int port, java.lang.String user, java.lang.String password, SshConfiguration configuration)
host
- the SSH server hostnameport
- the SSH server portuser
- the SSH server usernamepassword
- the SSH server passwordconfiguration
- 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 usernamepublic java.lang.String getHostname()
public int getPort()
public long getTimeout()
public void setTimeout(long value)
value
- the timeout in millisecondspublic long getReadTimeout()
public void setReadTimeout(long value)
value
- read timeout value in milliseconds.public void setCharacterSet(java.lang.String value)
value
- the encoding namepublic java.lang.String getCharacterSet()
public java.lang.String getTerminalType()
public void setTerminalType(java.lang.String value)
value
- the terminal typepublic boolean getEcho()
true
if server echo is enabled;
false
otherwisepublic void setEcho(boolean value)
value
- if true
server echo will
be enabled for the next connection.public boolean isInitReader()
connect()
.public void setInitReader(boolean value)
connect()
.value
- true to initialize reader, false otherwisepublic java.util.logging.Level getLogLevel()
public void setLogLevel(java.util.logging.Level level)
level
- new log levelpublic boolean getDebug()
System.out
.true
if debugging is enabled, false
otherwisesetDebugStream(PrintStream)
public void setDebug(boolean value)
System.out
.value
- true
if debugging is enabledsetDebugStream(java.io.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 value)
PrintStream
used in reporting debug statements. Default PrintStream
is System.out
.value
- the PrintStream
to send debug statements toPrintStream
public void addSshListener(SshListener listener)
listener
- a SshListenerSshListener
public void removeSshListener(SshListener listener)
listener
- a SshListenerSshListener
public SshHostKeys getHostKeys()
public Ssh connect() throws SshException
SshException
- if an I/O error occurs.public void disconnect()
public void close()
close
in interface java.lang.AutoCloseable
public boolean isConnected()
true
if the client is connected;
false
otherwisepublic java.net.InetSocketAddress getLocalSocketAddress()
public java.net.InetSocketAddress getRemoteSocketAddress()
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 pixelsSshException
- if an error occursCopyright © JSCAPE LLC. 1999-2021. All Rights Reserved