I did it using this library: jsch-0.1.50.jar
http://www.jcraft.com/jsch/ http://epaul.github.io/jsch-documentation/simple.javadoc/
Source code:
package com.iha.wcc.job.ssh;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import com.jcraft.jsch.*;
import java.util.Properties;
/**
* Run a SSH command using Jsch library.
*/
public class SshTask extends AsyncTask {
private static Session session;
private static Channel channel;
private Context context;
private String host;
private String user;
private String password;
private String command;
/**
* Constructor that load the necessary information to connect using SSG protocol.
* @param context Activity context, useful to display message.
* @param host Host IP to reach.
* @param user SSH user name.
* @param password SSH user password.
* @param command SSH command to execute.
*/
public SshTask(Context context, String host, String user, String password, String command){
this.context = context;
this.host = host;
this.user = user;
this.password = password;
this.command = command;
}
/**
*
* @param arg0
* @return
*/
@Override
protected Boolean doInBackground(String... arg0) {
JSch jsch=new JSch();
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
Session session;
try {
session = jsch.getSession(user, host, 22);
session.setConfig(config);
session.setPassword(password);
session.connect();
ChannelExec channel = (ChannelExec)session.openChannel("exec");
channel.setCommand(command);
channel.connect();
SshTask.session = session;
SshTask.channel = channel;
return true;
} catch (JSchException e) {
e.printStackTrace();
return false;
}
}
@Override
protected void onPostExecute(Boolean success){
if(success){
Toast.makeText(this.context, "Video stream successfully started.", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this.context, "Unable to start the camera video stream.", Toast.LENGTH_LONG).show();;
}
}
/**
* Shutdown the camera video stream.
*/
public static void disconnect(){
if(SshTask.session != null){
SshTask.channel.disconnect();
SshTask.session.disconnect();
}
}
}