I have adjusted my /etc/pam.d/su
file such that I can switch user from a particular user vs
to root
without password.
When I run su - -c "ls -l /tmp"
using the terminal it produces the output and it does not require any password. But when I try to do it using java ProcessBuilder it doesn't work. Following is my code:
import java.io.File;
import java.util.concurrent.TimeUnit;
class a {
public static void main(String args[]) {
try {
String cmd[] = {"su", "-", "-c", "\"ls -l /tmp\""};
ProcessBuilder pb = new ProcessBuilder(cmd).redirectOutput(new File("a"));
Process process = pb.start();
if(!process.waitFor(5, TimeUnit.SECONDS)){
process.destroy();
process.waitFor();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
I have checked my output file a
(as in above code), it is blank. But when String cmd[] = {"ls", "-l", "/tmp"};
my output file a
is not blank, it produces proper output. I want to use su
to run other programs but I am just test it using ls
.
Is there a way to run the above java code properly?