Executing terminal commands using java Runtime for mode based execution - java

I have installed a terminal client utility on my linux terminal that enables me to talk to an external remote device. How do i execute such stuff using the java runtime?
For example if i want to execute a query on a mongodb installation, I can open a terminal and move to the mongo client mode by executing the "mongo --port 27017" command and get connected to the mongo server and then execute the commands from the mongo client mode from the terminal window. How can i do this from java run time.
[akhilv#dc1devpavxsrv01 bin]$ ./mongo --port 27017
MongoDB shell version: 2.6.5
connecting to: 127.0.0.1:5000/test
rpset:PRIMARY> use quartz
switched to db quartz
rpset:PRIMARY> show collections
quartz__calendars
quartz__jobs
quartz__locks
quartz__triggers
system.indexes
rpset:PRIMARY>
rpset:PRIMARY> exit
bye
[akhilv#dc1devpavxsrv01 bin]$
The above is the actual stuff that I want to be doing from the Runtime. I first execute the command ./mongo --port 27017 from my raw terminal and i am moved into the mongo client mode. Then i execute the use quartz, show collections etc, which are understood only by my mongo client mode.
I am trying to execute "use quartz" and "show collections" commands to he executed from the java code using the Runtime. I am not very specific about Runtime, but need to be using something within java as i cannot go for client libraries from mongo or any other vendors.
Please help

It is possible to do that by forking external processes from java and capture its I/O channels. So basicly from java you would have to invoke shell command, campture and parse its output. See some details on Process class.
http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html
The most interesting methods for you are waitFor() getInputStream() and getOutputStream()
This is very similar how android applications can use root access - the same way :)

Related

how to access remote Linux machine through Java API

I need to access and run Linux commands on a remote CentOS machine through Java code. Please suggest me any API to access run the commands and also I need to get the output of the commands to be printed on the Java console.
Check out JSch - it allows you to connect via SSH, execute commands remotely and transfer files.
You can use Java ProcessorBuilder and Process classes to start an ssh process that executes remote command i.e. start an ssh process (e.g. ssh username#REMOTE_MACHINE 'CMD_ON_REMOTE_MACHINE') and read the output of the executed command using getInputStream() method of Process class.

How can I run a batch script from java on a remote windows machine

I have a machine that runs batch scripts over ssh on windows machine using open ssh and cygwin (copssh)
I'm looking to change this mechanism since this tool requires configuration where many mistakes can be made.
I will also need to collect the results of the script
Any ideas on how it can be done?
Thanks
You can use RMI and on the remote side just use Runtime.getRuntime().exec

Operating terminal session from Java

I am trying to make a terminal emulator in Java. The java program will accept the commands from user, and show its output to them. I can emulate simple commands like 'ls', but I don't know how to handle commands like 'cd'. This is because, I am using exec() method for executing terminal commands. So, all the commands are executed at current directory. The commands like 'cd ..' are executed, but then they have no persistent effect, because each command is separately executed by exec().
Any Ideas How I can emulate a whole session??
If you are executing commands with exec(), you are not writing a terminal emulator; you are writing a shell. In that case, you will need to keep track of things the shell keeps track of, like environment variables and working directory.
If you really want to write a terminal emulator, you would be talking to a shell process through a pseudo-terminal. Then your program would just be keeping track of the things a terminal keeps track of, like the line state and what appears on the screen.
Working with a pseudo-terminal from Java will be a little tricky, because most of the documentation assumes you are using a C api. man pty should get you started. Your Java process will have to open the master side of the pseudo-terminal with FileStream objects. I'm not sure there is a way within Java to get a child process to open the slave side of the pseudo-terminal; you might have to invoke a shell command with exec() that starts another shell command with standard input/output/error redirected to the slave side of the pseudo terminal.
JSch is a pure Java implementation of SSH2.
JSch allows you to connect to an sshd server and use port forwarding, X11 forwarding, file transfer, etc., and you can integrate its functionality into your own Java programs.
http://www.jcraft.com/jsch/
You should really give a try to Ganymed.
Ganymed SSH-2 for Java is a library which implements the SSH-2
protocol in pure Java (tested on J2SE 1.4.2 and 5.0). It allows one to
connect to SSH servers from within Java programs. It supports SSH
sessions (remote command execution and shell access), local and remote
port forwarding, local stream forwarding, X11 forwarding, SCP and
SFTP.
http://www.ganymed.ethz.ch/ssh2/
Ganymed along with apache FTP client you can also download and upload files.
Also there is a inbuilt example code for terminal emulation in Ganymed.
The following is a link to a project which is did using Ganymed along with apache FTP client.
GITHUB
Happy Coding!!

How to fire Java method using bash

Suppose I launch a Java application:
java -cp whatever.jar com.example.Start
Process launches ok and keeps running with PID 1314.
Now I would like the system to fire a method by users request.
How can I use bash to signal the running PID and have it fire a method?
My thought is to have bash echo data to the Java processes via a named pipe, which I'm pretty sure Java has support for.
To communicate with a Java process, you would normally use RMI from another process (this could be in the same JAR)
However, if you want a pure bash/unix utilities solution, you could have the application listen on a port for commands and send back responses. This means you could use plain telnet to send commands and get output. One example of this is to use a http server with wget or you could have a simple socket based solution.

Executing batch file on remote system using Java

How to execute a batch file located on Windows remote system? Batch file should run on remote system.
Abhinav,
For your problem I see RMI is the quickest possible solution
Check out the basics from these links (1,2)
Start the server from where you want to run the batch
In the Remote object on the server side Use Runtime.getRuntime().exec() to run your batch.
From the client machine give call to this remote object and method.
Another approach is to use SSH like sshj. This only requires the remote system to have SSH installed and is more secure than RMI.
You can use Jsch and Expect4j for executing commands on remote machine(window/Linux). Further more, if your system allows, transfer the batch file on remote machine using some FTP utility like Apache Commons Net and then execute commands that executes the script.

Categories