Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Help needed to run unix command on remote machine.

Thread view: 
ruds - 06 Sep 2006 05:03 GMT
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?
hiwa - 06 Sep 2006 05:26 GMT
ruds のメッセージ:

> Hello,
> can some one tell me how do i run unix commands on unix machine
[quoted text clipped - 3 lines]
> 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?
ruds - 06 Sep 2006 06:27 GMT
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?
hiwa - 06 Sep 2006 06:33 GMT
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.
Gordon Beaton - 06 Sep 2006 07:08 GMT
> 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

Signature

[ 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

Simon - 06 Sep 2006 08:03 GMT
> 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
ruds - 06 Sep 2006 09:49 GMT
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?
Simon - 06 Sep 2006 10:04 GMT
> 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.
Gordon Beaton - 06 Sep 2006 10:09 GMT
> 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

Signature

[ 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

ruds - 08 Sep 2006 05:40 GMT
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.
Gordon Beaton - 08 Sep 2006 07:24 GMT
> 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

Signature

[ 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

ruds - 08 Sep 2006 09:53 GMT
Thanks all of u for helping me.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.