Android SSH什么都不做

时间:2016-10-13 14:25:10

标签: android ssh

我创建了一个应用程序,它与我的覆盆子Pi连接到ssh并执行python文件。当我从Windows PC连接时,命令和python文件就像魅力一样,但应用程序由于某种原因没有做任何事情。

以下是代码:

import android.content.pm.PackageInstaller;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Properties;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

import java.io.ByteArrayOutputStream;
import java.util.Properties;


public class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new AsyncTask<Integer, Void, Void>() {
            @Override
            protected Void doInBackground(Integer... params) {
                try {
                    executeRemoteCommand("pi", "123456", "192.168.1.6", 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("cd /home/pi/Desktop | nohup python red.py");
        //System.out.print("cd /home/pi/Desktop | nohup python red.py");
        channelssh.connect();
        channelssh.disconnect();

        return baos.toString();
    }
}

这是Android Studio输出:

10/13 17:22:17: Launching app
No apk changes detected since last installation, skipping installation of D:\Android\Projects\LEDControl\app\build\outputs\apk\app-debug.apk
$ adb shell am force-stop com.example.seth.ledcontrol
$ adb shell am start -n "com.example.seth.ledcontrol/com.example.seth.ledcontrol.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 6987 on device samsung-gt_i9195-02de24f3
I/InstantRun: Instant Run Runtime started. Android package is com.example.seth.ledcontrol, real application class is null.
W/InstantRun: No instant run dex files added to classpath
I/PersonaManager: getPersonaService() name persona_policy
W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
W/dalvikvm: VFY: unable to resolve interface method 15423: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
W/dalvikvm: VFY: unable to resolve interface method 15427: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
W/dalvikvm: VFY: unable to resolve virtual method 658: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
W/dalvikvm: VFY: unable to resolve virtual method 680: Landroid/content/res/TypedArray;.getType (I)I
D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable
W/dalvikvm: VFY: unable to resolve virtual method 621: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity
W/dalvikvm: VFY: unable to resolve virtual method 623: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
I/Adreno-EGL: <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8960_KK_2.7_RB1__release_AU ()
              OpenGL ES Shader Compiler Version: 17.01.12.SPL
              Build Date: 03/25/14 Tue
              Local Branch: 
              Remote Branch: quic/kk_2.7_rb1.32
              Local Patches: NONE
              Reconstruct Branch: NOTHING
D/OpenGLRenderer: Enabling debug mode 0
I/dalvikvm: Total arena pages for JIT: 11
I/dalvikvm: Total arena pages for JIT: 12

1 个答案:

答案 0 :(得分:0)

cd无法像那样管道传输

为什么不使用程序的完整路径?

nohup python /home/pi/Desktop/red.py