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