Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes how to run a superuser commands via java program.? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to run a superuser commands via java program.?" Watch "how to run a superuser commands via java program.?" New topic
Author

how to run a superuser commands via java program.?

dharma lingam s
Greenhorn

Joined: Aug 29, 2013
Posts: 14
hi all,

i am trying to execute the linux super user commands via java program.

by the runtime class i achieev this. but super user commands are not able to run.

code is:
public static void main(String a[]) throws IOException
{
try {
Process p = Runtime.getRuntime().exec("ipconfig -a");
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}

normal commands are working.
anyone have the solution means help me.???

Regards,
cooldharma06
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello!

I also had problems with running cmd commands but on windows.

My problem was that command prompted to enter some data in order to continue and that's why Runtime was waiting for some input.

Anyway it is best suggestion to run at first command manually from command line and then from Runtime.exec


True person is moral, false is right!
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

Some choices for running a command as the 'root' user are :

1) Switch to 'root' before executing the command. This will work on all Linux distributions but of course you have to be able to switch to 'root'.
2) Use 'sudo' with the '-S' switch and pass the user's password to the Process stdin. This requires the user to be registered in the sudoers configuration file (the prime user is by default on Debian based distributions) with permissions set to execute the command being run. This either requires one to put the the password in the program (very very insecure) OR to create a dialog in which the user enters his password OR to pass the password as an argument to the Java program.
3) Use 'gksudo' which automatically brings up a password dialog . This is my preferred approach and the one I always use if I can. It also requires the user to be registered in the sudoers configuration file and, of course, that 'gksudo' is available.

For this 3rd approach your command would be

Note - you should really handle the process stderr stream and the process exit code which is explained in http://www.javaworld.com/jw-12-2000/jw-1229-traps.html . You should read ALL the sections and implement ALL the recommendations.
Note 1 - you might do better to use ProcessBuilder. It has, in my view, a better interface and is what Runtime.exec() uses behind the scenes. You still need to read the 'traps' article since it is still applicable and the recommendations still apply..
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8043
    
  22

dharma cool wrote:i am trying to execute the linux super user commands via java program.

The first question I have is: why?

This is NOT what Java was designed for; and I, for one, would be VERY suspicious of any such program running on one of my systems (15 years as a Unix/Linux admin), and if I discovered one, I would probably remove it immediately and silently.

Perhaps if you explained what you're trying to achieve, we could suggest a better (and safer) alternative.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
dharma lingam s
Greenhorn

Joined: Aug 29, 2013
Posts: 14
i installed some open source tools that things are work only on super user mode. to make easier my job i am doing automation so that i am asking..

@Winston Gutkowski if you know way means suggest me.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8043
    
  22

dharma lingam s wrote:i installed some open source tools that things are work only on super user mode.

Well, that right there sounds alarm bells for me. What are these "tools" for, and why do they need superuser access?

to make easier my job i am doing automation so that i am asking..

I need a bit of background before I start advising anything.
1. Is this for use on your home computer?
2. If for work:
(a) Have you informed your admins that you've installed these products?
(b) Have you informed them of your intention to automate this process, and got their approval?

Anything requiring superuser access is inherently unsafe, so you'd better be absolutely sure that you trust these "tools" before you even consider such a move. At most places I've worked, trying anything like this without written management approval would be grounds for dismissal; so think very carefully before you just plough ahead.

Furthermore "sudo" is mainly a command-line function, and it's designed to be used on individual commands, not via something like Java (which may well be why you're having so much trouble). I suspect you'd be much better off trying to get your java program running with SU privileges, and then invoke your tool but, to be honest, the whole approach sounds a bit clunky to me.

Is this not, for example, something that you could "automate" through cron?

Winston
dharma lingam s
Greenhorn

Joined: Aug 29, 2013
Posts: 14
Winston Gutkowski : xen hypervisor. And this is my system only. so no problem for calling the sudo by JAVA program.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8043
    
  22

dharma lingam s wrote:Winston Gutkowski : xen hypervisor. And this is my system only. so no problem for calling the sudo by JAVA program.

A hypervisor? Why would you want to run something like that with Java, when you can set it up perfectly easily with xinetd?

Methinks there's more to this than you're telling us; but if not, I stand by my previous post.

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: how to run a superuser commands via java program.?