I'm currently trying to code up an android app that does a simple "ls" command over ssh to a remote machine. I've tried to use JSch and this page , but it just doesn't seem to work. I've looked around and it seems that I have to implement some Async task to run the code. Here is the entire code for the main activity right now:
package com.sshtest.sshtest;
import android.app.Activity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import java.util.Properties;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.*;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.lang.String;
public class MainActivity extends Activity {
String string;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private class sshconnection extends AsyncTask<String, Void, String> {
protected String doInBackground(String... params) {
try {
JSch jsch = new JSch();
Session session = jsch.getSession("name", "ipaddress", 22);
session.setPassword("password");
// Avoid asking for key confirmation
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
ChannelExec channel = (ChannelExec)session.openChannel("exec");
channel.setCommand("ls");
channel.connect();
InputStream input = channel.getInputStream();
channel.disconnect();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(String result) {
TextView txt = (TextView) findViewById(R.id.txtipinfo);
txt.setText("Executed");
}
}
This is my first attempt at any serious coding, if anyone can help it would be greatly appreciated. Thanks!
If you're just want to test. Try not to using AsyncTask, but just simply a thread... like this
new Thread(new Runnable() {
@Override
public void run() {
Session session;
JSch jsch;
try {
jsch = new JSch();
jsch.addIdentity(privateKey, "yourprivatekey");
session = jsch.getSession("git", "github.com", 22);
session.setPassword("yourpass");
// Avoid asking for key confirmation
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
if(session.isConnected()){
System.out.println(this.getClass().getSimpleName() + " CONNECTED");
System.out.println(this.getClass().getSimpleName() + " YOO " + jsch.getIdentityRepository().getName()+" "+session.getClientVersion() + " " + session.isConnected());
}else{
System.out.println(this.getClass().getSimpleName() + " NOT CONNECTED");
}
}catch (Exception e){
e.printStackTrace();
}
}
}).start();
Call it in the onCreate() method on your Main Activity. Hope it helped !
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.