Java application to execute shell files from different servers - java

I would like to know if the following is possible:
I have to create a Java application that runs .sh files from different servers, I have my class to execute shells, with Runtime and Process, it runs .sh files from my computer, the thing now is that I would like to know if instead of my location be
process = runtime.exec("/home/user/Documents/example.sh");
could be:
process = runtime.exec("180.150.2.***/server/user/Documents/serverExample.sh");
and the thing is, that to get the .sh files from server, I have to login, this application could be a desktop application or a web application, but has to be in Java, so, how could I do this?
I appreciate your help.
Chema.

Basically, I don't think you can do that, the way you are trying. The Runtime.exec(...) will delegate to the OS to perform the actual execution.
There are any number of ways to achieve what you want, either purely in Java or via additional utilities based on the OS.
You could SSH or telnet to the remote machine and execute the commands via those interfaces.
You could write a client server app, where the server would allow you to send commands to it to be executed on your behalf (but you must understand that this is a massive security risk).

Check out Jsch or Ganymed SSH. I have used the latter to perform ssh/scp tasks programmatically.

Related

Java remote interactions

I'm in internship, and my mission is to build a deployment program in Java. My approach was based on a "tasks to do for a deployment" model with dynamic instanciation (but this is not the point). Every task does something locally or on the remote server (sometimes both). For example, I have a Copy task which copies a local file to the remote server. Got some tasks like this for basic interactions (Move, Delete, ...).
The point is, each deployment module is a server which has different parameters (OS, connection params, ...) and basic programs. One of them is a Linux server, so I figured I could use SSH (with JSCh) to do the job. But the other is a Windows machine, with no SSH whatsoever. I searched everywhere for several days to find what could be used, with no results.
So here is my question:
what Java API should I use to get the job done?
Is it possible with Telnet or FTP?
Is it possible at all without ssh ?
Little detail: I don't have any control on remote servers, I can't install new protocols or programs, nor have a RMI server launched on them.
Thanks for your answers.
There are a few options to interact with a remote Windows server. If your local Java application is running on Windows (that is, both your source and target server are running Windows), you can spawn a new process and execute PowerShell commands - see this link for an example.
There are ways to copy and delete files on a remote server using PowerShell. Since you didn't fully specify your requirements, I don't know if that covers all your use cases or not.
If your application does not run on Windows, you'll have to resort to a pure Java solution like j-Interop to connect to the remote system using DCOM / WMI. See this page for a good introduction to WMI and j-Interop. Word of warning though - getting DCOM to work requires some configuration changes, specifically changing permissions on registry keys, on modern versions of Windows (Windows 7 and up).

Launch two Java programs from the same code

I have a Java program which is divided into client and server code. However, for standalone users, I want both the server software and client software to be launch with the click of one button. I have had success embedding the http server with the server software. What I need now is a way to launch the two programs (as two instances) from a single place which can send information to the programs that it has spawned. How can I achieve this?
P.S. I have used JavaFX for the client program so it is better if I have a way of launching JavaFX programs as well.
Can always make a Java program that launches two Process obecjts after building the environment (classpath, correct java home etc taking in to account environment and OS). There should be reusable code in java based installers but you would have to comb thru them for what you need.
A simple program could just expect certain sub folders to have the server and client programs (along with resources like images etc), build up 2 process objects (makes sense to start the server first, maybe first check if the port the server is to listen on is free, if not, is another instance of server already running etc)

how execute linux command using java on windows?

I need execute bash commands on a separate Linux machine using java on Windows.
I need run executable file from specified directory, like
cd /home/bin
How can I do this?
You will need to use some kind of SSH library for Java that you can then use to create an SSH connection to the target machine and run the commands inside of the SSH session.
Here is another SO question that covers this. And here is another.
After long hours, I finally found useful information. please follow link "How do I run SSH commands on remote system using Java?"
Also use jar from "http://www.ganymed.ethz.ch/ssh2/" the link. It is the jar required for any person to execute ssh commands from java code in windows environment. There are other ways too. And most jars I didn't find them useful, especially when you want to automate user authentication to access linux environment.
Note, passing directly the password (ssh password) is not appropriate way of doing things.And many experts suggested in few other threads.But this simple solution works for someone who is not concerned about security.

A webserver which runs user java code and then returns results to webbrowser

I am new to java programming and Let me explain what i intend to achieve.
I have java class files present on a Linux machine.
I want to run these remotely from other machines and obtain the results (testng results or junit) back into the windows machine.
My approach
create a webserver which has my scripts (on the Linux machine).
Use webbrowser to connect to it and execute the java classes and then push the results from server to the webbrowser.
could you please provide me the links where i can learn to accomplish this task or if you have much better approach , guide me.

How to get the details using telnet API

Is it possible to get the following information via Telnet?
Software version
config files
config register
information on traffic and errors
If you have sample code that you could share that would be really useful so that I can procceed further with your help .
I will be thankful for your valuable replies.
Telnet is nothing more than a communication protocol to generally a shell interface on another Unix machine. The remote telnet daemon will most likely invoke authentication and shell processes so everything that you are requesting is yes, possible though you will thus require authentication.
As you know telnet for shell is primarily used internally inside secured networks and rarely on the 'net any longer.
Telnet is wrong protocol to do this kind of stuff. It is an (out-dated) protocol for interactive terminal sessions. You should be looking at "ssh" to execute commands on the remote machines, and then figuring out which commands you need to execute to extract the information of interest to you.
Some of the information you are after may also (in theory) be available using SNMP, but things like configuration files and application versions won't. The "ssh" approach will allow you to harvest any information that is available via commands run from the command line.
Injecting Java into the mix is almost certainly a bad idea. Java is best at tasks that are platform independent, but what you are trying to do is inherently platform specific.

Categories

Resources