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