I have a use case where in I have to connect to a CLI and execute commands in that CLI using java. Usually without using java, I do it by opening a linux terminal and connecting to other CLI and execute commands there. I have to implement the same using Java. I am able to run the commands on the linux terminal using Runtime.getRuntime().exec(). But, I need some help in executing the commands after connecting to a particular CLI from linux terminal using Java
I think you are asking how to make use of pseudo-terminals. I was going to warn you this was likely more complicated than you were bargaining for, but a short search showed the pty4j library which may be of help.
When you run an external program using Runtime.exec(), it returns a Process object that you can use to interact with the running process. That object has a getOutputStream() method that you can use to send commands to the process, and, getInputStream() and getErrorStream() methods that you can use to read messages produced by the process.
Related
I have an EC2 instance and i have a .jar that i already took (wget script command) from S3.
I want to run this .jar file, but not on start-up, just when i decide.
I'm using Java with "Eclipse Juno" with help from AWS SDK.
Running a user data script doesn't help me here.
Can some one help me with this?
Thanks in advance.
What you are looking for is unix cronjobs,
take a look at the cron job manual here and set it to run whenever you want.
also, you can install WEBMIN on your system, it gives you a great server management including cronjobs and scheduled commands as you need via web interface
if you want to run something with java, just use
String[] command={"java","-jar","myfile.jar"};
Runtime.getRuntime().exec(command);
You should try using JSch for remote invocation of shell scripts it's basically SSH within Java.
And here's a simple example.
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.
I want to connect a remote machine(has a Linux operation system) from my Java Program and I want to run a script on it. I will process the result of that script(it writes something to console) too.
What do you suggest for me? If anything needed I can explain more.
There are SSH libraries in java. you can use them to execute scripts on a remote machine.
I'm writing a shell script that's supposed to do the following.
- run a Java application that produces output
- run a shell command that produces output
- gather both outputs and send them out in an email
I have control of the source code of all the steps above.
Is there a best practice in gathering output from different sources? Should I redirect everything to a single temp file? Should I write different output to different files then concatenate them? What are the pros and cons of each approach?
If you don't worry about using just Java you could use the Runtime class to execute the shell command withing java (through exec) command, this will return you a Process object on which you have either getInputStream and getOutputStream so you will then be able to process the output of both the Java program and the shell command inside just one place and do whatever you want (keeping it in memory and directly send it by redirecting the outputstream to the inputstream of what you use to send the mail, with another exec) or saving it or whatever.
I'd favor using a second wrapper script which
calls the java program
calls the shell script
Captures output to a single file
reformats that output
suitable for mailing out Actually
does the mailing
Assuming you are using a unix shell, mailing/formatting and shell script calls are much simpler from the command line.
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...)