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

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.

Related

Running Java program in a Docker Image

So I have a Docker network that has a Docker file with a bunch of information. I have a java program that is going to bring up the enviorment and then produce several commands to run within this enviorment. To be clear, the first command I need to run is NOT inside the Docker enviorment. I am having some challenges with the Process and Runtime classes.
First, say I wanted my java program to launch a new gnome terminal and then run a command to get into the docker network. I have this command,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal"});
Gnome terminal sucessfully comes up but any additional arguments I give in this array are just ignored. For example,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","ls"});
Does not work. The command I ultimatly want to run would look something like this,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","sudo","docker","exec","-it","sawtooth-shell-default", "bash"});
Second, Once I have this running, will additional commmands I run work within the Docker enviorment? I have a python file with a Stream handler that specifies the correct commands to run.
Other documentation on related issues was limited.
I made sure my code was wrapped in a runtime exception try catch and that I was running the correct .class file. Any help on this would be great!
Edit: I have also tried to run this in another linux terminal like Hyper and Tilda
I also am able to get a sudo sign in when I run the command like so,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","--","sudo","docker","exec","-it","sawtooth-shell-default", "bash"});
However it closes immediatly after authorizing.
Okay this is what I was attempting to do.
https://www.atlassian.com/blog/software-teams/deploy-java-apps-with-docker-awesome
This site is outdated and I had to use this link for getting that latest version of the java PPA.
This process basically installs java into the docker contatiner so that I can run a java program that uses Runtime.

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!

"adb shell date > date.txt" command execution not working in Mac using Process exec

I have an automation script written in java. Here is the code sample.
String command = "adb shell date > date.txt";
Process process = Runtime.getRuntime().exec(command);
When executing the code in windows its working fine but when I am running it in mac its not working properly.
Later I found that, in windows this command execution is creating file in my local PC project directory. But in case of Mac its failed when trying to create the file in device directory and error showing. While from Mac terminal its working fine.
My question is why is not working in my mac machine and why file is not creating in my local PC?
The Java Runtime environment doesn't necessarily pick up the same environment variables, command path, and aliases that are at work when you use a macOS Terminal.
You might try entering which adb into the Terminal and see what path you get back, if any. If you do get a definite path, try executing that full path, not just adb, from Java.

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 get input from Putty to 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.

Categories

Resources