SSH using Java
001 /*
002 * @(#)SshExample.java
003 *
004 * Copyright (c) 2004 JSCAPE
005 * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
006 * All rights reserved.
007 *
008 * This software is the confidential and proprietary information of
009 * JSCAPE. ("Confidential Information"). You shall not disclose such
010 * Confidential Information and shall use it only in accordance with
011 * the terms of the license agreement you entered into with JSCAPE.
012 */
013 import com.jscape.inet.ssh.*;
014 import java.io.*;
015
016 public class SshExample implements SshListener {
017
018 // state of SSH connection
019 private boolean connected = false;
020
021 /**
022 * Creates a new SshExample instance.
023 *
024 */
025 public SshExample() {
026 String hostname = null;
027 String username = null;
028 String password = null;
029 Ssh ssh = null;
030
031 try {
032 BufferedReader bin =
033 new BufferedReader(new InputStreamReader(System.in));
034 System.out.print("Enter SSH hostname: ");
035 hostname = bin.readLine();
036
037 System.out.print("Enter SSH username: ");
038 username = bin.readLine();
039
040 System.out.print("Enter SSH password: ");
041 password = bin.readLine();
042
043 // create new Ssh instance
044 ssh = new Ssh(hostname, username, password);
045
046 // register to capture events
047 ssh.addSshListener(this);
048
049 System.out.println("Connecting please wait...");
050
051
052 // connect
053 ssh.connect();
054
055 // get output stream for writing data to SSH server
056 OutputStream out = ssh.getOutputStream();
057
058 // holds line entered at console
059 String line = null;
060
061 // read data from console
062 while (connected && (line = bin.readLine()) != null) {
063 // send line with CRLF to SSH server
064 line += "\r\n";
065 try {
066 out.write(line.getBytes());
067 out.flush();
068 } catch(Exception ioe){
069 connected = false;
070 }
071 }
072 } catch (Exception e) {
073 e.printStackTrace();
074 } finally {
075 try {
076 if(connected) {
077 ssh.disconnect();
078 }
079 } catch(Exception e) {
080
081 }
082 }
083 }
084
085 /**
086 * Captures SshConnectedEvent
087 */
088 public void connected(SshConnectedEvent ev) {
089 System.out.println("Connected: " + ev.getHost());
090 connected = true;
091 }
092
093 /**
094 * Captures SshDataReceivedEvent
095 */
096 public void dataReceived(SshDataReceivedEvent ev) {
097 // send data received to console
098 System.out.print(ev.getData());
099 }
100
101 /**
102 * Captures SshDisconnectedEvent
103 */
104 public void disconnected(SshDisconnectedEvent ev) {
105 System.out.println("Disconnected: " + ev.getHost() + ". Press Enter to exit");
106 connected = false;
107 }
108
109 /**
110 * Main method for SshExample
111 * @param args
112 */
113 public static void main(String[] args) {
114 SshExample test = new SshExample();
115 }
116
117 }
1. Lines 13-14. Add necessary import statements.
2. Lines 26-28. Instance variables for SSH hostname, SSH username
and SSH password.
3. Line 29. An Ssh instance for communicating with SSH server.
4. Lines 32-33. 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.
5. Lines 34-41. Obtain SSH login information from user.
6. Line 44. Create new Ssh instance, passing SSH hostname, username
and password as arguments to constructor.
7. Line 47. Register to capture SSH related events.
8. Line 53. Establish connection with SSH server using SSH hostname,
username and password provided in constructor.
9. Line 56. Get OutputStream to send data to SSH server.
10. Lines 59-71. Read data entered from console and send to SSH
server.
11. Lines 96-99. Captures data received from SSH server and prints
to console.
In this article you learned how to create an interactive SSH session
using Java and the SSH classes provided in SSH Factory.
The SSH classes in SSH Factory for Java make this easy removing the
complexities of the SSH protocol. To see what else SSH Factory
for Java has to offer Download a
FREE 30 day evaluation of SSH Factory for Java .
|