LocalTunneling.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.remote.tunnel;
00002 
00003 import com.jcraft.jsch.JSch;
00004 import com.jcraft.jsch.JSchException;
00005 import com.jcraft.jsch.Session;
00006 import com.jcraft.jsch.UserInfo;
00007 
00008 /***************************************************
00009  * 
00010  * Wrapper for SSH L-Tunneling with Jsch
00011  *
00012  ***************************************************/
00013 public class LocalTunneling 
00014 {
00015 
00016     final private JSch  jsch;
00017     final private UserInfo userInfo;
00018     
00019     private Session session;
00020     private int port;
00021     
00022     private String session_id = null;
00023     
00024     public LocalTunneling(UserInfo userInfo)
00025     {
00026         jsch          = new JSch();
00027         this.userInfo = userInfo;
00028     }
00029     
00030     /*******
00031      * Build SSH port local forwarding connection 
00032      * 
00033      * @param login_user : username at login host
00034      * @param login_host : the login host
00035      * @param remote_host : the remote host where a server listens
00036      * @param port : the port number (the same as the port that
00037      *               the server listens)
00038      * 
00039      * @return the assigned port
00040      * 
00041      * @throws JSchException
00042      */
00043     public int connect(String login_user, String login_host, 
00044             String remote_host, int port)
00045             throws JSchException
00046     {
00047         String id = getSessionID(login_user, login_host, remote_host, port);
00048 
00049         if (session_id != null) {
00050             // check whether this connection is the same as the previous one
00051             if (id.equals(session_id)) {
00052                 return this.port;
00053             }
00054         }
00055         session = jsch.getSession(login_user, login_host, 22);
00056         session.setUserInfo(userInfo);
00057         session.connect();
00058         
00059         int assigned_port = session.setPortForwardingL(port, remote_host, port);
00060         this.port = assigned_port;
00061 
00062         session_id = id;
00063         return this.port;
00064     }
00065     
00066     
00067     private String getSessionID(String login_user, String login_host, 
00068             String remote_host, int port)
00069     {
00070         return login_user + "@" + login_host + ":" + remote_host + ":" + port;
00071     }
00072     
00073     /*******
00074      * disconnect tunneling
00075      * 
00076      * @throws JSchException
00077      */
00078     public void disconnect() throws JSchException
00079     {
00080         session.delPortForwardingL(port);
00081         session.disconnect();
00082     }
00083     
00084     
00085     /*
00086      * (non-Javadoc)
00087      * @see java.lang.Object#toString()
00088      */
00089     @Override
00090     public String toString()
00091     {
00092         return session_id;
00093     }
00094     
00095     /*******
00096      * unit test 
00097      * @param args
00098      */
00099     public static void main(final String []args)
00100     {
00101         if (args.length != 4)
00102         {
00103             System.out.println("Required arguments: user password login_host remote_host");
00104             return;
00105         }
00106         LocalTunneling tunnel = new LocalTunneling( new UserInfo() {
00107 
00108             @Override
00109             public boolean promptPassword(String message) {
00110                 System.out.println(message);
00111                 return true;
00112             }
00113 
00114             @Override
00115             public String getPassword() {
00116                 return args[1];
00117             }
00118 
00119             @Override
00120             public boolean promptPassphrase(String message) {
00121                 System.out.println(message);
00122                 return true;
00123             }
00124 
00125             @Override
00126             public String getPassphrase() {
00127                 return null;
00128             }
00129 
00130             @Override
00131             public boolean promptYesNo(String message) {
00132                 System.out.println(message);
00133                 return true;
00134             }
00135 
00136             @Override
00137             public void showMessage(String message) {
00138                 System.out.println(message);                
00139             }
00140             
00141         });
00142         
00143         try {
00144             int port = tunnel.connect(args[0], args[2], args[3], 21590 );
00145             System.out.println("Assigned port: " + port);
00146             
00147             tunnel.disconnect();
00148             
00149         } catch (JSchException e) {
00150             e.printStackTrace();
00151         }
00152     }
00153 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1