代码之家  ›  专栏  ›  技术社区  ›  Omid1989

使用android应用程序发送SSH命令[重复]

  •  1
  • Omid1989  · 技术社区  · 7 年前

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

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

    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){
    
            }
        }
    }
    

    活动_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>
    

    那么,怎么了!?我错过了什么!?

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

    我补充道 try{Thread.sleep(1000);}catch(Exception ee){} channel.connect(); channel.disconnect(); 正如在类似问题中所建议的那样。但是没有帮助。还是一样的问题。当我按下按钮时,什么也没有发生。

    编辑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 回复  |  直到 7 年前
        1
  •  3
  •   Omid1989    7 年前

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

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

    在这两次更改之后,android应用程序运行得非常好。

    感谢@greenapps和@MartinPrikryl的帮助。