1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ 2 /** 3 * This program will demonstrate the port forwarding like option -L of 4 * ssh command; the given port on the local host will be forwarded to 5 * the given remote host and port on the remote side. 6 * $ CLASSPATH=.:../build javac PortForwardingL.java 7 * $ CLASSPATH=.:../build java PortForwardingL 8 * You will be asked username, hostname, port:host:hostport and passwd. 9 * If everything works fine, you will get the shell prompt. 10 * Try the port on localhost. 11 * 12 */ 13 import com.jcraft.jsch.*; 14 import java.awt.*; 15 import javax.swing.*; 16 17 public class PortForwardingL{ main(String[] arg)18 public static void main(String[] arg){ 19 20 int lport; 21 String rhost; 22 int rport; 23 24 try{ 25 JSch jsch=new JSch(); 26 27 String host=null; 28 if(arg.length>0){ 29 host=arg[0]; 30 } 31 else{ 32 host=JOptionPane.showInputDialog("Enter username@hostname", 33 System.getProperty("user.name")+ 34 "@localhost"); 35 } 36 String user=host.substring(0, host.indexOf('@')); 37 host=host.substring(host.indexOf('@')+1); 38 39 Session session=jsch.getSession(user, host, 22); 40 41 String foo=JOptionPane.showInputDialog("Enter -L port:host:hostport", 42 "port:host:hostport"); 43 lport=Integer.parseInt(foo.substring(0, foo.indexOf(':'))); 44 foo=foo.substring(foo.indexOf(':')+1); 45 rhost=foo.substring(0, foo.indexOf(':')); 46 rport=Integer.parseInt(foo.substring(foo.indexOf(':')+1)); 47 48 // username and password will be given via UserInfo interface. 49 UserInfo ui=new MyUserInfo(); 50 session.setUserInfo(ui); 51 52 session.connect(); 53 54 //Channel channel=session.openChannel("shell"); 55 //channel.connect(); 56 57 int assinged_port=session.setPortForwardingL(lport, rhost, rport); 58 System.out.println("localhost:"+assinged_port+" -> "+rhost+":"+rport); 59 } 60 catch(Exception e){ 61 System.out.println(e); 62 } 63 } 64 65 public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{ getPassword()66 public String getPassword(){ return passwd; } promptYesNo(String str)67 public boolean promptYesNo(String str){ 68 Object[] options={ "yes", "no" }; 69 int foo=JOptionPane.showOptionDialog(null, 70 str, 71 "Warning", 72 JOptionPane.DEFAULT_OPTION, 73 JOptionPane.WARNING_MESSAGE, 74 null, options, options[0]); 75 return foo==0; 76 } 77 78 String passwd; 79 JTextField passwordField=(JTextField)new JPasswordField(20); 80 getPassphrase()81 public String getPassphrase(){ return null; } promptPassphrase(String message)82 public boolean promptPassphrase(String message){ return true; } promptPassword(String message)83 public boolean promptPassword(String message){ 84 Object[] ob={passwordField}; 85 int result= 86 JOptionPane.showConfirmDialog(null, ob, message, 87 JOptionPane.OK_CANCEL_OPTION); 88 if(result==JOptionPane.OK_OPTION){ 89 passwd=passwordField.getText(); 90 return true; 91 } 92 else{ return false; } 93 } showMessage(String message)94 public void showMessage(String message){ 95 JOptionPane.showMessageDialog(null, message); 96 } 97 final GridBagConstraints gbc = 98 new GridBagConstraints(0,0,1,1,1,1, 99 GridBagConstraints.NORTHWEST, 100 GridBagConstraints.NONE, 101 new Insets(0,0,0,0),0,0); 102 private Container panel; promptKeyboardInteractive(String destination, String name, String instruction, String[] prompt, boolean[] echo)103 public String[] promptKeyboardInteractive(String destination, 104 String name, 105 String instruction, 106 String[] prompt, 107 boolean[] echo){ 108 panel = new JPanel(); 109 panel.setLayout(new GridBagLayout()); 110 111 gbc.weightx = 1.0; 112 gbc.gridwidth = GridBagConstraints.REMAINDER; 113 gbc.gridx = 0; 114 panel.add(new JLabel(instruction), gbc); 115 gbc.gridy++; 116 117 gbc.gridwidth = GridBagConstraints.RELATIVE; 118 119 JTextField[] texts=new JTextField[prompt.length]; 120 for(int i=0; i<prompt.length; i++){ 121 gbc.fill = GridBagConstraints.NONE; 122 gbc.gridx = 0; 123 gbc.weightx = 1; 124 panel.add(new JLabel(prompt[i]),gbc); 125 126 gbc.gridx = 1; 127 gbc.fill = GridBagConstraints.HORIZONTAL; 128 gbc.weighty = 1; 129 if(echo[i]){ 130 texts[i]=new JTextField(20); 131 } 132 else{ 133 texts[i]=new JPasswordField(20); 134 } 135 panel.add(texts[i], gbc); 136 gbc.gridy++; 137 } 138 139 if(JOptionPane.showConfirmDialog(null, panel, 140 destination+": "+name, 141 JOptionPane.OK_CANCEL_OPTION, 142 JOptionPane.QUESTION_MESSAGE) 143 ==JOptionPane.OK_OPTION){ 144 String[] response=new String[prompt.length]; 145 for(int i=0; i<prompt.length; i++){ 146 response[i]=texts[i].getText(); 147 } 148 return response; 149 } 150 else{ 151 return null; // cancel 152 } 153 } 154 } 155 } 156