How to get input from Putty to Java? - java

I am quite new to Ubuntu and shell programming. I am using putty to execute vmstat and iostat commands on Ubuntu. I need to execute those commands in java (Windows environment) and get an InputStream of the result. Is it possible?

Yes. Use plink (a command-line interface to the PuTTY back end).
For example,
plink remote_host "ls -l"
I've successfully used this to, from a Windows-based TFS Build, execute an Android build on a Linux box. So it's possible to work with the output from plink programatically.
Here's an example of how to call a shell command from within Java.

Related

Starting via Java an proccess (script/executable) on a remote Linux machine

Is there a way that allows to start form a java (web) application a process on a remote Linux application?
The Java (web) application it self is running on a Linux machine (but windows support would be nice).
It should be possible to run a command on operating system level on a remote Linux machine (Linux only).
Is there a built in possibility for Java to connect using ssh to another host and run a command and get the return-value, stdout, stderr?
You can use ProcessBuilder to run a command using bash as it follows (example using ssh):
ProcessBuilder pb = new ProcessBuilder(new String{"/bin/bash", "-c", "ssh [ssh-options/commands]"});
Process pro = pb.start();
Once you have started the process you can the get the outputStream, errorStream and the exitCode from it.
Here's some documentation that may help you:
Running ssh commands
ProcessBuilder use examples 1 (including use of Runtime.getRuntime to create a process)
ProcessBuilder use examples 2
Hope you find this useful!

Execute shell script on remote windows server without any third party installation

I want to execute shell script on remote windows server through java code without any server setup and installations like SSH. My script file is already there on server. I just want to execute it on server cmd from my machine. Any possible solution?
According to definition of shell script from wiki:
"A shell script is a computer program designed to be run by the Unix shell"
Taking into consideration that you want to run a shell script on a windows server it will simply not work. I would suggest you to use some kind of environment as it is provided by cygwin or any other alternatives.
You can also check the following question.

How to open terminal window and execute command in php?

I am making a website which requires a java program to run on the server (it is a ubuntu server). I want to achieve this by executing it from php. I tried 'shell_exec' and 'exec', but they both don't open a terminal window or execute the jar file. It did work on my windows pc, but I want to have it work on my linux server as well. I am using xampp as server.
The command I used that worked on windows:
shell_exec("java -jar PATH/TO/JAR/FILE.jar PARAMETERS");
I fixed it myself by adding '2>&1 &' at the end of the shell_exec() command. This makes the php script wait untill the program is finished.
The new command:
shell_exec("java -jar PATH/TO/JAR/FILE.jar PARAMETERS 2>&1 &");

Can we run powershell.exe on a remote windows machine from mac?

I have a piece of Java code in which I am using Java Processbuilder to run powershell command on a remote machine. The remote machine is Windows but can I run this from Mac? I don't have Mac so I can't test it but users might have.
String[] commands = new String[] {"powershell.exe", "invoke-command", "-computerName", "myCom"};
new ProcessBuilder(commands);
Thanks
It won't work. I was thinking I can invoke the powershell somehow to run on the remote machine rather than the client itself. But I didn't find a way to do so . So had to use that code for Windows and write a separate one for Linux.

How to open java program in other linux computer without terminal holding?

I have written a java program with jar file. The java program is to update status of linux server so it need to keep running, but the linux server is in data center, so I need to remote to server to open the program. I use ssh to login linux server. Use command of "java -jar file.jar" to run the program.
However, the java program of the linux server will close if I close the terminal in my computer. Since I cannot keep opening my computer, I wanna know how to open the java programming without holding my computer terminal.
you need to use nohup to keep the program running after you log out.:
server:~name$> nohup java -jar file.jar &
this will keep your program running
Two ways
One
nohup java -jar file.jar &
Another
java -jar file.jar &
In both cases your process will go in background however the process will terminate in the second approach when shell terminates in second case.
If this program is intended to be running on all your machines for monitoring purposes, you should be running it as a service from your server's init system (systemd for most systems these days). You can use the Java Service Wrapper or jsvc or write your own init script.
Another solution apart from the proposed one:
screen -d -m java -jar your.jar
You will then have a detached screen with your java command in it. List with screen -l, reattach with screen -D -RR <screenid_obtained_via_screen_-ls>

Categories

Resources