1 2 import com.jcraft.jsch.*; 3 import java.awt.*; 4 import javax.swing.*; 5 import java.io.*; 6 7 public class Exec{ 8 public static void main(String [] arg){ 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 30 31 UserInfo ui=new MyUserInfo(); 33 session.setUserInfo(ui); 34 session.connect(); 35 36 String command=JOptionPane.showInputDialog("Enter command", 37 "set|grep SSH"); 38 39 Channel channel=session.openChannel("exec"); 40 ((ChannelExec)channel).setCommand(command); 41 42 45 channel.setInputStream(null); 47 48 50 ((ChannelExec)channel).setErrStream(System.err); 53 54 InputStream in=channel.getInputStream(); 55 56 channel.connect(); 57 58 byte[] tmp=new byte[1024]; 59 while(true){ 60 while(in.available()>0){ 61 int i=in.read(tmp, 0, 1024); 62 if(i<0)break; 63 System.out.print(new String (tmp, 0, i)); 64 } 65 if(channel.isClosed()){ 66 System.out.println("exit-status: "+channel.getExitStatus()); 67 break; 68 } 69 try{Thread.sleep(1000);}catch(Exception ee){} 70 } 71 channel.disconnect(); 72 session.disconnect(); 73 } 74 catch(Exception e){ 75 System.out.println(e); 76 } 77 } 78 79 public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{ 80 public String getPassword(){ return passwd; } 81 public boolean promptYesNo(String str){ 82 Object [] options={ "yes", "no" }; 83 int foo=JOptionPane.showOptionDialog(null, 84 str, 85 "Warning", 86 JOptionPane.DEFAULT_OPTION, 87 JOptionPane.WARNING_MESSAGE, 88 null, options, options[0]); 89 return foo==0; 90 } 91 92 String passwd; 93 JTextField passwordField=(JTextField)new JPasswordField(20); 94 95 public String getPassphrase(){ return null; } 96 public boolean promptPassphrase(String message){ return true; } 97 public boolean promptPassword(String message){ 98 Object [] ob={passwordField}; 99 int result= 100 JOptionPane.showConfirmDialog(null, ob, message, 101 JOptionPane.OK_CANCEL_OPTION); 102 if(result==JOptionPane.OK_OPTION){ 103 passwd=passwordField.getText(); 104 return true; 105 } 106 else{ 107 return false; 108 } 109 } 110 public void showMessage(String message){ 111 JOptionPane.showMessageDialog(null, message); 112 } 113 final GridBagConstraints gbc = 114 new GridBagConstraints(0,0,1,1,1,1, 115 GridBagConstraints.NORTHWEST, 116 GridBagConstraints.NONE, 117 new Insets(0,0,0,0),0,0); 118 private Container panel; 119 public String [] promptKeyboardInteractive(String destination, 120 String name, 121 String instruction, 122 String [] prompt, 123 boolean[] echo){ 124 panel = new JPanel(); 125 panel.setLayout(new GridBagLayout()); 126 127 gbc.weightx = 1.0; 128 gbc.gridwidth = GridBagConstraints.REMAINDER; 129 gbc.gridx = 0; 130 panel.add(new JLabel(instruction), gbc); 131 gbc.gridy++; 132 133 gbc.gridwidth = GridBagConstraints.RELATIVE; 134 135 JTextField[] texts=new JTextField[prompt.length]; 136 for(int i=0; i<prompt.length; i++){ 137 gbc.fill = GridBagConstraints.NONE; 138 gbc.gridx = 0; 139 gbc.weightx = 1; 140 panel.add(new JLabel(prompt[i]),gbc); 141 142 gbc.gridx = 1; 143 gbc.fill = GridBagConstraints.HORIZONTAL; 144 gbc.weighty = 1; 145 if(echo[i]){ 146 texts[i]=new JTextField(20); 147 } 148 else{ 149 texts[i]=new JPasswordField(20); 150 } 151 panel.add(texts[i], gbc); 152 gbc.gridy++; 153 } 154 155 if(JOptionPane.showConfirmDialog(null, panel, 156 destination+": "+name, 157 JOptionPane.OK_CANCEL_OPTION, 158 JOptionPane.QUESTION_MESSAGE) 159 ==JOptionPane.OK_OPTION){ 160 String [] response=new String [prompt.length]; 161 for(int i=0; i<prompt.length; i++){ 162 response[i]=texts[i].getText(); 163 } 164 return response; 165 } 166 else{ 167 return null; } 169 } 170 } 171 } 172 | Popular Tags |