views:

610

answers:

3

Hi all,

I am new to this kind of application and looking for some sample code how to connect to remote server using SSH , execute commands and get output back using java as programming language.

Thanks in advance.....

Regards, Devayani

A: 

Have a look at Runtime.exec() Javadoc

Process p = Runtime.exec("ssh myhost");
PrintStream out = new PrintStream(p.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream());

out.println("ls -l /home/me");
while (in.ready()) {
  String s = in.readLine();
  System.out.println(s);
}
out.println("exit");

p.waitFor();
bobah
+1  A: 

Check this library

Maurice Perry
A: 

I used ganymede for this a few yeas ago... http://www.cleondris.ch/opensource/ssh2/

KarlP