Java and .NET components, FTP, TELNET, SMTP, POP3, IMAP, HTTP, SSH
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 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

JSCAPE Secure FTP Server 6.6 Released
06/10/2010 02:03 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

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

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

more...


Feedback

Request a feature or component

Request a Java or .NET component


 

Scripting SSH sessions using Java

Overview

This article will demonstrate how using the SSH classes in SSH Factory for Java you can script an SSH session for automation purposes. This example will prompt the user for connection details, establish a connection to the server, issue a directory listing command printing the directory listing to the console and finally disconnecting.

As a prerequisite to this article you may wish to read the article titled SSH using Java for a brief introduction to use the SSH Factory for Java classes in an interactive environment. To see what else SSH Factory for Java has to offer Download a FREE 30 day SSH Factory for Java Evaluation.


Example

01 /*
02  * @(#)SshSessionExample.java
03  *
04  */
05 import java.io.*;
06 
07 import com.jscape.inet.ssh.*;
08 import com.jscape.inet.ssh.util.SshParameters;
09 
10 public class SshSessionExample extends SshAdapter {
11   
12   // define SSH prompts .. these must match exactly those provided by SSH server
13   private String shellPrompt = "$";
14   
15   // variable for connection info
16   private String hostname;
17   private String username;
18   private String password;
19   private SshSession session;
20   
21   /**
22    * Constructs a new SshSessionExample instance
23    @param hostname the SSH hostname
24    @param username the SSH username
25    @param password the SSH password
26    */
27   public SshSessionExample(String hostname, String username, String passwordthrows SshException {
28     this.hostname = hostname;
29     this.username = username;
30     this.password = password;
31     SshParameters sshParams = new SshParameters(hostname,username,password);
32     session = new SshSession(sshParams);
33     session.setShellPrompt(shellPrompt);
34     session.addSshListener(this);
35   }
36   
37   /**
38    * Connects to server, logs in and performs UNIX directory listing command
39    @throws TelnetException
40    */
41   public void printDirListing() throws SshException {
42     session.connect();
43     String dirListing = session.send("ls -l");
44     System.out.println(dirListing);
45     session.disconnect();    
46   }
47   
48   public void connected(SshConnectedEvent evt) {
49     System.out.println("Connected to host: " + evt.getHost());
50   }
51   
52   public void disconnected(SshDisconnectedEvent evt) {
53     System.out.println("Disconnected from host: " + evt.getHost());
54   }
55   
56   /**
57    * Runs example
58    @param args
59    */
60   public static void main(String[] args) {
61     try {
62       String tmpHost = null;
63       String tmpUser = null;
64       String tmpPass = null;
65       BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
66       System.out.print("Hostname: ");
67       tmpHost = reader.readLine();
68       System.out.print("Username: ");
69       tmpUser = reader.readLine();
70       System.out.print("Password: ");
71       tmpPass = reader.readLine();
72       SshSessionExample ss = new SshSessionExample(tmpHost,tmpUser,tmpPass);
73       ss.printDirListing();          
74     catch(Exception e) {
75       e.printStackTrace();
76     }
77     
78   }
79 
80 }

1. Lines 5-8. Add necessary import statements.
2. Line 13. Define remote server shell prompt. This should match EXACTLY.
3. Lines 16-19. Define variables for holding connection properties.
4. Lines 28-31. Set variables in constructor and create a new SshParameters instance to hold connection properties.
5. Line 32. Create new SshSession instance.
6. Line 33. Set expected shell prompt for SshSession.
7. Line 34. Add an SshListener in order to capture events.
8. Line 42. Establishes a connection to SSH server and waits for expected shell prompt.
9. Line 43. Sends ls -l command to server and stores in local variable dirListing.
10. Line 44. Prints out dirListing variable.
11. Line 45. Disconnects from SSH server.
12. Lines 48-50. Captures event fired when connection to SSH server is established.
13. Lines 52-54. Captures event fired when connection to SSH server is closed.
14. Lines 60-78. main method for running this program and prompting user for connection details.

Summary

In this article you learned how to create an automated 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 .