1 package net.sf.jftp.tools; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Font ; 5 import java.awt.event.KeyAdapter ; 6 import java.awt.event.KeyEvent ; 7 import java.awt.event.WindowAdapter ; 8 import java.awt.event.WindowEvent ; 9 import java.io.BufferedInputStream ; 10 import java.io.BufferedOutputStream ; 11 import java.io.IOException ; 12 import java.io.StreamTokenizer ; 13 import java.util.Vector ; 14 15 import javax.swing.JFrame ; 16 import javax.swing.JOptionPane ; 17 import javax.swing.JScrollBar ; 18 import javax.swing.JScrollPane ; 19 import javax.swing.JTextArea ; 20 21 import net.sf.jftp.JFtp; 22 import net.sf.jftp.config.Settings; 23 import net.sf.jftp.gui.framework.HFrame; 24 import net.sf.jftp.net.SftpVerification; 25 import net.sf.jftp.system.logging.Log; 26 27 import com.sshtools.common.hosts.DialogHostKeyVerification; 28 import com.sshtools.j2ssh.SshClient; 29 import com.sshtools.j2ssh.authentication.AuthenticationProtocolState; 30 import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient; 31 import com.sshtools.j2ssh.configuration.ConfigurationLoader; 32 import com.sshtools.j2ssh.configuration.SshConnectionProperties; 33 import com.sshtools.j2ssh.session.SessionChannelClient; 34 35 36 public class SshShell extends JFrame implements Runnable 37 { 38 BufferedOutputStream out; 39 BufferedInputStream in; 40 BufferedInputStream err; 41 JTextArea text = new JTextArea (25, 101); 42 43 long off; 45 Thread runner; 46 JScrollPane textP; 47 int port; 48 SshConnectionProperties properties; 49 String input = ""; 50 SshClient ssh; 51 Vector commands = new Vector (); 52 int currCmd = 0; 53 54 public SshShell(SshConnectionProperties properties, String user, String pass, int port) 55 { 56 this.port = port; 57 this.properties = properties; 58 59 try 60 { 61 init(user, pass); 62 } 63 catch(Exception e) 64 { 65 e.printStackTrace(); 66 Log.debug("ERROR: " + e.getMessage()); 67 } 68 } 69 70 public SshShell() 71 { 72 try 73 { 74 String host = JOptionPane.showInternalInputDialog(JFtp.desktop, 75 "Please enter a host:"); 76 String user = JOptionPane.showInternalInputDialog(JFtp.desktop, 77 "Please enter your username:"); 78 String pass = JOptionPane.showInternalInputDialog(JFtp.desktop, 79 "Please enter your password:"); 80 81 if((host != null) && (user != null) && (pass != null)) 82 { 83 init(user, pass); 84 } 85 } 86 catch(Exception e) 87 { 88 e.printStackTrace(); 89 Log.debug("ERROR: " + e.getMessage()); 90 } 91 } 92 93 public void init(String user, String pass) throws Exception 94 { 95 setTitle("SSH Shell"); 96 97 addWindowListener(new WindowAdapter () 99 { 100 public void windowClosing(WindowEvent e) 101 { 102 ssh.disconnect(); 103 dispose(); 104 } 105 }); 106 107 HFrame.fixLocation(this); 109 110 textP = new JScrollPane (text); 111 text.setFont(new Font ("Monospaced", Font.TRUETYPE_FONT, 10)); 112 113 131 getContentPane().setLayout(new BorderLayout (5, 5)); 132 getContentPane().add("Center", textP); 133 134 text.setEditable(false); 136 text.setLineWrap(true); 137 138 setBackground(text.getBackground()); 139 140 text.addKeyListener(new KeyAdapter () 141 { 142 public void keyPressed(KeyEvent e) 143 { 144 if((e.getKeyCode() == KeyEvent.VK_BACK_SPACE) && 145 (input.length() > 0)) 146 { 147 input = input.substring(0, input.length() - 1); 148 149 String t = text.getText(); 150 t = t.substring(0, t.length() - 1); 151 text.setText(t); 152 } 153 else if(e.getKeyCode() == KeyEvent.VK_UP) 154 { 155 String t = text.getText(); 156 t = t.substring(0, t.length() - input.length()); 157 158 if((currCmd <= commands.size()) && (currCmd > 0)) 159 { 160 currCmd--; 161 162 String cmd = (String ) commands.get(currCmd); 163 input = cmd.substring(0, cmd.length() - 1); 164 text.setText(t + input); 165 } 166 } 167 else if(e.getKeyCode() == KeyEvent.VK_DOWN) 168 { 169 String t = text.getText(); 170 t = t.substring(0, t.length() - input.length()); 171 172 if(((currCmd + 1) < commands.size()) && (currCmd >= 0)) 173 { 174 currCmd++; 175 176 String cmd = (String ) commands.get(currCmd); 177 input = cmd.substring(0, cmd.length() - 1); 178 text.setText(t + input); 179 } 180 } 181 else if(e.getKeyCode() != KeyEvent.VK_SHIFT) 182 { 183 if(!e.isActionKey()) 185 { 186 input += e.getKeyChar(); 187 text.append("" + e.getKeyChar()); 188 } 189 } 190 191 if(e.getKeyCode() == KeyEvent.VK_ENTER) 192 { 193 send(); 194 } 195 } 196 }); 197 198 ConfigurationLoader.initialize(false); 199 200 ssh = new SshClient(); 201 ssh.setSocketTimeout(30000); 202 203 if(Settings.getEnableSshKeys()) 208 { 209 ssh.connect(properties, new DialogHostKeyVerification(this)); 210 } 211 else 212 { 213 ssh.connect(properties, 214 new SftpVerification(Settings.sshHostKeyVerificationFile)); 215 } 216 217 PasswordAuthenticationClient pwd = new PasswordAuthenticationClient(); 218 pwd.setUsername(user); 219 pwd.setPassword(pass); 220 221 int result = ssh.authenticate(pwd); 222 223 if(result == AuthenticationProtocolState.COMPLETE) 224 { 225 SessionChannelClient session = ssh.openSessionChannel(); 226 227 if(!session.requestPseudoTerminal("vt100", 80, 24, 0, 0, "")) 228 { 229 Log.debug("ERROR: Could not open terminal"); 230 } 231 232 if(session.startShell()) 233 { 234 out = new BufferedOutputStream (session.getOutputStream()); 235 in = new BufferedInputStream (session.getInputStream()); 236 err = new BufferedInputStream (session.getStderrInputStream()); 237 238 } 240 else 241 { 242 Log.debug("ERROR: Could not start shell"); 243 } 244 } 245 else 246 { 247 ssh.disconnect(); 248 } 249 250 pack(); 251 setVisible(true); 252 253 runner = new Thread (this); 254 runner.start(); 255 256 toFront(); 257 text.requestFocus(); 258 } 259 260 public void run() 261 { 262 try 263 { 264 byte[] b = new byte[4096]; 265 int i; 266 267 while((i = in.read(b)) != StreamTokenizer.TT_EOF) 268 { 269 text.append(new String (b, 0, i)); 270 271 while(err.available() > 0) 273 { 274 err.read(b); 275 text.append(new String (b, 0, i)); 276 } 277 278 while(text.getRows() > 500) 279 { 280 String t = text.getText(); 281 t = t.substring(250); 282 283 text.setText(t); 284 } 285 286 try 287 { 288 Thread.sleep(100); 289 } 290 catch(Exception ex) 291 { 292 ex.printStackTrace(); 293 } 294 295 JScrollBar bar = textP.getVerticalScrollBar(); 296 bar.setValue(bar.getMaximum()); 297 } 298 } 299 catch(Exception ex) 300 { 301 ex.printStackTrace(); 302 Log.debug("ERROR: " + ex.getMessage()); 303 this.dispose(); 304 } 305 } 306 307 private void send() 308 { 309 try 310 { 311 String msg = input; 312 input = ""; 313 314 out.write(msg.getBytes()); 315 out.flush(); 316 317 commands.add(msg); 318 currCmd = commands.size(); 319 320 } 322 catch(IOException ex) 323 { 324 ex.printStackTrace(); 325 Log.debug("ERROR: " + ex.getMessage()); 326 this.dispose(); 327 } 328 } 329 330 public static void main(String [] args) 331 { 332 new SshShell(); 333 } 334 } 335 | Popular Tags |