KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > BasicWithHTTPProxy


1 import java.io.BufferedReader JavaDoc;
2 import java.io.IOException JavaDoc;
3 import java.io.InputStream JavaDoc;
4 import java.io.InputStreamReader JavaDoc;
5
6 import ch.ethz.ssh2.Connection;
7 import ch.ethz.ssh2.HTTPProxyData;
8 import ch.ethz.ssh2.Session;
9 import ch.ethz.ssh2.StreamGobbler;
10
11 public class BasicWithHTTPProxy
12 {
13     public static void main(String JavaDoc[] args)
14     {
15         String JavaDoc hostname = "my-ssh-server";
16         String JavaDoc username = "joe";
17         String JavaDoc password = "joespass";
18
19         String JavaDoc proxyHost = "192.168.1.1";
20         int proxyPort = 3128; // default port used by squid
21

22         try
23         {
24             /* Create a connection instance */
25
26             Connection conn = new Connection(hostname);
27
28             /* We want to connect through a HTTP proxy */
29             
30             conn.setProxyData(new HTTPProxyData(proxyHost, proxyPort));
31             
32             // if the proxy requires basic authentication:
33
// conn.setProxyData(new HTTPProxyData(proxyHost, proxyPort, "username", "secret"));
34

35             /* Now connect (through the proxy) */
36
37             conn.connect();
38
39             /* Authenticate.
40              * If you get an IOException saying something like
41              * "Authentication method password not supported by the server at this stage."
42              * then please check the FAQ.
43              */

44
45             boolean isAuthenticated = conn.authenticateWithPassword(username, password);
46
47             if (isAuthenticated == false)
48                 throw new IOException JavaDoc("Authentication failed.");
49
50             /* Create a session */
51
52             Session sess = conn.openSession();
53
54             sess.execCommand("uname -a && date && uptime && who");
55
56             System.out.println("Here is some information about the remote host:");
57
58             /*
59              * This basic example does not handle stderr, which is sometimes dangerous
60              * (please read the FAQ).
61              */

62
63             InputStream JavaDoc stdout = new StreamGobbler(sess.getStdout());
64
65             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(stdout));
66
67             while (true)
68             {
69                 String JavaDoc line = br.readLine();
70                 if (line == null)
71                     break;
72                 System.out.println(line);
73             }
74
75             /* Show exit status, if available (otherwise "null") */
76
77             System.out.println("ExitCode: " + sess.getExitStatus());
78
79             /* Close this session */
80
81             sess.close();
82
83             /* Close the connection */
84
85             conn.close();
86
87         }
88         catch (IOException JavaDoc e)
89         {
90             e.printStackTrace(System.err);
91             System.exit(2);
92         }
93     }
94 }
95
Popular Tags