1 2 import com.jcraft.jsch.*; 3 import java.awt.*; 4 import javax.swing.*; 5 6 public class Shell{ 7 public static void main(String [] arg){ 8 9 try{ 10 JSch jsch=new JSch(); 11 12 14 String host=JOptionPane.showInputDialog("Enter username@hostname", 15 System.getProperty("user.name")+ 16 "@localhost"); 17 String user=host.substring(0, host.indexOf('@')); 18 host=host.substring(host.indexOf('@')+1); 19 20 Session session=jsch.getSession(user, host, 22); 21 23 UserInfo ui=new MyUserInfo(); 25 session.setUserInfo(ui); 26 27 31 session.connect(30000); 34 Channel channel=session.openChannel("shell"); 35 36 39 channel.setInputStream(System.in); 40 channel.setOutputStream(System.out); 41 42 48 49 channel.connect(3*1000); 51 } 52 catch(Exception e){ 53 System.out.println(e); 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 |