http://www.ganymed.ethz.ch/ssh2/ 一个SSH协议的JAVA实现包,下面我代码去调用它package cn.com.xinli.ssh.test;   
import java.io.BufferedReader;   
import java.io.IOException;   
import java.io.InputStream;   
import java.io.InputStreamReader;   
  
import ch.ethz.ssh2.Connection;   
import ch.ethz.ssh2.Session;   
import ch.ethz.ssh2.StreamGobbler;   
  
public class Basic   
{   
    public static void main(String[] args)   
    {   
        String hostname = "172.100.1.64";   
        String username = "******";   
        String password = "******";   
        try  
        {    
            Connection conn = new Connection(hostname);   
            conn.connect();   
            boolean isAuthenticated = conn.authenticateWithPassword(username, password);    
            Session sess = conn.openSession(); 
/////////////////////////////////////////////  执行多个就报错
            sess.execCommand("last"); 
/////////////////////////////////////////////
            InputStream stdout = new StreamGobbler(sess.getStdout());   
            BufferedReader br = new BufferedReader(new InputStreamReader(stdout));   
  
            while (true)   
            {   
                String line = br.readLine();   
                if (line == null)   
                    break;   
                System.out.println(line);   
            }   
            sess.close();   
            conn.close();   
        }    
    }   
}  
我现在要在Session处理多个command指令,如上述代码处         
   sess.execCommand("last"); 
   sess.execCommand("last"); 
就会报错说Session不能2个,看FAQ,说需要用Session.startShell(), Session.requestPTY("last"),Session.requestPTY("last"),可我用了还是不行,有没有做过相关的大侠,如何才能支持多个操作进行
如果实在不行,我单独用一个线程处理一个session,又担心while不结束会有很多SESSION堆积

解决方案 »

  1.   

    API里这样说的,是不是就是Session.startShell(), Session.requestPTY("last")的意思
    Note: If you really want to mimic the behavior of putty, then don't use Session.execCommand(), instead aquire a pty (pseudo terminal) and then start a shell (use Session.requestPTY() and Session.startShell()). You then have to communicate with the shell process at the other end through stdin and stdout. However, you also have to implement terminal logic (e.g., escape sequence handling (unless you use a "dumb" pty), "expect-send" logic (output parsing, shell prompt detection), etc.). 
      

  2.   

    顺序是先Session.requestPTY(),然后再Session.startShell()
      

  3.   

    You then have to communicate with the shell process at the other end through stdin and stdout. However, you also have to implement terminal logic之后,你还必须在另一端通过stdin和stdout与shell process交流。不过,你还必须实现终端的逻辑。看着挺复杂了...呵呵~~
      

  4.   

    有API吗  能给我一个吗  谢谢   QQ174282686   邮箱[email protected]
      

  5.   

    sess.execCommand("last && last");
    我看例子里面多个命令是用&&分隔的,如:sess.execCommand("uname -a && date && uptime && who");不妨试试。
    借贵宝地问一个问题:sess.execCommand("ls");能打印出所有资料夹和文件的名称,sess.execCommand("ll");什么也打印不出来,这是为什么呢?
      

  6.   

    这个说得对,主要是unix的命令必须依托于一个终端机,所以你必须先建立一个终端机,然后调用/执行命令。
      

  7.   

    执行多条命令吗?
    加;号就行了呀
       sess.execCommand("last;last"); 
      

  8.   

    我看上面的英文。如果说你要模拟putty那种软件的功能,那么你调用start.shell()方法。启动这个方法后,要对复杂的输入、输出流做处理。解析格式,这个是一个相当复杂的工程。相当于你使用java做一个像putty那种远程登陆工具一样。你去看看putty的源码你就大概知道需要好大的工作量了。而且我现在还没有发现一款开源的,用java做的类似putty那种工具网站。如果有知道的请告诉我一下。