SSH Using Java
Overview
This article will demonstrate how using the SSH API in SSH Factory for Java you can develop a console based interactive SSH shell.
Code Example
Download SshExample.java (2.7K)
001 /*
002 * @(#)SshExample.java
003 *
004 * Copyright (c) JSCAPE LLC.
005 *
006 * This software is the confidential and proprietary information of
007 * JSCAPE. ("Confidential Information"). You shall not disclose such
008 * Confidential Information and shall use it only in accordance with
009 * the terms of the license agreement you entered into with JSCAPE.
010 */
011 import com.jscape.inet.ssh.*;
012 import com.jscape.inet.ssh.util.SshParameters;
013 import java.io.*;
014
015 public class SshExample implements SshListener {
016
017 // state of SSH connection
018 private boolean connected = false;
019
020 /**
021 * Creates a new SshExample instance.
022 *
023 */
024 public SshExample() {
025 String hostname = null;
026 String username = null;
027 String password = null;
028 Ssh ssh = null;
029
030 try {
031 BufferedReader bin =
032 new BufferedReader(new InputStreamReader(System.in));
033 System.out.print("Enter SSH hostname: ");
034 hostname = bin.readLine();
035
036 System.out.print("Enter SSH username: ");
037 username = bin.readLine();
038
039 System.out.print("Enter SSH password: ");
040 password = bin.readLine();
041
042 // create new Ssh instance
043 SshParameters sshParams = new SshParameters(hostname,username,password);
044 ssh = new Ssh(sshParams);
045
046 // register to capture events
047 ssh.addSshListener(this);
048
049 System.out.println("Connecting please wait...");
050
051 // connect
052 ssh.connect();
053
054 // get output stream for writing data to SSH server
055 OutputStream out = ssh.getOutputStream();
056
057 // holds line entered at console
058 String line = null;
059
060 // read data from console
061 while (connected && (line = bin.readLine()) != null) {
062 // send line with LF to SSH server
063 line += "\n";
064 try {
065 out.write(line.getBytes());
066 out.flush();
067 } catch(Exception ioe){
068 connected = false;
069 }
070 }
071 } catch (Exception e) {
072 e.printStackTrace();
073 } finally {
074 try {
075 if(connected) {
076 ssh.disconnect();
077 }
078 } catch(Exception e) {
079
080 }
081 }
082 }
083
084 /**
085 * Captures SshConnectedEvent
086 */
087 public void connected(SshConnectedEvent ev) {
088 System.out.println("Connected: " + ev.getHost());
089 connected = true;
090 }
091
092 /**
093 * Captures SshDataReceivedEvent
094 */
095 public void dataReceived(SshDataReceivedEvent ev) {
096 // send data received to console
097 System.out.print(ev.getData());
098 }
099
100 /**
101 * Captures SshDisconnectedEvent
102 */
103 public void disconnected(SshDisconnectedEvent ev) {
104 System.out.println("Disconnected: " + ev.getHost() + ". Press Enter to exit");
105 connected = false;
106 }
107
108 /**
109 * Main method for SshExample
110 * @param args
111 */
112 public static void main(String[] args) {
113 SshExample test = new SshExample();
114 }
115
116 }
- Lines 11-13. Add necessary import statements.
- Lines 25-27. Instance variables for SSH hostname, SSH username and SSH password.
- Line 28. An Ssh instance for communicating with SSH server.
- Lines 31-32. Creates a BufferedReader for obtaining SSH login information from user. BufferedReader will also be used to read data from console and submit to SSH server.
- Lines 33-40. Obtain SSH login information from user.
- Line 43. Create new Ssh instance, passing SSH hostname, username and password as arguments to constructor.
- Line 46. Register to capture SSH related events.
- Line 52. Establish connection with SSH server using SSH hostname, username and password provided in constructor.
- Line 55. Get OutputStream to send data to SSH server.
- Lines 58-70. Read data entered from console and send to SSH server.
- Lines 95-98. Captures data received from SSH server and prints to console.
Comments