Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8780351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:01:16+00:00 2026-06-13T20:01:16+00:00

I want to run a command on a remote machine using the RExecClient Apache

  • 0

I want to run a command on a remote machine using the RExecClient Apache Commons-Net class.

My code is:

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();
        }
    }
}

The ls command works fine but the tar command does not extract the archive and returns nothing. The message I get is InputStream is not available!.

I checked twice the tar archive exists on the server. When I rlogin manually and run the command, nothing is returned but the tar archive is extracted.

I am out of ideas.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:01:17+00:00Added an answer on June 13, 2026 at 8:01 pm

    I finally solved this problem. It is not necessary to specify the port when connecting.

    Also, if you are logged as root, check the $HOME/.rhosts file. When logged as non root, configure both $HOME/.rhosts and /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();
            }
        }
    }
    

    But I still have a question. How to check the command terminated correctly (I posted the question here)? Maybe it is possible to use the method addProtocolCommandListener from the SocketClient class. I saw there is a protected method fireReplyReceived.

    Thanks for the help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to run a tail -f logfile command on a remote machine using
i want to run some command on several machine using ssh. I know it
I want to be able to run a command from my machine using ssh
I want to run some command from a remote server in my php using
I want to run a standalone java application on a remote server. It would
I want to write code in Ruby witch net::ssh that run commands one by
I want to run a command, that will count the number of words in
tmp-file contains: database_1 database_2 database_3 I want to run a command like mysqldump DATABASE
I want to run a mysql command and set the output of that to
I want to run a bash command once for each line of a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.