|
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
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(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,
com.jscape.inet.ssh.SshConfiguration config)
|
|
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. |
boolean |
getDebug()
Gets whether debugging is enabled. |
java.io.PrintStream |
getDebugStream()
Gets PrintStream used in reporting debug statements. |
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. |
int |
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 |
requestPTY(java.lang.String type,
int width,
int height)
Method requestPty allocates a pseudo-terminal for the session. |
void |
requestShell()
Method requestShell request the user's default shell. |
void |
setDebug(boolean debug)
Enable debugging output to debugging stream. |
void |
setDebugStream(java.io.PrintStream debugStream)
Sets PrintStream used in reporting debug statements. |
void |
setInitReader(boolean initReader)
Sets if reader should be initialized automatically or if will be created by user. |
void |
setTerminalType(java.lang.String terminalType)
Sets terminal type emulation to use for this SSH session. |
void |
setTimeout(int 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)
sshParams -
public Ssh(java.lang.String host,
java.lang.String user)
host - SSH hostuser - SSH username
public Ssh(java.lang.String host,
int port,
java.lang.String user)
host - SSH hostport - SSH portuser - SSH username
public Ssh(java.lang.String host,
java.lang.String user,
java.lang.String password)
host - the SSH hostuser - the SSH userpassword - the SSH password
public 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 password
public Ssh(java.lang.String host,
int port,
java.lang.String user,
java.lang.String password,
com.jscape.inet.ssh.SshConfiguration config)
host - port - user - password - config - | Method Detail |
public void setInitReader(boolean initReader)
initReader - true to initialize reader, false otherwisepublic boolean isInitReader()
public void connect()
throws SshException
SshException - if an I/O error occurs.public java.io.InputStream getInputStream()
public java.io.OutputStream getOutputStream()
public void disconnect()
public boolean isConnected()
true if the client is connected;
false otherwisepublic java.lang.String getHostname()
public int getPort()
public void requestPTY(java.lang.String type,
int width,
int height)
throws SshException
type - String TERM environment variablewidth - long the terminal width in charsheight - long the terminal height in chars
SshException - if error occurs
public void requestShell()
throws SshException
SshException - if an error occurs
public void requestExec(java.lang.String command)
throws SshException
command - String command for execution.
SshException - if error occurspublic void addSshListener(SshListener listener)
listener - a SshListenerSshListenerpublic void removeSshListener(SshListener listener)
listener - a SshListenerSshListenerpublic void setDebug(boolean debug)
debug - setDebugStream(java.io.PrintStream)public boolean getDebug()
public java.io.PrintStream getDebugStream()
PrintStreampublic void setDebugStream(java.io.PrintStream debugStream)
debugStream - the PrintStream to send debug statements toPrintStreampublic void setTerminalType(java.lang.String terminalType)
terminalType - the terminal typepublic void setTimeout(int timeout)
timeout - the timeout in millisecondspublic int getTimeout()
|
SSH Factory | ||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||