Simple SSH connects to JSch

I am trying to make one of this simple example:

SSH, execute remote commands with Android

I just want to see if I can connect from my Android phone to the linux server using SSH, but it does not work ...

Here is my main code:

package com.example.ssh;

import java.util.Properties;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import android.os.Bundle;
import android.app.Activity;

 public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try
    {
        JSch jsch = new JSch();
          Session session = jsch.getSession("root","192.168.0.26", 22);
          session.setPassword("xxxxx");

          // Avoid asking for key confirmation
          Properties prop = new Properties();
          prop.put("StrictHostKeyChecking", "no");
          session.setConfig(prop);

          session.connect();

    }
    catch (Exception e)
    {
      System.out.println(e.getMessage());
    }
}
}

What did I do wrong? I have no error messages and I do not see any SSH connection on my Linux. I added jsch and jzlib libraries. I have no problem connecting to the putty session.

EDIT1: Actually, I found an error explaining why it does not work, even if I do not know how to solve the problem. Error:

android.os.NetworkOnMainThreadException

so this means that the application cannot perform a network operation in its main thread ...

+13
3

, , . , , , , . Android , , .

onCreate() ( AsyncTask ) SSH-. , , .

http://developer.android.com/reference/android/os/AsyncTask.html

+16

chubbsondubs . , , , :

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    new AsyncTask<Integer, Void, Void>(){
       @Override
       protected Void doInBackground(Integer... params) {
           try {
                 executeRemoteCommand("root", "myPW","192.168.0.26", 22);
           } catch (Exception e) {
                 e.printStackTrace();
           }
           return null;
       }
    }.execute(1);
}

public static String executeRemoteCommand(String username,String password,String hostname,int port)
        throws Exception {
    JSch jsch = new JSch();
    Session session = jsch.getSession(username, hostname, port);
    session.setPassword(password);

    // Avoid asking for key confirmation
    Properties prop = new Properties();
    prop.put("StrictHostKeyChecking", "no");
    session.setConfig(prop);

    session.connect();

    // SSH Channel
    ChannelExec channelssh = (ChannelExec)
            session.openChannel("exec");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    channelssh.setOutputStream(baos);

    // Execute command
    channelssh.setCommand("lsusb > /home/pi/test.txt");
    channelssh.connect();
    channelssh.disconnect();

    return baos.toString();
}
+12

A Kotlin solution:

import android.os.AsyncTask
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.jcraft.jsch.ChannelExec
import com.jcraft.jsch.JSch
import java.io.ByteArrayOutputStream
import java.util.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        SshTask().execute()
    }

    class SshTask : AsyncTask<Void, Void, String>() {
        override fun doInBackground(vararg p0: Void?): String {
            val output = executeRemoteCommand("demo", "password", "test.rebex.net")
            print(output)
            return output
        }
    }
}

fun executeRemoteCommand(username: String,
                         password: String,
                         hostname: String,
                         port: Int = 22): String {
    val jsch = JSch()
    val session = jsch.getSession(username, hostname, port)
    session.setPassword(password)

    // Avoid asking for key confirmation.
    val properties = Properties()
    properties.put("StrictHostKeyChecking", "no")
    session.setConfig(properties)

    session.connect()

    // Create SSH Channel.
    val sshChannel = session.openChannel("exec") as ChannelExec
    val outputStream = ByteArrayOutputStream()
    sshChannel.outputStream = outputStream

    // Execute command.
    sshChannel.setCommand("ls")
    sshChannel.connect()

    // Sleep needed in order to wait long enough to get result back.
    Thread.sleep(1_000)
    sshChannel.disconnect()

    session.disconnect()

    return outputStream.toString()
}

In build.gradleadd:

dependencies {
    ...
    compile group: 'com.jcraft', name: 'jsch', version: '0.1.54'
}
0
source

All Articles