Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By jashburn

Thread: Run "sudo" linux commands from java programm

  1. #1
    kattoha is offline Member
    Join Date
    Apr 2014
    Posts
    5
    Rep Power
    0

    Default Run "sudo" linux commands from java programm

    Hello everybody, i am newbie in java and on this forum. I want to find information about running sudo commands from java, but without gksudo/kdesudo.
    This is my code. It works, but after removal "sudo" element.
    Java Code:
    	String array[]={"sudo", "dolphin"};
    	Process proc = null;
    	try
    	{
    	    proc = Runtime.getRuntime().exec(array);
    	}
    	catch (IOException e)
    	{
    	    e.printStackTrace();
    	}
    How i can insert password in this terminal? I tried write pass after array with commands and in, and even in outputstream.
    Give me please a little help.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Run "sudo" linux commands from java programm

    The Process class has an output stream for sending data to the executing program.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kattoha is offline Member
    Join Date
    Apr 2014
    Posts
    5
    Rep Power
    0

    Default Re: Run "sudo" linux commands from java programm

    Quote Originally Posted by Norm View Post
    The Process class has an output stream for sending data to the executing program.
    Tell me please, what is wrong?
    Java Code:
    	String array[]={"sudo", "dolphin"};
            String pas = "password";
    	Process proc = null;
            BufferedWriter writer = null;
    	try
    	{
    	    proc = Runtime.getRuntime().exec(array);
                writer = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
                writer.write(pas);
    	}
    	catch (IOException e)
    	{
    	    e.printStackTrace();
    	}
    Programm could not run the window.
    Last edited by kattoha; 04-06-2014 at 03:51 AM.

  4. #4
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Run "sudo" linux commands from java programm

    By default sudo reads the password from the user's terminal, and not from standard input. This is why writing the password to outStream obtained from proc.getOutputStream() will not immediately work. See Process (Java Platform SE 7 ) for the API doc for this method.

    You can make sudo read the password from standard input using the -S parameter. Look for the description of this parameter in sudo's man page. This means you need to change the command array to the following:
    Java Code:
    String[] array = { "sudo",  "-S", "dolphin" };
    Even with this the job is not done. If you have read the man page, you'd see that it specifically states the password needs to be followed with a newline character (\n).

    On top of that, after writing the password you need to flush the writer otherwise the password will not be sent to sudo.

    Your current implementation stores the password as a String. This is insecure. The password should at least be stored in a char array. See Writing Secure Software: Java Security: Why Not To Use String Objects For Storing Secrets for more info.

    Besides that, for a more generic command execution solution you may at times need to get the command's output that is sent to standard out or standard error, as well as the process's exit value. See When Runtime.exec() won't | JavaWorld for a good way to do this.
    kattoha likes this.

  5. #5
    kattoha is offline Member
    Join Date
    Apr 2014
    Posts
    5
    Rep Power
    0

    Smile Re: Run "sudo" linux commands from java programm

    Quote Originally Posted by jashburn View Post
    You can make sudo read the password from standard input using the -S parameter. Look for the description of this parameter in sudo's man page. This means you need to change the command array to the following:
    Java Code:
    String[] array = { "sudo",  "-S", "dolphin" };
    Big thanks for You. My code is workes with "-S" and standart output.
    I am happy and my sleeping will good.
    Last edited by kattoha; 04-06-2014 at 04:53 PM.

  6. #6
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Run "sudo" linux commands from java programm

    You're welcome. Night, night. Don't let the Runtime.exec bugs bite. ;-)

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 09:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 04:06 PM
  3. setting the "Java classes" directory in Linux
    By mr_anderson in forum Advanced Java
    Replies: 1
    Last Post: 08-02-2010, 08:18 AM
  4. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •