Help needed to run unix command on remote machine.

Discussion in 'Java' started by ruds, Sep 6, 2006.

  1. ruds

    ruds Guest

    Hello,
    can some one tell me how do i run unix commands on unix machine
    remotely?
    I want to fire a unix command from one my machine onto other machine
    through java program.
    On shell we use rsh command but it does not work through java, is there
    some other way of doing it?
     
    ruds, Sep 6, 2006
    #1
    1. Advertising

  2. ruds

    hiwa Guest

    ruds ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:

    > Hello,
    > can some one tell me how do i run unix commands on unix machine
    > remotely?
    > I want to fire a unix command from one my machine onto other machine
    > through java program.
    > On shell we use rsh command but it does not work through java, is there
    > some other way of doing it?


    > we use rsh command but it does not work through java

    Why?
     
    hiwa, Sep 6, 2006
    #2
    1. Advertising

  3. ruds

    ruds Guest

    I want to create an application that invokes some other applications on
    the unix m/c ,
    for this i need to find out the how do i invoke the required
    application along with some parameters to it.
    And i want this for unix so how do i go about it?
     
    ruds, Sep 6, 2006
    #3
  4. ruds

    hiwa Guest

    ruds ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:

    > I want to create an application that invokes some other applications on
    > the unix m/c ,
    > for this i need to find out the how do i invoke the required
    > application along with some parameters to it.
    > And i want this for unix so how do i go about it?

    You could use Runtime.exec() method for running shell command.
     
    hiwa, Sep 6, 2006
    #4
  5. On 5 Sep 2006 21:03:51 -0700, ruds wrote:
    > On shell we use rsh command but it does not work through java, is
    > there some other way of doing it?


    rsh (or better: ssh) should work just fine from Runtime.exec(). What
    problems are you having? What does your code look like?

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 6, 2006
    #5
  6. ruds

    Simon Guest

    ruds wrote:

    > can some one tell me how do i run unix commands on unix machine
    > remotely?


    If, for some reason, you find it unconvenient to call ssh from Runtime.exec you
    can also use a Java ssh implementation, e.g. this one:

    http://www.jcraft.com/jsch/

    Cheers,
    Simon
     
    Simon, Sep 6, 2006
    #6
  7. ruds

    ruds Guest

    I want to get cpu stats of the remote machine ,for this i'm using:
    Process p=Runtime.getRuntime('rsh mac01 "iostat -t 5"');
    but it is not recognizing the rsh and giving error as given in my first
    post.

    Please tell me what to do?
     
    ruds, Sep 6, 2006
    #7
  8. ruds

    Simon Guest

    ruds wrote:
    > I want to get cpu stats of the remote machine ,for this i'm using:
    > Process p=Runtime.getRuntime('rsh mac01 "iostat -t 5"');


    Did you try to compile that?

    > but it is not recognizing the rsh and giving error as given in my first
    > post.


    Which error? Which post?

    > Please tell me what to do?


    Show us the code that you are actually using together with the error message.
     
    Simon, Sep 6, 2006
    #8
  9. On 6 Sep 2006 01:49:11 -0700, ruds wrote:
    > I want to get cpu stats of the remote machine ,for this i'm using:
    > Process p=Runtime.getRuntime('rsh mac01 "iostat -t 5"');
    > but it is not recognizing the rsh and giving error as given in my first
    > post.


    None of your posts in this thread say what the error is.

    The line of code in your example here will not compile (for more than
    one reason). Please post the *real* code you need help with, and the
    *real* error messages you get. Use cut-and-paste.

    If rsh is not recognized, then you likely need to specify a complete
    path like /bin/rsh or /usr/bin/rsh.

    Note that if you need to use quoting in the command line, you need to
    use the version of exec() that takes an array of Strings. Don't add
    any extra quotation marks, the grouping is implied by the array
    structure, like this:

    String[] cmd = {
    "/usr/bin/rsh",
    "mac01",
    "iostat -t 5",
    };

    Process p = Runtime.getRuntime().exec(cmd);

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 6, 2006
    #9
  10. ruds

    ruds Guest

    Thanks everyone for replying and Sorry for not posting the code.
    Heres the code :

    Process p = Runtime.getRuntime().exec("rsh mac5 ps");
    BufferedReader stdInput = new BufferedReader(new
    InputStreamReader(p.getInputStream()));

    BufferedReader stdError = new BufferedReader(new
    InputStreamReader(p.getErrorStream()));

    // read the output from the command
    System.out.println("Here is the standard output of the command:
    while ((s = stdInput.readLine()) != null) {
    System.out.println(s);
    }

    // read any errors from the attempted command
    System.out.println("Here is the standard error of the command (if
    any):\n");
    while ((s = stdError.readLine()) != null)
    {
    System.out.println(s);
    }

    System.exit(0);
    }//end of try
    catch (IOException e)
    {
    System.out.println("exception occured");
    e.printStackTrace();
    System.exit(-1);
    }

    I'm able to execute rsh but i'm not getting the $ prompt .
    Also ps does not execute properly its giving o/p as:
    PID TTY TIME CMD

    Thats it.
    Can u tell what is the problem and where i'm going wrong???
    thanks in advance.
     
    ruds, Sep 8, 2006
    #10
  11. On 7 Sep 2006 21:40:18 -0700, ruds wrote:
    > I'm able to execute rsh but i'm not getting the $ prompt .


    Not a Java issue. You won't ever get a prompt if you specify a command
    for rsh to run on the remote. The two are mutually exclusive: if you
    want a prompt, don't specify a command. Try it from a command shell.

    > Also ps does not execute properly its giving o/p as:
    > PID TTY TIME CMD
    >
    > Thats it.
    > Can u tell what is the problem and where i'm going wrong???


    Also not a Java issue. Try running this rsh command in a command
    shell, and see if it works any differently.

    Presumably there were no processes running that ps without arguments
    shows by default. Try specifying some arguments, like "ps ax" or
    "ps -ef" to see more.

    To bring this back on topic for the group: don't first read all of
    stdout and then all of stderr. You need to read both at the same time
    while the program is running. Otherwise a program that produces a lot
    of output to stderr will hang, and your program will deadlock while
    you wait for stdout to reach EOF.

    To solve this you need to either use a second thread (to read stderr),
    or use ProcessBuilder and specify redirectErrorStream(true).

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 8, 2006
    #11
  12. ruds

    ruds Guest

    Thanks all of u for helping me.
     
    ruds, Sep 8, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    1,765
    Terry Hancock
    Apr 23, 2005
  2. Sonu
    Replies:
    2
    Views:
    404
    Alex Popescu
    Aug 5, 2007
  3. Dan Stromberg
    Replies:
    0
    Views:
    486
    Dan Stromberg
    Dec 23, 2010
  4. Tim Chandler
    Replies:
    0
    Views:
    235
    Tim Chandler
    Oct 7, 2003
  5. Dune
    Replies:
    4
    Views:
    257
    J. Gleixner
    Jun 2, 2006
Loading...

Share This Page