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

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


 

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 .