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)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 沭阳县| 淮安市| 蕲春县| 陵川县| 峡江县| 辽宁省| 榆中县| 衡东县| 敖汉旗| 永顺县| 英吉沙县| 富阳市| 黔西县| 浪卡子县| 肃宁县| 五大连池市| 济宁市| 渑池县| 淄博市| 漳州市| 余姚市| 鹤峰县| 商河县| 虹口区| 高尔夫| 武山县| 遵义县| 大安市| 大足县| 芷江| 特克斯县| 广丰县| 汉源县| 余姚市| 英超| 若羌县| 台安县| 仙居县| 仙桃市| 三亚市| 吴旗县|