PortForwarding.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.remote.tunnel;
00002 
00003 import java.awt.Container;
00004 import java.awt.GridBagConstraints;
00005 import java.awt.GridBagLayout;
00006 import java.awt.Insets;
00007 
00008 import javax.swing.JLabel;
00009 import javax.swing.JOptionPane;
00010 import javax.swing.JPanel;
00011 import javax.swing.JPasswordField;
00012 import javax.swing.JTextField;
00013 
00014 import com.jcraft.jsch.JSch;
00015 import com.jcraft.jsch.Session;
00016 import com.jcraft.jsch.UIKeyboardInteractive;
00017 import com.jcraft.jsch.UserInfo;
00018 
00019 public class PortForwarding {
00020     public static void main(String[] arg){
00021 
00022         int lport;
00023         String rhost;
00024         int rport;
00025 
00026         try{
00027             JSch jsch=new JSch();
00028 
00029             String host=null;
00030             if(arg.length>0){
00031                 host=arg[0];
00032             }
00033             else{
00034                 host=JOptionPane.showInputDialog("Enter username@hostname",
00035                         System.getProperty("user.name")+
00036                         "@localhost"); 
00037             }
00038             String user=host.substring(0, host.indexOf('@'));
00039             host=host.substring(host.indexOf('@')+1);
00040 
00041             Session session=jsch.getSession(user, host, 22);
00042 
00043             String foo=JOptionPane.showInputDialog("Enter -L port:host:hostport",
00044                     "port:host:hostport");
00045             lport=Integer.parseInt(foo.substring(0, foo.indexOf(':')));
00046             foo=foo.substring(foo.indexOf(':')+1);
00047             rhost=foo.substring(0, foo.indexOf(':'));
00048             rport=Integer.parseInt(foo.substring(foo.indexOf(':')+1));
00049 
00050             // username and password will be given via UserInfo interface.
00051             UserInfo ui=new MyUserInfo();
00052             session.setUserInfo(ui);
00053 
00054             session.connect();
00055 
00056             //Channel channel=session.openChannel("shell");
00057             //channel.connect();
00058 
00059             int assinged_port=session.setPortForwardingL(lport, rhost, rport);
00060             System.out.println("localhost:"+assinged_port+" -> "+rhost+":"+rport);
00061         }
00062         catch(Exception e){
00063             System.out.println(e);
00064         }
00065     }
00066 
00067     public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
00068         public String getPassword(){ return passwd; }
00069         public boolean promptYesNo(String str){
00070             Object[] options={ "yes", "no" };
00071             int foo=JOptionPane.showOptionDialog(null, 
00072                     str,
00073                     "Warning", 
00074                     JOptionPane.DEFAULT_OPTION, 
00075                     JOptionPane.WARNING_MESSAGE,
00076                     null, options, options[0]);
00077             return foo==0;
00078         }
00079 
00080         String passwd;
00081         JTextField passwordField=(JTextField)new JPasswordField(20);
00082 
00083         public String getPassphrase(){ return null; }
00084         public boolean promptPassphrase(String message){ return true; }
00085         public boolean promptPassword(String message){
00086             Object[] ob={passwordField}; 
00087             int result=
00088                     JOptionPane.showConfirmDialog(null, ob, message,
00089                             JOptionPane.OK_CANCEL_OPTION);
00090             if(result==JOptionPane.OK_OPTION){
00091                 passwd=passwordField.getText();
00092                 return true;
00093             }
00094             else{ return false; }
00095         }
00096         public void showMessage(String message){
00097             JOptionPane.showMessageDialog(null, message);
00098         }
00099         final GridBagConstraints gbc = 
00100                 new GridBagConstraints(0,0,1,1,1,1,
00101                         GridBagConstraints.NORTHWEST,
00102                         GridBagConstraints.NONE,
00103                         new Insets(0,0,0,0),0,0);
00104         private Container panel;
00105         public String[] promptKeyboardInteractive(String destination,
00106                 String name,
00107                 String instruction,
00108                 String[] prompt,
00109                 boolean[] echo){
00110             panel = new JPanel();
00111             panel.setLayout(new GridBagLayout());
00112 
00113             gbc.weightx = 1.0;
00114             gbc.gridwidth = GridBagConstraints.REMAINDER;
00115             gbc.gridx = 0;
00116             panel.add(new JLabel(instruction), gbc);
00117             gbc.gridy++;
00118 
00119             gbc.gridwidth = GridBagConstraints.RELATIVE;
00120 
00121             JTextField[] texts=new JTextField[prompt.length];
00122             for(int i=0; i<prompt.length; i++){
00123                 gbc.fill = GridBagConstraints.NONE;
00124                 gbc.gridx = 0;
00125                 gbc.weightx = 1;
00126                 panel.add(new JLabel(prompt[i]),gbc);
00127 
00128                 gbc.gridx = 1;
00129                 gbc.fill = GridBagConstraints.HORIZONTAL;
00130                 gbc.weighty = 1;
00131                 if(echo[i]){
00132                     texts[i]=new JTextField(20);
00133                 }
00134                 else{
00135                     texts[i]=new JPasswordField(20);
00136                 }
00137                 panel.add(texts[i], gbc);
00138                 gbc.gridy++;
00139             }
00140 
00141             if(JOptionPane.showConfirmDialog(null, panel, 
00142                     destination+": "+name,
00143                     JOptionPane.OK_CANCEL_OPTION,
00144                     JOptionPane.QUESTION_MESSAGE)
00145                     ==JOptionPane.OK_OPTION){
00146                 String[] response=new String[prompt.length];
00147                 for(int i=0; i<prompt.length; i++){
00148                     response[i]=texts[i].getText();
00149                 }
00150                 return response;
00151             }
00152             else{
00153                 return null;  // cancel
00154             }
00155         }
00156     }
00157 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1