android - 使用 Android 应用程序发送 SSH 命令

标签 android ssh raspberry-pi jsch

<分区>

我正在尝试通过 WiFi 从我的 Android 手机向 Raspberry Pi 发送命令。这就是我所做的:我已将 RPi 设置为 wifi 接入点,现在我可以使用 Android 手机连接到它。

现在我已经在 Android Studio 中编写了一个简单的应用程序来使用 SSH 发送命令。但不幸的是它不起作用。以下是我的代码:

MainActivity.java

package example_company.controller1;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.graphics.Color;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);    
    }

    public void onClick11 (View v) {
        new AsyncTask<Integer, Void, Void>(){
            @Override
            protected Void doInBackground(Integer... params) {
                try {
                    executeSSHcommand();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute(1);
    }

    public void executeSSHcommand(){
        String user = "pi";
        String password = "raspberry";
        String host = "192.168.0.1";
        int port=22;
        try{

            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setTimeout(10000);
            session.connect();
            ChannelExec channel = (ChannelExec)session.openChannel("exec");
            channel.setCommand("omxd n");
            channel.connect();
            channel.disconnect();

        }
        catch(JSchException e){

        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="example_company.controller1.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Button
        android:text="NEXT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/button"
        android:onClick="onCLick11" />

</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="example_company.controller1">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

所以,怎么了!?我错过了什么!?

非常感谢您的帮助和支持。

编辑 1

我在 channel.connect();channel.disconnect 之间添加了 try{Thread.sleep(1000);}catch(Exception ee){} (); 如类似问题中所建议。但没有帮助。仍然是同样的问题。当我按下按钮时,没有任何反应。

编辑 2

gradle build的消息如下:

Information:Gradle tasks [:app:assembleDebug]
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.jcraft.jsch.Channel$1) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.jcraft.jsch.ChannelSftp$1) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.jcraft.jsch.ChannelSftp$2) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.jcraft.jsch.ChannelSftp$3) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.jcraft.jsch.ConfigRepository$1) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.jcraft.jsch.ConfigRepository$2) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.jcraft.jsch.JSch$1) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.jcraft.jsch.Util$1) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
Information:BUILD SUCCESSFUL
Information:Total time: 1 mins 8.318 secs
Information:64 errors
Information:0 warnings
Information:See complete output in console

最佳答案

我通过改变两件事解决了这个问题:

  1. 在连接线和断开线之间添加 try{Thread.sleep(1000);}catch(Exception ee){}。我使用了更高的值,例如 10000,而不是 1000。
  2. RPi 中正在监听 android 的进程已经死了!我用 JuiceSSH 实现了这一点。于是,在RPi中解决了这个进程的问题后,它就活跃起来了。

经过这两项更改后,Android 应用程序运行完美。

感谢@greenapps 和@MartinPrikryl 的帮助。

关于android - 使用 Android 应用程序发送 SSH 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47076122/

相关文章:

Linux - 通过 ssh 隧道启动 recordMyDesktop

c - Pigpio不会在树莓派上初始化

mysql - 无法使用 "startx"启动我的树莓派

tcp - "iot_tls_connect L#143 TCP Connection Error"使用 Raspberry Pi 的基本 AWS 示例

java - 如何在android中的谷歌地图的特定半径内绘制标记

java - 如何使用 xmpp 连接 Facebook 聊天,我想输入 friend 的用户名,然后聊天显示 SASL 身份验证失败

android - java.lang.NoClassDefFoundError : org. ksoap2.serialization.SoapSerializationEnvelope

bash - 如何使用 Bash 脚本中的密码运行 sftp 命令?

linux - 为什么设置了权限却无法启动 SSH shell?

java - 使用 SimpleAdapter 访问 listView 的 View