• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
3  * Please refer to the LICENSE.txt for licensing details.
4  */
5 import java.io.BufferedReader;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.InputStreamReader;
9 
10 import ch.ethz.ssh2.Connection;
11 import ch.ethz.ssh2.HTTPProxyData;
12 import ch.ethz.ssh2.Session;
13 import ch.ethz.ssh2.StreamGobbler;
14 
15 public class BasicWithHTTPProxy
16 {
main(String[] args)17 	public static void main(String[] args)
18 	{
19 		String hostname = "my-ssh-server";
20 		String username = "joe";
21 		String password = "joespass";
22 
23 		String proxyHost = "192.168.1.1";
24 		int proxyPort = 3128; // default port used by squid
25 
26 		try
27 		{
28 			/* Create a connection instance */
29 
30 			Connection conn = new Connection(hostname);
31 
32 			/* We want to connect through a HTTP proxy */
33 
34 			conn.setProxyData(new HTTPProxyData(proxyHost, proxyPort));
35 
36 			// if the proxy requires basic authentication:
37 			// conn.setProxyData(new HTTPProxyData(proxyHost, proxyPort, "username", "secret"));
38 
39 			/* Now connect (through the proxy) */
40 
41 			conn.connect();
42 
43 			/* Authenticate.
44 			 * If you get an IOException saying something like
45 			 * "Authentication method password not supported by the server at this stage."
46 			 * then please check the FAQ.
47 			 */
48 
49 			boolean isAuthenticated = conn.authenticateWithPassword(username, password);
50 
51 			if (isAuthenticated == false)
52 				throw new IOException("Authentication failed.");
53 
54 			/* Create a session */
55 
56 			Session sess = conn.openSession();
57 
58 			sess.execCommand("uname -a && date && uptime && who");
59 
60 			System.out.println("Here is some information about the remote host:");
61 
62 			/*
63 			 * This basic example does not handle stderr, which is sometimes dangerous
64 			 * (please read the FAQ).
65 			 */
66 
67 			InputStream stdout = new StreamGobbler(sess.getStdout());
68 
69 			BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
70 
71 			while (true)
72 			{
73 				String line = br.readLine();
74 				if (line == null)
75 					break;
76 				System.out.println(line);
77 			}
78 
79 			/* Show exit status, if available (otherwise "null") */
80 
81 			System.out.println("ExitCode: " + sess.getExitStatus());
82 
83 			/* Close this session */
84 
85 			sess.close();
86 
87 			/* Close the connection */
88 
89 			conn.close();
90 
91 		}
92 		catch (IOException e)
93 		{
94 			e.printStackTrace(System.err);
95 			System.exit(2);
96 		}
97 	}
98 }
99