1
import java.io.BufferedReader;
2
import java.io.IOException;
3
import java.io.InputStream;
4
import java.io.InputStreamReader;
6
import com.trilead.ssh2.Connection;
7
import com.trilead.ssh2.Session;
8
import com.trilead.ssh2.StreamGobbler;
12
* This is a very basic example that shows
13
* how one can login to a machine and execute a command.
15
* @author Christian Plattner, plattner@trilead.com
16
* @version $Id: Basic.java,v 1.4 2007/10/15 12:49:57 cplattne Exp $
20
public static void main(String[] args)
22
String hostname = "127.0.0.1";
23
String username = "joe";
24
String password = "joespass";
28
/* Create a connection instance */
30
Connection conn = new Connection(hostname);
37
* If you get an IOException saying something like
38
* "Authentication method password not supported by the server at this stage."
39
* then please check the FAQ.
42
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
44
if (isAuthenticated == false)
45
throw new IOException("Authentication failed.");
47
/* Create a session */
49
Session sess = conn.openSession();
51
sess.execCommand("uname -a && date && uptime && who");
53
System.out.println("Here is some information about the remote host:");
56
* This basic example does not handle stderr, which is sometimes dangerous
57
* (please read the FAQ).
60
InputStream stdout = new StreamGobbler(sess.getStdout());
62
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
66
String line = br.readLine();
69
System.out.println(line);
72
/* Show exit status, if available (otherwise "null") */
74
System.out.println("ExitCode: " + sess.getExitStatus());
76
/* Close this session */
80
/* Close the connection */
87
e.printStackTrace(System.err);