2014-05-04 110 views
1

我目前正在嘗試編寫一個通過ssh向遠程計算機執行簡單「ls」命令的android應用程序。我試過使用JSch和this page,但它似乎不工作。我環顧四周,似乎我必須執行一些異步任務來運行代碼。下面是主要活動的整個代碼現在:實現JSch在Android應用程序中運行ssh命令

package com.sshtest.sshtest; 

import android.app.Activity; 
import android.os.Bundle; 
import android.os.AsyncTask; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.TextView; 
import java.util.Properties; 
import com.jcraft.jsch.JSch; 
import com.jcraft.jsch.Session; 
import com.jcraft.jsch.Channel; 
import com.jcraft.jsch.ChannelExec; 
import com.jcraft.jsch.*; 

import java.io.InputStream; 
import java.io.ByteArrayOutputStream; 
import java.lang.String; 




public class MainActivity extends Activity { 

    String string; 

    @Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 


private class sshconnection extends AsyncTask<String, Void, String> { 

    protected String doInBackground(String... params) { 

     try { 
      JSch jsch = new JSch(); 
      Session session = jsch.getSession("name", "ipaddress", 22); 
      session.setPassword("password"); 

      // Avoid asking for key confirmation 
      Properties prop = new Properties(); 
      prop.put("StrictHostKeyChecking", "no"); 
      session.setConfig(prop); 

      session.connect(); 

      ChannelExec channel = (ChannelExec)session.openChannel("exec"); 
      channel.setCommand("ls"); 
      channel.connect(); 

      InputStream input = channel.getInputStream(); 


      channel.disconnect(); 






     } catch (Exception e) { 
      System.out.println(e.getMessage()); 
     } 
     return null; 


    } 

    @Override 
    protected void onPostExecute(String result) { 
     TextView txt = (TextView) findViewById(R.id.txtipinfo); 
     txt.setText("Executed"); 

    } 


} 
  • 添加使用許可權的android:NAME = 「android.permission.INTERNET對」 以AndroidManifest.xml中

這是我第一次嘗試任何嚴肅的編碼,如果任何人都可以幫助它將不勝感激。謝謝!

+0

你在哪裏打電話給你的'AsyncTask'? – iTech

+0

啊,它在onCreate「sshconnection.execute();」但是我得到了一個非靜態的引用錯誤,我一直在試圖找出它。 – user3602524

回答

0

如果你只是想測試。不要嘗試使用的AsyncTask,但只是單純的線......這樣

 new Thread(new Runnable() { 
     @Override 
     public void run() { 

      Session session; 
      JSch jsch; 
      try { 
       jsch = new JSch(); 
       jsch.addIdentity(privateKey, "yourprivatekey"); 
       session = jsch.getSession("git", "github.com", 22); 
       session.setPassword("yourpass"); 

       // Avoid asking for key confirmation 
       Properties prop = new Properties(); 
       prop.put("StrictHostKeyChecking", "no"); 
       session.setConfig(prop); 

       session.connect(); 

       if(session.isConnected()){ 
        System.out.println(this.getClass().getSimpleName() + " CONNECTED"); 
        System.out.println(this.getClass().getSimpleName() + " YOO " + jsch.getIdentityRepository().getName()+" "+session.getClientVersion() + " " + session.isConnected()); 
       }else{ 
        System.out.println(this.getClass().getSimpleName() + " NOT CONNECTED"); 
       } 
      }catch (Exception e){ 
       e.printStackTrace(); 
      } 
     } 
    }).start(); 

說它在你的主要活動的onCreate()方法。 希望它有幫助!

相關問題