This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to run a superuser commands via java program.?

 
dharma lingam s
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Lysenko
Ranch Hand
Posts: 511
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
dharma lingam s
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski : xen hypervisor. And this is my system only. so no problem for calling the sudo by JAVA program.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic