2012-10-16 40 views
0

我想使用RExecClient Apache Commons-Net類在遠程計算機上運行命令。RExecClient apache InputStream不可用

我的代碼是:

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歸檔文件。

我沒有想法。

回答

0

我終於解決了這個問題。連接時不需要指定端口。

此外,如果您以根用戶身份登錄,請檢查$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(); 
     } 
    } 
} 

但我仍然有一個問題。如何檢查命令正確終止(我發佈的問題here)?也許可以使用SocketClient類中的方法addProtocolCommandListener。我看到有一個受保護的方法fireReplyReceived

感謝您的幫助。

相關問題