// login validateData private void validateData() { String host = jTextField1.getText(); String username = jTextField2.getText(); int port = 22; try { port = Integer.parseInt(jTextField3.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, "端口号必须为数字"); return; } if (host.length() == 0) { JOptionPane.showMessageDialog(this, "必须填写主机"); return; } if (username.length() == 0) { JOptionPane.showMessageDialog(this, "必须填写登陆用户名"); return; } // jTextField1.setVisible(false); // jTextField2.setVisible(false); // jTextField3.setVisible(false); // jButton1.setVisible(false); // jButton2.setVisible(false); ConService cs = null; // TODO must try { cs = new ConService(host, port, username); initPassword(); // if failure while while (!cs.login(pass)) { initPassword(); } // opensession Session sess = cs.getSession(); int x_width = 90; int y_width = 20; sess.requestPTY("temp", x_width, y_width, 0, 0, null); sess.startShell(); TerminalDialog td = new TerminalDialog(myframe, username + "@" + host, sess, x_width, y_width); /* The following call blocks until the dialog has been closed */ td.setVisible(true); // myframe.dispose(); } catch (Exception e) { JOptionPane.showMessageDialog(this, e.getMessage()); } }
private void runCommand(final CommandToDo commandToDo) throws IOException { final SSHConnection sshConnection = context.getSSHConnection(); final String requestPTY = context.getRequestPTY(); final Session session = sshConnection.getConnection().openSession(); try { if (requestPTY != null) { session.requestPTY(requestPTY, Const.TERMINAL_WIDTH, Const.TERMINAL_HEIGHT, 0, 0, null); } runCommand(commandToDo, session); } finally { session.close(); } }
public String executeCommand(List<String> commands) throws IOException, InterruptedException { InputStream stdout; InputStream stderr; Session sess = connection.openSession(); sess.requestPTY("bash"); sess.startShell(); PrintWriter out = new PrintWriter(sess.getStdin()); for (String command : commands) { out.println(command); } out.println("exit"); out.flush(); out.close(); stdout = new StreamGobbler(sess.getStdout()); stderr = new StreamGobbler(sess.getStderr()); final BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout, tools.office.StringUtils.getUTF8String())); final BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr, tools.office.StringUtils.getUTF8String())); final StringBuilder returnBuilder = new StringBuilder(); final Thread readThread = new Thread() { public void run() { String line; while (true) { try { line = stdoutReader.readLine(); if (line == null) { break; } returnBuilder.append(line + tools.file.FileUtils.Line_SEP); } catch (IOException e) { returnBuilder.append(e.getCause() + e.getMessage()); break; } } } }; Thread readErrThread = new Thread() { public void run() { String errline; while (true) { try { errline = stderrReader.readLine(); if (errline == null) { break; } returnBuilder.append(errline + tools.file.FileUtils.Line_SEP); } catch (IOException e) { returnBuilder.append(e.getCause() + e.getMessage()); break; } } } }; stdoutReader.close(); stderrReader.close(); readThread.start(); readErrThread.start(); readThread.join(); readErrThread.join(); sess.close(); return returnBuilder.toString(); }