1 2 import com.jcraft.jsch.*; 3 import java.awt.*; 4 import javax.swing.*; 5 6 public class KnownHosts{ 7 public static void main(String [] arg){ 8 9 try{ 10 JSch jsch=new JSch(); 11 12 JFileChooser chooser = new JFileChooser(); 13 chooser.setDialogTitle("Choose your known_hosts(ex. ~/.ssh/known_hosts)"); 14 chooser.setFileHidingEnabled(false); 15 int returnVal=chooser.showOpenDialog(null); 16 if(returnVal==JFileChooser.APPROVE_OPTION) { 17 System.out.println("You chose "+ 18 chooser.getSelectedFile().getAbsolutePath()+"."); 19 jsch.setKnownHosts(chooser.getSelectedFile().getAbsolutePath()); 20 } 21 22 HostKeyRepository hkr=jsch.getHostKeyRepository(); 23 HostKey[] hks=hkr.getHostKey(); 24 if(hks!=null){ 25 System.out.println("Host keys in "+hkr.getKnownHostsRepositoryID()); 26 for(int i=0; i<hks.length; i++){ 27 HostKey hk=hks[i]; 28 System.out.println(hk.getHost()+" "+ 29 hk.getType()+" "+ 30 hk.getFingerPrint(jsch)); 31 } 32 System.out.println(""); 33 } 34 35 String host=JOptionPane.showInputDialog("Enter username@hostname", 36 System.getProperty("user.name")+ 37 "@localhost"); 38 String user=host.substring(0, host.indexOf('@')); 39 host=host.substring(host.indexOf('@')+1); 40 41 Session session=jsch.getSession(user, host, 22); 42 43 UserInfo ui=new MyUserInfo(); 45 session.setUserInfo(ui); 46 47 53 54 session.connect(); 55 56 { 57 HostKey hk=session.getHostKey(); 58 System.out.println("HostKey: "+ 59 hk.getHost()+" "+ 60 hk.getType()+" "+ 61 hk.getFingerPrint(jsch)); 62 } 63 64 Channel channel=session.openChannel("shell"); 65 66 channel.setInputStream(System.in); 67 channel.setOutputStream(System.out); 68 69 channel.connect(); 70 } 71 catch(Exception e){ 72 System.out.println(e); 73 } 74 } 75 76 public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{ 77 public String getPassword(){ return passwd; } 78 public boolean promptYesNo(String str){ 79 Object [] options={ "yes", "no" }; 80 int foo=JOptionPane.showOptionDialog(null, 81 str, 82 "Warning", 83 JOptionPane.DEFAULT_OPTION, 84 JOptionPane.WARNING_MESSAGE, 85 null, options, options[0]); 86 return foo==0; 87 } 88 89 String passwd; 90 JTextField passwordField=(JTextField)new JPasswordField(20); 91 92 public String getPassphrase(){ return null; } 93 public boolean promptPassphrase(String message){ return true; } 94 public boolean promptPassword(String message){ 95 Object [] ob={passwordField}; 96 int result= 97 JOptionPane.showConfirmDialog(null, ob, message, 98 JOptionPane.OK_CANCEL_OPTION); 99 if(result==JOptionPane.OK_OPTION){ 100 passwd=passwordField.getText(); 101 return true; 102 } 103 else{ return false; } 104 } 105 public void showMessage(String message){ 106 JOptionPane.showMessageDialog(null, message); 107 } 108 final GridBagConstraints gbc = 109 new GridBagConstraints(0,0,1,1,1,1, 110 GridBagConstraints.NORTHWEST, 111 GridBagConstraints.NONE, 112 new Insets(0,0,0,0),0,0); 113 private Container panel; 114 public String [] promptKeyboardInteractive(String destination, 115 String name, 116 String instruction, 117 String [] prompt, 118 boolean[] echo){ 119 panel = new JPanel(); 120 panel.setLayout(new GridBagLayout()); 121 122 gbc.weightx = 1.0; 123 gbc.gridwidth = GridBagConstraints.REMAINDER; 124 gbc.gridx = 0; 125 panel.add(new JLabel(instruction), gbc); 126 gbc.gridy++; 127 128 gbc.gridwidth = GridBagConstraints.RELATIVE; 129 130 JTextField[] texts=new JTextField[prompt.length]; 131 for(int i=0; i<prompt.length; i++){ 132 gbc.fill = GridBagConstraints.NONE; 133 gbc.gridx = 0; 134 gbc.weightx = 1; 135 panel.add(new JLabel(prompt[i]),gbc); 136 137 gbc.gridx = 1; 138 gbc.fill = GridBagConstraints.HORIZONTAL; 139 gbc.weighty = 1; 140 if(echo[i]){ 141 texts[i]=new JTextField(20); 142 } 143 else{ 144 texts[i]=new JPasswordField(20); 145 } 146 panel.add(texts[i], gbc); 147 gbc.gridy++; 148 } 149 150 if(JOptionPane.showConfirmDialog(null, panel, 151 destination+": "+name, 152 JOptionPane.OK_CANCEL_OPTION, 153 JOptionPane.QUESTION_MESSAGE) 154 ==JOptionPane.OK_OPTION){ 155 String [] response=new String [prompt.length]; 156 for(int i=0; i<prompt.length; i++){ 157 response[i]=texts[i].getText(); 158 } 159 return response; 160 } 161 else{ 162 return null; } 164 } 165 } 166 } 167 168 169 | Popular Tags |