1 import java.io.BufferedReader ; 2 import java.io.IOException ; 3 import java.io.InputStream ; 4 import java.io.InputStreamReader ; 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 [] args) 14 { 15 String hostname = "my-ssh-server"; 16 String username = "joe"; 17 String password = "joespass"; 18 19 String proxyHost = "192.168.1.1"; 20 int proxyPort = 3128; 22 try 23 { 24 25 26 Connection conn = new Connection(hostname); 27 28 29 30 conn.setProxyData(new HTTPProxyData(proxyHost, proxyPort)); 31 32 35 36 37 conn.connect(); 38 39 44 45 boolean isAuthenticated = conn.authenticateWithPassword(username, password); 46 47 if (isAuthenticated == false) 48 throw new IOException ("Authentication failed."); 49 50 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 62 63 InputStream stdout = new StreamGobbler(sess.getStdout()); 64 65 BufferedReader br = new BufferedReader (new InputStreamReader (stdout)); 66 67 while (true) 68 { 69 String line = br.readLine(); 70 if (line == null) 71 break; 72 System.out.println(line); 73 } 74 75 76 77 System.out.println("ExitCode: " + sess.getExitStatus()); 78 79 80 81 sess.close(); 82 83 84 85 conn.close(); 86 87 } 88 catch (IOException e) 89 { 90 e.printStackTrace(System.err); 91 System.exit(2); 92 } 93 } 94 } 95 | Popular Tags |