views:

280

answers:

3

Hey I have run into the following problem when attempting to build a program in java which executes commands on a remote linux server and returns the output for processing...

Basically I have installed Cygwin with an SSH client and want to do the following:

Open Cygwin,

Send command "user@ip";

Return output;

Send command "password";

Return output;

Send multiple other commands,

Return output;

...etc...

So far:

Process proc = Runtime.getRuntime().exec("C:/Power Apps/Cygwin/Cygwin.bat");

Works nicely except I am at a loss as to how to attempt the next steps.

Any help?

+1  A: 

The quick way: Don't go through cygwin. Pass your login info and commands as arguments to ssh.

A better way: Install and use the open source and very mature Sun Grid Engine and use its DRMAA binding for Java to exec your commands. You might also consider switching to a scripting language (yours is a very script like task). If you do DRMAA has Perl, Ruby and other bindings as well.

harschware
A: 

You could also use Plink: Download here

There is a good set of instructions link here

You can use a command like: plink root@myserver -pw passw /etc/backups/do-backup.sh

MontyBongo
That would be ideal except I need to log in to a monitor and from there the other servers which do not allow external access on SSH.
Moustachio
+1  A: 

Use a ssh implementation in java. I used Ganymede a couple of years ago, there are perhaps better alternatives now. (?)

Using Ganymede, you will get an input stream to read from, and an output stream to write to.

You can create a LineInputReader on the input stream and use that to read Strings representing the output from the remote server. Then use a regexp Pattern/Matcher to parse responses.

Create a PrintWriter on the output stream and use println() to send your commands.

Its simple and actually quite powerful (if you know regexp... It might require some trial and error to get it right...)

KarlP