2016-10-13 133 views
0

我創建了一個應用程序,該應用程序通過我的樹莓派連接到ssh並執行一個python文件。當我從我的Windows PC連接時,該命令和python文件就像一個魅力工作,但由於某種原因,該應用程序不做任何事情。Android SSH什麼都不做

下面是代碼:

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工作室輸出:

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 
+0

這logcat的似乎並不被過濾爲您的應用程序 –

+0

不知道該怎麼做... –

+0

Android Studio中有一個下拉旁邊輸出的選項來過濾。基本上,我想指出的是,那裏沒有錯誤,也沒有來自您的應用程序的消息 –

回答

0

cd不能你爲什麼不使用完整路徑通過管道像

到程序?

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

Thx!工作!我使用了以前的命令,因爲這是我從我的Windows PC終端連接! –

+0

我不認爲它應該從任何與這樣的管道連接。這是正確的用法 –