Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RexeClient apache InputStream不可用_Java_Apache Commons Net - Fatal编程技术网

Java RexeClient apache InputStream不可用

Java RexeClient apache InputStream不可用,java,apache-commons-net,Java,Apache Commons Net,我想使用RexeClient 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_NU

我想使用RexeClient 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不可用

我检查了两次服务器上是否存在tar存档。当我手动登录并运行该命令时,除了提取tar存档外,不会返回任何内容


我没有主意了。

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

此外,如果您以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();
        }
    }
}
但我还有一个问题。如何检查命令是否正确终止(我发布了问题)?可能可以使用SocketClient类中的方法
addProtocolCommandListener
。我看到有一个受保护的方法
fireReplyReceived

谢谢你的帮助