posts - 495,comments - 227,trackbacks - 0
          模擬終端顯示

          package test.tomcat;

          import java.io.BufferedReader;
          import java.io.InputStream;
          import java.io.InputStreamReader;
          import java.io.OutputStream;
          import java.util.Arrays;

          import ch.ethz.ssh2.Connection;
          import ch.ethz.ssh2.Session;

          /**
           * @author simone (wangxinsh55@126.com)
           */
          public class SshTerm {
              private static final String hostname = "IP";
              private static final String userid = "userid";
              private static final String password = "password";
              
              private static String keyBuf = "hogehogehoge";
              
              public static void main(String[] arg) throws Exception {
                  Connection con = new Connection(hostname);
                  con.connect();
                  boolean ret = con.authenticateWithPassword(userid, password);
                  if (!ret) {
                      System.out.println("loggin error!");
                  }
                  Session session = con.openSession();
                  session.requestDumbPTY();
                  session.startShell();
                  OutputStream os = session.getStdin();
                  
                  Thread t1 = new Std(session.getStdout());
                  Thread t2 = new Std(session.getStderr());
                  
                  t1.start();
                  t2.start();
                  
                  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                  while (true) {
                      keyBuf = br.readLine();
                      keyBuf = keyBuf += "\r";
                      byte[] buf = keyBuf.getBytes();
                      os.write(buf, 0, buf.length);
                      if (keyBuf.equals("exit" + "\r")) {
                          session.close();
                          con.close();
                          System.exit(0);
                      }
                  }
              }
              
              public static String getKeyBuf() {
                  return keyBuf;
              }
          }

          class Std extends Thread {
              private InputStream is;
              private final byte[] buf1 = new byte[4096];
              private final byte[] buf2 = new byte[2048];
              
              public Std() {
              }
              
              public Std(InputStream is) {
                  this.is = is;
              }
              
              @Override
              public void run() {
                  while (true) {
                      try {
                          Arrays.fill(buf1, (byte) 0x00);
                          Arrays.fill(buf2, (byte) 0x00);
                          int n = is.read(buf1);
                          Thread.sleep(500);
                          int len = is.read(buf2);
                          System.arraycopy(buf2, 0, buf1, n, len);
                          len += n;
                          if (len > 0) {
                              String st = new String(buf1, "UTF-8");
                              st = st.replaceAll("\00", "");
                              System.out.print(st);
                          }
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
              }
          }
          posted on 2013-01-18 17:47 SIMONE 閱讀(1129) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 武隆县| 辉南县| 通州市| 黎城县| 邵武市| 绍兴市| 桐柏县| 都安| 杭锦旗| 池州市| 仪陇县| 洛川县| 突泉县| 临沂市| 沂源县| 杭州市| 涡阳县| 张家川| 丘北县| 宜丰县| 霍州市| 剑河县| 南漳县| 锡林郭勒盟| 安化县| 辉南县| 东方市| 嘉荫县| 儋州市| 南皮县| 苍山县| 宁南县| 海阳市| 井研县| 霸州市| 山阴县| 卢湾区| 广饶县| 新安县| 长寿区| 金阳县|