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