~ubuntu-branches/ubuntu/oneiric/trilead-ssh2/oneiric

« back to all changes in this revision

Viewing changes to examples/Basic.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthew Johnson
  • Date: 2010-02-20 13:52:54 UTC
  • mfrom: (2.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20100220135254-xf16zql13loa8e1h
Tags: 6401-1
* Switch upstream to svnkit
* New upstream release
* Version is svn revision from svnkit
* Import readme, faq and examples from old upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import java.io.BufferedReader;
2
 
import java.io.IOException;
3
 
import java.io.InputStream;
4
 
import java.io.InputStreamReader;
5
 
 
6
 
import com.trilead.ssh2.Connection;
7
 
import com.trilead.ssh2.Session;
8
 
import com.trilead.ssh2.StreamGobbler;
9
 
 
10
 
 
11
 
/**
12
 
 * This is a very basic example that shows
13
 
 * how one can login to a machine and execute a command.
14
 
 *  
15
 
 * @author Christian Plattner, plattner@trilead.com
16
 
 * @version $Id: Basic.java,v 1.4 2007/10/15 12:49:57 cplattne Exp $
17
 
 */
18
 
public class Basic
19
 
{
20
 
        public static void main(String[] args)
21
 
        {
22
 
                String hostname = "127.0.0.1";
23
 
                String username = "joe";
24
 
                String password = "joespass";
25
 
 
26
 
                try
27
 
                {
28
 
                        /* Create a connection instance */
29
 
 
30
 
                        Connection conn = new Connection(hostname);
31
 
 
32
 
                        /* Now connect */
33
 
 
34
 
                        conn.connect();
35
 
 
36
 
                        /* Authenticate.
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.
40
 
                         */
41
 
 
42
 
                        boolean isAuthenticated = conn.authenticateWithPassword(username, password);
43
 
 
44
 
                        if (isAuthenticated == false)
45
 
                                throw new IOException("Authentication failed.");
46
 
 
47
 
                        /* Create a session */
48
 
 
49
 
                        Session sess = conn.openSession();
50
 
 
51
 
                        sess.execCommand("uname -a && date && uptime && who");
52
 
 
53
 
                        System.out.println("Here is some information about the remote host:");
54
 
 
55
 
                        /* 
56
 
                         * This basic example does not handle stderr, which is sometimes dangerous
57
 
                         * (please read the FAQ).
58
 
                         */
59
 
 
60
 
                        InputStream stdout = new StreamGobbler(sess.getStdout());
61
 
 
62
 
                        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
63
 
 
64
 
                        while (true)
65
 
                        {
66
 
                                String line = br.readLine();
67
 
                                if (line == null)
68
 
                                        break;
69
 
                                System.out.println(line);
70
 
                        }
71
 
 
72
 
                        /* Show exit status, if available (otherwise "null") */
73
 
 
74
 
                        System.out.println("ExitCode: " + sess.getExitStatus());
75
 
 
76
 
                        /* Close this session */
77
 
 
78
 
                        sess.close();
79
 
 
80
 
                        /* Close the connection */
81
 
 
82
 
                        conn.close();
83
 
 
84
 
                }
85
 
                catch (IOException e)
86
 
                {
87
 
                        e.printStackTrace(System.err);
88
 
                        System.exit(2);
89
 
                }
90
 
        }
91
 
}