How do I run the ps command from Java code? I know how to execute commands when the application is running locally, but I want to execute the command for a remote Linux operating system. For example, a Java client running on Windows. The Java client needs to connect with the remote Linux system and needs to execute the ps command of Linux and get all process details.
You can run commands using Runtime.exec
https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#exec-java.lang.String-
Also, if you have key based authentication enabled you can run commands in remote systems using a ssh connection like this:
ssh user#remote.host ps -eaf
So, you can use something like:
Runtime.getRuntime().exec("ssh admin#example.com ps -eaf");
Related
I am using java to run a Psexec command to excute a process on a remote computer:
Java -> Psexec -> remote computer starts a process.
I have the PID of that process.
Is there a way to have the remote computer terminate that process?
Maybe using psexec to tell the remote computer to run a cmd command to terminate it ?
If both your local and your remote machine are UNIX-based (i.e. macOS or Ubuntu) then you can use ssh:
ssh user#remote_host 'kill -9 <pid>'
I have a Java program that runs inside a Docker container. This program needs to execute a shell command that should be run by the host system, but just calling Runtime.getRuntime().exec(...) executes it inside the Docker container (as it should be).
Is there a way I can start a process from inside a container so the process runs outside? I suspect that the exec command should go via Docker to tell it that the command itself needs to be run on the host, but I'm not sure how to do that.
The idea how to do it can be based on how docker comand line client communicates with docker service. It is just a client, that uses unix socket (i.e. just file) to stream commands to the service. Thus you may connect 1) the service on the host machine via tcp (google for docker TCP socket) 2) may make volumes of docker host machine with docker utility to make them availdable on the docker container like those parameters of docker run on Ubuntu
-v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker -v /usr/lib/x86_64-linux-gnu/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7
that make you possible to exectude docker utility in container and actually all commands will be done in host service
I am using spring shell to develop command line interface application, I am facing one issue in windows machine, On Linux machine i am able to execute OS commands by using !<OS Command>,
but this is not working in windows machine. Spring shell says
Unable to execute command, The system can not find the specified command.
Please let me know how can i execute windows command in Spring shell running in windows platform( commands like date, time,path etc)
Thanks in advance.
date, time, path, etc. are built-in commands of the windows command shell. It seems that these built-in commands are not available within spring-shell and spring-shell looks after commands which exist real as file in the windows path.
Windows-Commands like calc (! calc) which are not built-in are available.
All,
I have a remote server that I recently enabled VNC for using vnc4server and Chicken for mac as the client.
The purpose for doing so was to enable running Java's Jconsole to monitor an executable jar file that is running my server logic.
However, after logging into my server using VNC, I keep getting an error when I try to use Jconsole on vnc.
It states connection failed do you want to try again. Now I am logged in as the same user that started the process.
Is there something I am missing when using jconsole in VNC? Also can I monitor my executable jar file remotely using Jconsole on my local machine?
These are the options I am including to run the jar file: java -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9005 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.remote.ssl=false -Djava.rmi.server.hostname=ipaddress -jar path
Thanks
These JVM options fixed things. Fix found here: You need to pass to the VM: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false
https://forums.oracle.com/thread/1177644
This does not seem like an VNC issue- either the ports are not open, they are being blocked b a firewall, or there is some kind of permission/authentication issue with the app itself related to monitoring it.
In order to eliminate VNC as the cause (and use localhost in a local connection on jconsole), do "ssh -X REMOTHOST -n jconsole" and see. This will also eliminate the overhead of running the full X server and VNC.
Also on linux you can find out what process holds a port open by doing:
netstat -ap | grep PORT_NUMBER on the remote host you want to run on.
Colin
I am currently working on a Java Applet which is supposed to start a telnet session via command line. My current approach is to run:
String connectionString = "cmd /c start cmd.exe /k \"telnet\"";
Runtime.getRuntime().exec(connectionString);
When I execute this, cmd.exe opens but displays "telnet is not recognized as an internal or external command, operable program or batch file".
I do have telnet set up, there is no problem opening a cmd window and executing telnet there. I also tried to run the above snippet with other programs (rasdial, jarsigner) and it perfectly works.
Why would the cmd.exe not recognize telnet when started from java? Any help highly appreciated!
If the JVM is 32-bit on a Windows 7 system, then according to this post
...on a 64-bit Windows 7 system, telnet only works when launched from a 64-bit application....
You can try Apache Commons Net API which support telnet protocol. You can refer this sample example. You can also refer this reference guide.