Java SSH - ssh java, java telnet, rexec, rsh, rlogin
Home Search Knowledge Base Support

Support

Click here for access to live sales support.

For technical support please submit a ticket to the Help Desk.

 

Java and .NET Help

iPod shuffle offer

For a limited time get the newly re-designed iPod shuffle free with qualifying purchase.

Click for details.


News

Secure iNet Factory 8.0 Released
04/04/2008 02:24 PM

Updates to Email Factory for .NET and Secure iNet Factory
03/29/2008 04:06 PM

JSCAPE Secure FTP Server 3.9 Preview
03/14/2008 12:19 PM

AnyClient Service and Application Launched
03/12/2008 03:41 PM

JSCAPE Secure FTP Server 3.8 Released
02/12/2008 10:50 AM


Tutorials

Email Validation with Java
04/15/2008 02:04 PM

Sending HTML Based Email Using Java
03/11/2008 02:47 PM

Secure FTP Using Java and FTPS (FTP over SSL)
03/10/2008 04:08 PM

FTP Directory Listing Using Java
03/10/2008 03:57 PM

Sending Email Using Java
03/09/2008 03:43 PM

SSH Using Java
03/09/2008 02:53 PM


Articles

DMZ File Transfer Streaming
03/28/2008 11:57 AM

Phishing looks to FTP to distribute malware
03/13/2008 05:14 PM

Ad Hoc File Transfer Explained
03/13/2008 09:16 AM

Password Policies Made Easy
03/12/2008 03:03 PM


Feedback

Request a feature or component

Request a Java or .NET component


 

SSH using Java

Overview

This article will demonstrate how using the SSH classes in SSH Factory for Java you can develop a console based interactive SSH shell. This article is a prerequisite to the article titled Scripting SSH sessions using Java which demonstrates how to automate the execution of commands thru SSH.

To see what else SSH Factory for Java has to offer Download a FREE 30 day SSH Factory for Java Evaluation.


Example

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.

Summary

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 .