Running and retrieving process on remote linux machine using java code - java

I am running a process on a remote linux machine through java. I need to retrieve each line of processing in a string in my java code during runtime. How do I achieve this?
I am currently using sshxcute-1.0 jar inorder to connect to my remote machine and run my commands. It has a method Result.sysout which returns the entire console of the processing on my remote machine as a string. But this happens only after the entire process concludes.
I would like to retrieve each and every line of processing as it happens during runtime without closing my connection with the remote machine. Is it possible?

Related

http connection timesout when running from commandline

I developed a java code to connect to a device and issue few POST commands. This runs well in eclipse. But when I build and run on command-line it always times out.
I am running eclipse in the same machine as command line .I am using java 8.
Network problems are best tackled by first keeping Java out of it. So as a first step, you can open up the console and try to do a telnet connection to the socket address you used in your program by entering
telnet targetserver.example.com 12345
If that times out as well, the source of your problem is not within Java.
You haven't provided much (e.g. source as requested) but my guess into the blue is that you're sitting behind a proxy that is configured in Eclipse. Eclipse passes that information to the started application so the connection works. Starting the application on the console lacks this information, so the network connection is attempted directly without going via the proxy.

How to execute a bat file on a different machine and fetch the output in Java

I need to execute a Windows .bat file on a different machine. I know :
the IP address of the remote machine, and
the executable file location.
How do I execute that particular file and how can I get its output?
Both the machines run Windows.
I already saw these links:
http://docs.oracle.com/javase/1.5.0/docs/guide/rmi/hello/hello-world.html
http://www.onezerozeroone.eu/html/rmi_howto.html
But I am still not very clear on how to start?
You can't do this just using Java and RMI.
For obvious reasons, there isn't a way of running code on a remote machine unless that machine is set up to allow that sort of access. Otherwise, anyone could run anything on your machine at any time!
If it's a .bat file that you want to run, then you should look at setting up something like an ssh server on the other machine. That way, your machine will be able to connect to it via ssh (using a Java library if you like), execute the .bat file, and capture the output.
But if it doesn't need to be a .bat file, and you just want some of your own code to be triggered on the remote machine, you could write a (Java) application to run on the remote machine and listen on a particular port for messages instructing it on what to calculate, and then return the results.
That is pretty much the standard pattern for all remote services, including web servers.

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.

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!!

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