In the JAVA application I'm developping, I need to execute on the server side a piece of PHP code sent by the client.
I found some intresting libraries but they don't return the output when there are syntax errors.
As an alternative, I thought of creating a php file, adding the content to it, then execute it using ubuntu command lines.
What I don't really know, is how to get the output of the execution, and when executing the code using terminal, what's the path of the php file that I created in java.
If you have any other ideas that would be great
I don't know much about Java but if you're going to execute php using terminal then you can use standard output redirection
php fileThatYouUploaded.php > someTemporaryFileToGetOutput.txt 2> tempFileWithErrorsIfAny.txt
or if you want the output and errors in the same file:
php fileThatYouUploaded.php > someTemporaryFileWithOutputAndErrors.txt 2>&1
As a side note: trusting a code from a user is a very bad idea. You would be better with some kind of library that will allow you to whitelist/blacklist functions and classes or even run it in closed/sandboxed environment.
Related
I need to create a Java application which sends some input parameters to a python script and sends some output back to my java application.
I cannot run the script in my java code using jython Or similar things as the python scripts are build on demand and I may need to add new scripts every now and then. So this should not impact my java app.
My java application will be running on a container and based on a few condition check it might have to select 1 of the py scripts from suppose 100 scripts and run it. And again the condition later on may change and a different script has to run at that time
I went through many websites and tutorials on the net but did not find anything relevant.
Has someone tried anything similar?
Before I get to the problem, here is what I am trying to do. So I have an assignment where you are supposed to take some sort of variable based code, interpret it, then spit out java code that connects to a database based on the latter. So basically it is java code that reads a file, and outputs a java file based on what is in the file. The output java code has to be output into a different directory (THIS IS THE PROBLEM). Also he gave us files that help us login and help with authentication. Here is the problem when I am in the directory with the output java code, and I run it, it connects to the database no problem, but when I am in any other directory, the code runs fine, but it doesn't want to connect to the database. Here are the arguments I use to run the code.
java -cp /usr/share/java/postgresql-jdbc4-
9.1.jar:/home/undergrad/3/USERNAME/testDoc/bin/a1
-Djava.security.krb5.conf=krb5.conf
-Djava.security.auth.login.config=jaas.conf a1
Since this is an assignment I can't put all my code on here, but here is the code to add something to a database. Keep in mind that this is generated code, and that that is why it looks so ugly, but the assignment is due tonight, so I can't really change that.
The stacktrace says that
the GSS authentication failed, it ended in a PSQLException. So there was an error while connecting. Yea it said it couldn't find the authentication files.
I'm building a web application that helps people improve their English pronunciation for some words. The website displays a sentence for the user, and he/she speaks it and then press "Results" button. The web application then sends two files to the server: .txt and .wav files.
The server (which is Linux (Ubuntu)) should take the files, and do some analysis and calculations, and then print out the results on a file called "Results.txt". Then the web application (which is php based) should read the results from the file and displays them to the user.
The problem is: I'm not sure what is best to do the communication between the web application and the Linux server. Till now, I succeeded in writing the .txt and .wav files on the server. And I can build a Linux script that takes these two files and do the required calculations. What I'm facing is that: I don't know how to properly and effectively start the script. And more importantly: when the script is done, how to know that I can safely read the results from the "Results.txt" file? I need a synchronization tool or method.
I asked some guys, and they told me to use a java application on the server side, but I not sure how to do it!
Any help, Please?? :)
First, you can do it with PHP. Using the shell_exec() method you can run commands your Linux scripts and also read the output. Ex:
$output = shell_exec("ls -l > outputFile.txt");
will write the current directory listing to a file called outputFile.txt
Second, you can also do the same using Java. Use:
Runtime.getRuntime().exec("ls -l").waitfor();
Not using the waitfor() in the end will cause asynchronous execution of your shell script. You can also read the stdout and stderr streams of your Linux script using
Process.getInputStream()
and
Process.getErrorStream()
methods respectively. Hope that helps.
I have a little question: we have to run Java programs and parts of the code will be uploaded by the users.
So I want to know what's the best way to run them? I know 2 possible ways,
exec("javac Usercode.class") and then run the whole thing with exec("java Main"), but I tried it with exec() and it don't work. maybe because the http is not root? But I don't know exactly why.
http://php-java-bridge.sourceforge.net/pjb/ ?
Any suggestions?
And another question is, how can I run these programs in a sandbox. we have a Debian server and so it's no problem to execute the command with a limited time, but is there a possible way to run the whole code in a sandbox?
Ideas for sandboxing:
Run in a chroot using e.g. Debian's schroot command. Protects against them accessing files outside of the chroot but not against them doing things like opening sockets etc.
Each user has their own Linux username against which they validate. Commands will then be run under the appropriate username (e.g. by using sudo or a set-uid executable).
Maintain a pool of virtual servers - expensive and complicated but gives best isolation.
Hey I have run into the following problem when attempting to build a program in java which executes commands on a remote linux server and returns the output for processing...
Basically I have installed Cygwin with an SSH client and want to do the following:
Open Cygwin,
Send command "user#ip";
Return output;
Send command "password";
Return output;
Send multiple other commands,
Return output;
...etc...
So far:
Process proc = Runtime.getRuntime().exec("C:/Power Apps/Cygwin/Cygwin.bat");
Works nicely except I am at a loss as to how to attempt the next steps.
Any help?
The quick way: Don't go through cygwin. Pass your login info and commands as arguments to ssh.
A better way: Install and use the open source and very mature Sun Grid Engine and use its DRMAA binding for Java to exec your commands. You might also consider switching to a scripting language (yours is a very script like task). If you do DRMAA has Perl, Ruby and other bindings as well.
You could also use Plink:
Download here
There is a good set of instructions link here
You can use a command like:
plink root#myserver -pw passw /etc/backups/do-backup.sh
Use a ssh implementation in java. I used Ganymede a couple of years ago, there are perhaps better alternatives now. (?)
Using Ganymede, you will get an input stream to read from, and an output stream to write to.
You can create a LineInputReader on the input stream and use that to read Strings representing the output from the remote server. Then use a regexp Pattern/Matcher to parse responses.
Create a PrintWriter on the output stream and use println() to send your commands.
Its simple and actually quite powerful (if you know regexp... It might require some trial and error to get it right...)