jsch home: http://www.jcraft.com/jsch/
package test;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.util.Properties;import com.jcraft.jsch.Channel;import com.jcraft.jsch.ChannelExec;import com.jcraft.jsch.JSch;import com.jcraft.jsch.Session;public class JschTest {/**
* @param args*/public static void main(String[] args) throws Exception {JSch jsch = new JSch();System.out.println("Getting session...");String host = "yourhost";String userName = "userName";String password = "password";String cmd = "ls";Session session = jsch.getSession(userName, host, 22);System.out.println("session " + session.getUserName() + "@" + session.getHost() + " established");session.setPassword(password);Properties config = new java.util.Properties();config.put("StrictHostKeyChecking", "no");session.setConfig(config);session.connect(40000);Channel channel = session.openChannel("exec");((ChannelExec) channel).setCommand(cmd);channel.connect();channel.run();// get I/O streams for remote scpOutputStream out = channel.getOutputStream();InputStream in = channel.getInputStream();String output = "";while (channel.isClosed() != true) {output += streamToString(in);}System.out.println("output:\n" + output);channel.disconnect();session.disconnect();}private static String streamToString(InputStream input) throws Exception {String output = "";while (input.available() > 0) {output += ((char) (input.read()));}return output;}}