1 2 import com.jcraft.jsch.*; 3 import java.awt.*; 4 import javax.swing.*; 5 6 public class UserAuthKI{ 7 public static void main(String [] arg){ 8 9 try{ 10 JSch jsch=new JSch(); 11 12 String host=JOptionPane.showInputDialog("Enter username@hostname", 13 System.getProperty("user.name")+ 14 "@localhost"); 15 String user=host.substring(0, host.indexOf('@')); 16 host=host.substring(host.indexOf('@')+1); 17 18 Session session=jsch.getSession(user, host, 22); 19 20 UserInfo ui=new MyUserInfo(); 22 session.setUserInfo(ui); 23 session.connect(); 24 25 Channel channel=session.openChannel("shell"); 26 27 channel.setInputStream(System.in); 28 channel.setOutputStream(System.out); 29 30 channel.connect(); 31 } 32 catch(Exception e){ 33 System.out.println(e); 34 } 35 } 36 37 public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{ 38 public String getPassword(){ return passwd; } 39 public boolean promptYesNo(String str){ 40 Object [] options={ "yes", "no" }; 41 int foo=JOptionPane.showOptionDialog(null, 42 str, 43 "Warning", 44 JOptionPane.DEFAULT_OPTION, 45 JOptionPane.WARNING_MESSAGE, 46 null, options, options[0]); 47 return foo==0; 48 } 49 50 String passwd; 51 JTextField passwordField=(JTextField)new JPasswordField(20); 52 53 public String getPassphrase(){ return null; } 54 public boolean promptPassphrase(String message){ return false; } 55 public boolean promptPassword(String message){ 56 Object [] ob={passwordField}; 57 int result= 58 JOptionPane.showConfirmDialog(null, ob, message, 59 JOptionPane.OK_CANCEL_OPTION); 60 if(result==JOptionPane.OK_OPTION){ 61 passwd=passwordField.getText(); 62 return true; 63 } 64 else{ 65 return false; 66 } 67 } 68 public void showMessage(String message){ 69 JOptionPane.showMessageDialog(null, message); 70 } 71 72 final GridBagConstraints gbc = 73 new GridBagConstraints(0,0,1,1,1,1, 74 GridBagConstraints.NORTHWEST, 75 GridBagConstraints.NONE, 76 new Insets(0,0,0,0),0,0); 77 private Container panel; 78 public String [] promptKeyboardInteractive(String destination, 79 String name, 80 String instruction, 81 String [] prompt, 82 boolean[] echo){ 83 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 |