Solved

Execute unix command remotely ?

Posted on 2007-11-14
8
1,573 Views
Last Modified: 2013-12-29
Hello,

is it possible in Java to call a Unix command from one server to another ? Without RMI ?

Thank you.
0
Comment
Question by:matthew016
8 Comments
 
LVL 23

Expert Comment

by:rama_krishna580
Comment Utility
Look at here it might help you...
http://www.computing.net/unix/wwwboard/forum/5887.html

SampleCode:

/**
* @param command the string containing the command to execute
*/
public void executeCommand(String command)
{
Process p;
try{
p=Runtime.getRuntime().exec(command);
BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream()));

int i=p.waitFor();
if(i==0)
System.out.println("Command executed successfully");
else
System.out.println("Failed to execute command");
String s=null;

while((s=in.readLine())!=null)
System.out.println(s);
}

R.K
0
 
LVL 23

Accepted Solution

by:
Siva Prasanna Kumar earned 250 total points
Comment Utility
0
 
LVL 17

Assisted Solution

by:contactkarthi
contactkarthi earned 250 total points
Comment Utility
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 17

Expert Comment

by:contactkarthi
Comment Utility
>>rama_krishna580

the question is executing the command on aremote machine... i think yours will execute it locally
0
 
LVL 23

Expert Comment

by:rama_krishna580
Comment Utility
0
 
LVL 9

Expert Comment

by:ysnky
Comment Utility
for example, ls

import java.io.InputStream;
import java.io.IOException;

public class foo
{
   public foo()
   {
       Runtime rt = Runtime.getRuntime();
       Process p = null;
       try
       {
           p = rt.exec( "ls -l" );
       }
       catch ( IOException ioe )
       {
           System.out.println( "Error executing file" );
       }
       InputStream output = p.getInputStream();
       System.out.println( output );
   }

   public static void main( String args[] )
   {
       foo f = new foo();
       System.exit( 0 );
   }
}

for more;
http://www.computing.net/unix/wwwboard/forum/5887.html
http://www.unix.com/shell-programming-scripting/13932-execute-command-unix-java-possible-not.html
http://forum.java.sun.com/thread.jspa?threadID=471076&messageID=2175621
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now