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 アーカイブが存在することを 2 回確認しました。手動で rlogin してコマンドを実行すると、何も返されませんが、tar アーカイブが抽出されます。

私はアイデアがありません。

4

1 に答える 1

0

私はついにこの問題を解決しました。接続時にポートを指定する必要はありません。

また、rootとしてログに記録されている場合は、$HOME/.rhostsファイルを確認してください。非ルートとしてログに記録される場合は、との両方$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();
        }
    }
}

しかし、まだ質問があります。コマンドが正しく終了したことを確認する方法(ここに質問を投稿しました)?たぶんaddProtocolCommandListener、SocketClientクラスのメソッドを使用することは可能です。保護されたメソッドがあるのを見ましたfireReplyReceived

助けてくれてありがとう。

于 2012-11-05T15:20:27.497 に答える