Run linux command in remote machine from java - java

I´m working with application in java.
I can execute linux command (bash) on my machine host, but i want to execute this command in a remote machine like ssh.
I´m ussing this code
Process process = Runtime.getRuntime().exec(script);
process = Runtime.getRuntime().exec(script);
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
How i can execute linux shell in remote machine with java code?

Luis: as Eric suggested one possible solution is to run a local script that performs an SSH itself on the remote server.
For instance if you have a Linux->Linux environment, your script you could have something like:
ssh remoteuser#remotehost 'bash -s' < localscripttoexecuteremotely.sh
In a Windows->Linux scenario you could do:
plink remoteuser#remotehost -m localscripttoexecuteremotely.sh
Take a look at this thread for additional information.

Related

Firefox headless never returns from taking screenshot if called from Tomcat

I play around with firefox taking screenshots in headless mode. I call firefox from a web application in a Tomcat 8 (run on Ubuntu 18.04) with the following code:
String command = "firefox --headless --screenshot /opt/foobar.png http://foo.bar; pkill firefox";
Process start = Runtime.getRuntime().exec(new String[]{"bash","-c", command});
BufferedReader reader = new BufferedReader(new InputStreamReader(start.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
log.info(line);
}
int exitCode = start.waitFor();
I get the following output (the same as with getErrorStream()):
*** You are running in headless mode. but afterwards nothing happens. Firefox is not returning and the screenshot is not created.
I know the web application is run by the user tomcat, so I created a directory /var/lib/tomcat8/.cache with chmod 777 (for testing only!).
If I call firefox --headless --screenshot /opt/foobar.png http://foo.bar; pkill firefox under my user's bash, Firefox creates the screenshot and returns in less than a second.
Any ideas why Firefox is not returning? There seems to be some permission trouble but as Firefox does not output anything it's hard to tell. Any help is appreciated!
The trick was to create /var/lib/tomcat8/.mozilla and chown it to tomcat8 was well as chmod 77.

List files on SFTP server within Docker

I'm trying to access files directly from an SFTP server, using Docker.
The following works:
import static java.nio.file.Paths.get;
public File[] copyExtractFiles() {
String command = "sftp -i case-loader/./docker/config -o StrictHostKeyChecking=no -P 2222 sftp#localhost:incoming/*.xml src/test/resources/extract";
Process p = new ProcessBuilder("bash", "-c", command).start();
p.waitFor();
BufferedReader stdOutput = new BufferedReader(new InputStreamReader(p.getInputStream(), Charset.forName(CHARSET_NAME)));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream(), Charset.forName(CHARSET_NAME)));
return get("src/test/resources/extract").toFile().listFiles();
}
This transfers an XML file from the incoming directory on the Docker image to the src/test/resources/extract directory, and then lists the files.
However, I do not have access to the local file system and so want to access the files directly on the SFTP server. Is this possible? What do I need to change?
Use SFTP library, like JSch, instead of driving an external console application. Then you will be able to download the file list to memory.
Java - download from SFTP directly to memory without ever writing to file.

Having trouble with Tomcat running a python script

I am trying to run a python script from a java bean on a Tomcat server. The code that executes this looks like this:
Process child = Runtime.getRuntime().exec(test);
BufferedReader in = new BufferedReader(new InputStreamReader(child.getInputStream()));
String line = in.readLine();
String response = "";
while(line != null){
response += line;
line += in.readLine();
}
child.waitFor();
The error I receive is in the form of two pop-ups one that reads "Could not determine the package or source package name." and the other is an OS error from Ubuntu stating an internal error has occurred with the python script.
Is there something in Tomcat that needs to be setup in order for it to be able to execute a python script on the local system?
Some background, I need the python script to dynamically generate an image based on data selected by the user. My test system is Ubuntu 12.04, I will be deploying on a CentOS system.

Cannot execute external vb script from Java program through Jenkins Slave setup

Through Jenkins - Slave setup (running in Windows), we have created a ANT job which in internally calls the below JAVA Program,
String[] command = {"cmd" , "/c", System.getProperty("user.dir")+"/Read_email/ReadEmail.vbs"};
Process p = Runtime.getRuntime().exec(command);
System.out.println("Process Completed");
The ReadEmail.vbs file never gets called or executed.
There is no error message or warning getting generated.
When I run this java program from eclipse or through Master Jenkinks, VB Scripts gets executed without any errors.
Your
String[] command = {"cmd" , "/c", System.getProperty("user.dir")+"/Read_email/ReadEmail.vbs"};
relies on the executing process to know where to find cmd.exe and who to call for a .vbs.
I used a 'fully redundant':
String[] command = {"C:/WINDOWS/system32/cmd.exe" , "/c", "C:/WINDOWS/system32/cscript.exe", "E:/trials/SoTrials/answers/21228622/java/callme.vbs"};
try {
Process p = Runtime.getRuntime().exec(command);
} catch(Exception e) {
System.out.format("%s\n", e.toString());
}
successfully from a simple commandline program. I hope this strategy works for your more complicated Jenkins setup.

wget file download ftp waitfor() hangs

I am trying to download a XML file from a FTP server with wget in my Java programm.
I have to wait until it finishes the download.
String command = "WGET -O "
+props.getProperty("xmlFolder")+""+
+ rs.getString("software")
+ ".xml ftp://"+props.getProperty("ftpUser")
+":"+props.getProperty("ftpPasswort")+"#"+rs.getString("xmlPfad");
System.out.println(command);
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
System.out.println("downloaded!");
Without the waitfor() it works perfectly, but with this function it stucks after 2-3 MB are downloaded. Any suggestions?
Have you tried to use the --quiet option for wget?
EDIT 1:
The pipe's write side (child process) might be full.
EDIT 2:
From openjdk-6-src-b20-21_jun_2010
In jdk/src/solaris/native/java/lang/UNIXProcess_md.c (at least for a UNIX system) we can see how Java launches a new child process and how it is using pipe to redirect stdout and stderr from child (wget) to parent process (Java)

Categories

Resources