0

我在尝试使用 Java 中的 Ganymed 库测试连接时遇到了一些问题,2 周前它工作正常,没有问题,但现在我收到以下错误消息:

The execute request failed.

这与 Ganymed “execCommand()” 方法有关。当我使用 WinSCP 连接时,一切正常,但尝试使用 java 连接时出现错误。我也在想,如果防火墙也可能是原因,只是一个想法。

我用来执行tail命令的代码如下:

      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.util.HashMap;
      import java.util.Map;
      import ch.ethz.ssh2.Connection;
      import ch.ethz.ssh2.Session;
      import ch.ethz.ssh2.StreamGobbler;


     public class SSHTesting {


public static void main(String[] args) {


    try{ Connection conn = new Connection("eappdev101.momentum.co.za");

    conn.connect();

        boolean isAuthenticated = conn.authenticateWithPassword("username", "password");

        if (isAuthenticated == false) {
            System.out.println("Credentials are wrong.");
        }

        Session sess = conn.openSession();

        sess.execCommand("tail -f /logs/SystemOut.log");
        InputStream stdout = new StreamGobbler(sess.getStdout());
        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

        sess.close();
        conn.close();
        System.out.println("Done");
}

catch(Exception ie){
    System.out.println(ie.getMessage());
}



}

    }

任何光线都受到高度赞赏。先感谢您。

4

1 回答 1

0

对我来说,它适用于 key ,我认为出于安全原因你应该使用 key

检查下面的代码

private void init() throws IOException{
        char privateKeyChar[] = sshPrivateKey.toCharArray();

        conn = new Connection(ftpHostname,22);
        conn.connect();

        boolean isAuthenticated = conn.authenticateWithPublicKey(sshUsername, privateKeyChar, null);
        if (isAuthenticated == false)
            throw new IOException("Authentication failed.");

        session = conn.openSession();       
    }

    public String excuteCMD(String cmd) throws IOException
    {
        session = conn.openSession();
        session.execCommand(cmd);
        InputStream stdout = new StreamGobbler(session.getStdout());
        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

        while (true)
        {

            String line = br.readLine();
            if (line == null)
                break;
            else
                output+=line;
        }

        return output;
    }
于 2013-09-25T08:43:17.513 回答