Scripting SSH sessions using Java
|
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. |
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 password) throws 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.
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 .
|