Thursday, June 18, 2009

SSH and SSHExec from Java client

you can reuse the existing Ant implementation of SSHExec:

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.optional.ssh.SSHExec;

public class UnixShellTest {
public static void main(String[] args) throws Exception {
UnixShellTest shellTest = new UnixShellTest();
shellTest.test();
}

private void test() throws Exception {
Project p = new Project();
final SSHExec sshexec = new SSHExec();
sshexec.setProject(p);
sshexec.setCommand("ls");
sshexec.setHost("yourunixhosthere");
sshexec.setPassword("yourunixpassword");
sshexec.setUsername("yourunixusername");
sshexec.setTrust(true);
sshexec.setOutputproperty("result");
sshexec.execute();
String result = sshexec.getProject().getProperty("result");
System.out.println("result= " + result);
}
}

No comments: