0

我想使用 RExecClient Apache Commons-Net 类在远程机器上运行命令。

我的代码是:

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.bsd.RExecClient;

public class TestRlogin {

    static final int PORT_NUMBER = 512;
    private RExecClient client;
    private final String url = "test.corp";
    private final String login = "bob";
    private final String password = "bob";

    public TestRlogin() {
        client = new RExecClient();
    }
    public String run(String cmd) throws IOException {
        String res = null;
        InputStream is = null;
        if (client != null) {
            try {
                if (!client.isConnected()) {
                    client.connect(url, PORT_NUMBER);
                }
                client.rexec(login, password, cmd);
                is = client.getInputStream();
                if (is != null && is.available() > 0) {
                    res = IOUtils.toString(is);
                } else {
                    System.err.println("InputStream is not available!");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                IOUtils.closeQuietly(is);
                client.disconnect();
            }
        } else {
            System.err.println("The RLogin client is not connected to "+url);
        }
        return res;
    }
    public void main(String[] args) {
        TestRlogin trl = new TestRlogin();
        try {
            System.out.println(trl.run("ls -lrt /tmp;"));
            System.out.println(trl.run("tar -xf /tmp/archive.tar;"));
            System.out.println(trl.run("ls -lrt /tmp;"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

ls命令工作正常,但该tar命令不提取存档并且不返回任何内容。我得到的信息是InputStream is not available!

我检查了两次服务器上存在的 tar 存档。当我手动 rlogin 并运行命令时,没有返回任何内容,但提取了 tar 存档。

我没主意了。

4

1 回答 1

0

我终于解决了这个问题。连接时无需指定端口。

此外,如果您以 root 身份登录,请检查该$HOME/.rhosts文件。以非 root 身份登录时,同时配置$HOME/.rhosts/etc/hosts.equiv

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.bsd.RExecClient;

public class TestRlogin {

    private RExecClient client;
    private final String url = "test.corp";
    private final String login = "bob";
    private final String password = "bob";

    public TestRlogin() {
        client = new RExecClient();
    }
    public String run(String cmd) throws IOException {
        String res = null;
        InputStream is = null;
        if (client != null) {
            try {
                if (!client.isConnected()) {
                    client.connect(url);
                }
                client.rexec(login, password, cmd);
                is = client.getInputStream();
                if (is != null && is.available() > 0) {
                    res = IOUtils.toString(is);
                } else {
                    System.err.println("InputStream is not available!");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                IOUtils.closeQuietly(is);
                client.disconnect();
            }
        } else {
            System.err.println("The RLogin client is not connected to "+url);
        }
        return res;
    }
    public void main(String[] args) {
        TestRlogin trl = new TestRlogin();
        try {
            System.out.println(trl.run("ls -lrt /tmp;"));
            System.out.println(trl.run("tar -xf /tmp/archive.tar;"));
            System.out.println(trl.run("ls -lrt /tmp;"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

但我还有一个问题。如何检查命令是否正确终止(我在这里发布了问题)?也许可以使用addProtocolCommandListenerSocketClient 类中的方法。我看到有一个受保护的方法fireReplyReceived

谢谢您的帮助。

于 2012-11-05T15:20:27.497 回答