| 1 2 import com.jcraft.jsch.*; 3 import java.awt.*; 4 import javax.swing.*; 5 6 public class PortForwardingL{ 7 public static void main(String [] arg){ 8 9 int lport; 10 String rhost; 11 int rport; 12 13 try{ 14 JSch jsch=new JSch(); 15 16 String host=JOptionPane.showInputDialog("Enter username@hostname", 17 System.getProperty("user.name")+ 18 "@localhost"); 19 String user=host.substring(0, host.indexOf('@')); 20 host=host.substring(host.indexOf('@')+1); 21 22 Session session=jsch.getSession(user, host, 22); 23 24 String foo=JOptionPane.showInputDialog("Enter -L port:host:hostport", 25 "port:host:hostport"); 26 lport=Integer.parseInt(foo.substring(0, foo.indexOf(':'))); 27 foo=foo.substring(foo.indexOf(':')+1); 28 rhost=foo.substring(0, foo.indexOf(':')); 29 rport=Integer.parseInt(foo.substring(foo.indexOf(':')+1)); 30 31 UserInfo ui=new MyUserInfo(); 33 session.setUserInfo(ui); 34 35 session.connect(); 36 37 40 int assinged_port=session.setPortForwardingL(lport, rhost, rport); 41 System.out.println("localhost:"+assinged_port+" -> "+rhost+":"+rport); 42 } 43 catch(Exception e){ 44 System.out.println(e); 45 } 46 } 47 48 public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{ 49 public String getPassword(){ return passwd; } 50 public boolean promptYesNo(String str){ 51 Object [] options={ "yes", "no" }; 52 int foo=JOptionPane.showOptionDialog(null, 53 str, 54 "Warning", 55 JOptionPane.DEFAULT_OPTION, 56 JOptionPane.WARNING_MESSAGE, 57 null, options, options[0]); 58 return foo==0; 59 } 60 61 String passwd; 62 JTextField passwordField=(JTextField)new JPasswordField(20); 63 64 public String getPassphrase(){ return null; } 65 public boolean promptPassphrase(String message){ return true; } 66 public boolean promptPassword(String message){ 67 Object [] ob={passwordField}; 68 int result= 69 JOptionPane.showConfirmDialog(null, ob, message, 70 JOptionPane.OK_CANCEL_OPTION); 71 if(result==JOptionPane.OK_OPTION){ 72 passwd=passwordField.getText(); 73 return true; 74 } 75 else{ return false; } 76 } 77 public void showMessage(String message){ 78 JOptionPane.showMessageDialog(null, message); 79 } 80 final GridBagConstraints gbc = 81 new GridBagConstraints(0,0,1,1,1,1, 82 GridBagConstraints.NORTHWEST, 83 GridBagConstraints.NONE, 84 new Insets(0,0,0,0),0,0); 85 private Container panel; 86 public String [] promptKeyboardInteractive(String destination, 87 String name, 88 String instruction, 89 String [] prompt, 90 boolean[] echo){ 91 panel = new JPanel(); 92 panel.setLayout(new GridBagLayout()); 93 94 gbc.weightx = 1.0; 95 gbc.gridwidth = GridBagConstraints.REMAINDER; 96 gbc.gridx = 0; 97 panel.add(new JLabel(instruction), gbc); 98 gbc.gridy++; 99 100 gbc.gridwidth = GridBagConstraints.RELATIVE; 101 102 JTextField[] texts=new JTextField[prompt.length]; 103 for(int i=0; i<prompt.length; i++){ 104 gbc.fill = GridBagConstraints.NONE; 105 gbc.gridx = 0; 106 gbc.weightx = 1; 107 panel.add(new JLabel(prompt[i]),gbc); 108 109 gbc.gridx = 1; 110 gbc.fill = GridBagConstraints.HORIZONTAL; 111 gbc.weighty = 1; 112 if(echo[i]){ 113 texts[i]=new JTextField(20); 114 } 115 else{ 116 texts[i]=new JPasswordField(20); 117 } 118 panel.add(texts[i], gbc); 119 gbc.gridy++; 120 } 121 122 if(JOptionPane.showConfirmDialog(null, panel, 123 destination+": "+name, 124 JOptionPane.OK_CANCEL_OPTION, 125 JOptionPane.QUESTION_MESSAGE) 126 ==JOptionPane.OK_OPTION){ 127 String [] response=new String [prompt.length]; 128 for(int i=0; i<prompt.length; i++){ 129 response[i]=texts[i].getText(); 130 } 131 return response; 132 } 133 else{ 134 return null; } 136 } 137 } 138 } 139 | Popular Tags |