Java Process sh command with root credentials - java

I'm trying to build a java method to execute an 'sh' command on terminal using Process class. It looks like this:
Process p;
String[] cmd = { "sh", "/dir/file.sh"};
p = Runtime.getRuntime().exec(cmd);
After setting up some permissions I can fire the command manually through console without sudo credentials and it will do the job, but java runs from tomcat at the server and the tomcat user doesn't appear to get through permits, even though the target sh file has been given -rwxr-xr-x flags.
I was wondering how could I set the same cmd String[] for it to execute like root user, something like:
echo "password" | sudo -S sh /dir/file.sh
I tried this but it didn't work:
String[] cmd = { "echo 'password' |", "sudo -S", "sh", "/dir/file.sh"};

Related

Java.Error when calling python script with Runtime

I have to calling python script in java project.I dont use jython because in script i using todoist-api. I tried use this:
try {
String cmd = "/home/kiryushin/projects/python/stm/venv/lib/stmtest1.py";
Process p = Runtime.getRuntime().exec(cmd);
}
catch (IOException e){e.printStackTrace();}
I get
" java.io.IOException: Cannot run program "/home/kiryushin/projects/python/stm/venv/lib/stmtest1.py": error=13, Permisson denied"
I try change permission with chmod -r 777, and other commands but i get this error again.
Ubuntu 18.04 lts. JDK 10. Intelij idea community edition.
Try with :
First manually check with sudo permission.
And modify:
String cmd = "sudo python /home/kiryushin/projects/python/stm/venv/lib/stmtest1.py"
use your command like this, echo your password then use sudo -S along with your command
String cmd = 'echo your_password|sudo -S python /home/kiryushin/projects/python/stm/venv/lib/stmtest1.py';
That's how I solve my problem
Recreate my project without virtual environments (venv)
Add shebang in python script
With pip3 add lib todoist-app
In calling python script i write:
String cmd = "python3.6 /home/kiryushin/projects/python/stm2/stm.py"; Process p = Runtime.getRuntime().exec(cmd);

Command from console doesn't run when launched from sshj

I launch a jar from the console and it runs fine:
nohup /usr/bin/java -jar payara-micro-5.183.jar myapp.war > log.out 2>&1 & echo $!
When I run the same command from a distant computer with sshj, it returns a pid but the process can't be seen in the console, and actually the process doesn't run. In both cases I log via ssh, public key, with the same user.
The code running the command via sshj:
try (Session session = sshClient.startSession()) {
final Command cmd = session.exec("nohup /usr/bin/java -jar payara-micro-5.183.jar myapp.war > log.out 2>&1 & echo $!");
String response = (IOUtils.readFully(cmd.getInputStream()).toString());
// response shows a pid number
cmd.join(2, TimeUnit.SECONDS);
System.out.println("\n** exit status: " + cmd.getExitStatus());
//exit status is 0
}
Solution:
With a previous command, I had cdto the directory where the jar is situated.
but this cp doesn't remove the need to use full paths for files used in the commands run after

Running java process command using Tomcat-7

I am using Java-7 and process builder to run shell script using Tomcat 7.
processBuilder = new ProcessBuilder("/bin/bash", "/opt/script.sh");
processBuilder.start();
As the above mention code invoked via JSP page hosted by Tomcat7 Server, so the command run but process is not able to continue as it is initiated by tomcat7 user (Default user of tomcat) How can i run this command as root, assuming i know root password?
Run the command using sudo:
Effectively, sudo allows a user to run a program as another user (most
often the root user).
ProcessBuilder b = new ProcessBuilder("/bin/sh", "-c", "echo <password> | sudo -S /opt/script.sh");
b.start();

Linux terminal command in Java does not work

I'm trying to execute a linux command in my java code. It needs to change permissions for some directory.
Here is my attempt:
String Cmd = "echo myPassword | sudo -S chmod 777 -R /home/somePath";
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(Cmd);
The command held in String Cmd is working perfectly when I used it in terminal. But when I use it in my code nothing happens. There is no error or warning feedback that helps me to understand my mistake. What might be the problem?
Java will not magically select bash as your executable. You probably want to do something like
"bash -c <your command>"
See this question:
How to run unix / shell commands with wildcards using Java?
(Also the | is a bash-thing. Java won't magically create pipes between processes.)

How to solve permission when running bash command from java

When I run commands from the console everything is OK:
sudo -u oracle fgrep ...
When I run the same command from Java code using ProcessBuilder, sudo doesn't work, and I need to set chmod to 775 or else I don't have permission to read logs.
Why doesn't this work? Is there an option to read logs without chmod 775?
Here is how I am using ProcessBuilder:
ProcessBuilder pb = new ProcessBuilder("bash", "-c", command);
Process shell = pb.start();
InputStream is = shell.getInputStream();
Since you say chmod 775 for log file it works, it's obvious your process doesn't have permission.
You can run your java with sudo:
sudo java ClassFileName
Or just add sudo as the first string in the array that you pass to bash process:
command[0]="sudo -u oracle ";
//command[1]=commandname;
//command[2...n]=Other params;
Assuming user oracle is in sudoers list and won't ask for password, this will run just like how it runs in commandline when you use sudo.
a. You don't need the bash -c, when you're executing the command you have a shell.
b. The command needs to be split on spaces and then passed into the ProcessBuilder as an array.

Categories

Resources