1 import java.io.File ; 2 import java.io.IOException ; 3 4 import ch.ethz.ssh2.Connection; 5 import ch.ethz.ssh2.LocalPortForwarder; 6 7 public class PortForwarding 8 { 9 public static void sleepSomeTime(long milliSeconds) 10 { 11 try 12 { 13 Thread.sleep(milliSeconds); 14 } 15 catch (InterruptedException e) 16 { 17 } 18 } 19 20 public static void main(String [] args) 21 { 22 String hostname = "127.0.0.1"; 23 String username = "joe"; 24 25 File keyfile = new File ("~/.ssh/id_rsa"); String keyfilePass = "joespass"; 28 try 29 { 30 31 32 Connection conn = new Connection(hostname); 33 34 35 36 conn.connect(); 37 38 39 40 boolean isAuthenticated = conn.authenticateWithPublicKey(username, keyfile, keyfilePass); 41 42 if (isAuthenticated == false) 43 throw new IOException ("Authentication failed."); 44 45 46 47 56 57 58 59 LocalPortForwarder lpf1 = conn.createLocalPortForwarder(8080, "www.ethz.ch", 80); 60 61 62 63 64 65 66 76 77 78 79 conn.requestRemotePortForwarding("127.0.0.1", 8080, "www.ganymed.ethz.ch", 80); 80 81 84 85 86 sleepSomeTime(30000); 87 88 89 90 conn.cancelRemotePortForwarding(8080); 91 92 93 sleepSomeTime(20000); 94 95 96 97 lpf1.close(); 98 99 100 101 conn.close(); 102 103 } 104 catch (IOException e) 105 { 106 e.printStackTrace(System.err); 107 System.exit(2); 108 } 109 } 110 } 111 | Popular Tags |