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

iTunes Gift Card Offer

For a limited time get a $50.00 iTunes gift card free with qualifying purchase.

Click for details.


News

JSCAPE Secure FTP Server 7.0 Released
08/20/2010 03:31 AM

JSCAPE launches Employee Giving Program
07/03/2010 08:26 AM

SSH Factory 3.6 Released
07/03/2010 06:22 AM

Secure iNet Factory 8.5 Released
07/03/2010 06:07 AM

Secure FTP Factory 8.5 Released
07/03/2010 06:03 AM

Secure FTP Applet 6.2 Released
06/10/2010 02:23 PM

more...


Tutorials

Enabling Phone Authentication
04/08/2009 11:24 AM

Detecting and Handling Brute Force Password Attacks
01/29/2009 09:44 AM

Creating a Domain
12/15/2008 11:33 AM

Public key authentication with SFTP
10/02/2008 07:59 AM

Formatting MimeMessages using .NET
09/14/2008 04:31 PM

Communicating with an IMAP4 server in .NET
09/14/2008 03:54 PM

more...


Articles

Open up corporate data to your partners
08/03/2010 10:01 PM

Access vital corporate documents on the go
07/01/2010 02:15 PM

SFTP and Encryption
05/17/2010 09:52 PM

Streamlining web uploads with ZIP archives
12/14/2009 10:11 AM

Using regular expressions in complex trigger conditions
09/08/2009 07:42 AM

Using custom forms to automate business processes
07/03/2009 08:51 AM

more...


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 .