与 JSch 的简单 SSH 连接 [英] Simple SSH connect with JSch

查看:18
本文介绍了与 JSch 的简单 SSH 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从这个简单的例子中做出一些事情:

SSH,用安卓执行远程命令

我只是想看看我是否可以使用 SSH 从我的 android 手机连接到 linux 服务器,但它不起作用...

这是我的主要代码:

package com.example.ssh;导入 java.util.Properties;导入 com.jcraft.jsch.JSch;导入 com.jcraft.jsch.Session;导入 android.os.Bundle;导入 android.app.Activity;公共类 MainActivity 扩展 Activity {@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);尝试{JSch jsch = new JSch();Session session = jsch.getSession("root","192.168.0.26", 22);session.setPassword("xxxxx");//避免要求密钥确认属性 prop = new Properties();prop.put("StrictHostKeyChecking", "no");session.setConfig(prop);会话连接();}捕获(例外 e){System.out.println(e.getMessage());}}}

我做错了什么?我没有错误消息,我的 Linux 上也看不到任何 SSH 连接.我添加了库 jsch 和 jzlib.我没有问题连接到腻子会话.

EDIT1 :事实上,我发现了一个错误,它解释了为什么即使我不知道如何解决问题它也不起作用.错误是:

android.os.NetworkOnMainThreadException

所以这似乎意味着应用程序无法在其主线程上执行网络操作...

解决方案

您必须在另一个线程中执行该代码,以免挂起 UI 线程,这就是异常的含义.如果 UI 线程正在执行网络调用,则它无法重新绘制 UI,因此您的用户会看到一个冻结的 UI,当应用程序正在等待网络调用完成时,该 UI 不会响应他们.Android 希望避免此类糟糕的用户体验,因此它会通过抛出此异常来防止您执行此类操作.

您的 onCreate() 方法应该调用另一个线程(我建议在原始线程上使用 AsyncTask)来执行 SSH 连接.完成后,它可以将结果发送回 UI 线程,并从 UI 线程安全地更新应用程序的 UI.

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

I am trying to make something from this simple example :

SSH, execute remote commands with Android

I just want to see if I can connect from my android phone to a linux server using SSH but it doesn't 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 don't see any SSH connection on my Linux. I added the libraries jsch and jzlib. I have no problem to get connect with a putty session.

EDIT1 : In fact, I found an error which explain why it doesn't work even if I don't know how to resolve the problem. The error is :

android.os.NetworkOnMainThreadException

so it seems to mean that the app can't perform a networking operation on its main thread...

解决方案

You have to execute that code in another thread so you don't hang the UI thread is what that exception means. If the UI thread is executing a network call it can't repaint the UI so your users sees a frozen UI that doesn't respond to them while the app is waiting on the network call to finish. Android wants to avoid bad user experiences like this so it prevents you from doing things like this by throwing this exception.

Your onCreate() method should invoke another thread (I'd suggest using an AsyncTask over a raw thread) to perform the SSH connection. Then when it's done it can post the results back to the UI thread and safely update your application's UI from the UI thread.

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

这篇关于与 JSch 的简单 SSH 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆