2016-06-24 7 views
1

私はアンドロイドでコマンドを発行して結果を通知するアプリケーションを作ろうとしています。これは、より多くの出力を待つコマンドが正しく処理されないという例外を除いてうまくいっているようです。私はコマンド "su & & nmap -sS 192.168.1.1"を発行しようとしています。出力のために得られるのは、nmapが起動したことだけです。 nmapが開始した出力だけでなく、以下のコードの修正版を使ったスキャンの結果を得る方法を知っている人はいますか?Android java InputStreamReader

try { 
     EditText inputTxt = (EditText) findViewById(R.id.input); 
     String str = inputTxt.getText().toString(); 
     Process command = Runtime.getRuntime().exec(str); 

     BufferedReader reader = new BufferedReader(
       new InputStreamReader(command.getInputStream())); 
     int read; 
     char[] buffer = new char[4096]; 
     StringBuffer output = new StringBuffer(); 
     while ((read = reader.read(buffer)) > 0) { 
      output.append(buffer, 0, read); 
     } 
     reader.close(); 

     TextView textView = (TextView) findViewById(R.id.textView); 
     textView.setText(output); 

    } 
    catch (IOException e) { 
     Log.e("Run_Command", Log.getStackTraceString(e)); 
    } 
+1

「su」は最初にパスワードを待っていますか? –

+0

プロセスへの入力である出力ストリームを閉じる必要があります。 – EJP

答えて

-1

私は別のバージョンのコードを思いついた。これはthread.sleepを使用し、固定サイズのバッファを持っていません。

最終的には、最終的なthread.sleepが完了した時点でスキャンが完了しているにもかかわらず、nmapが開始され、スキャンの結果ではなく出力されます。

public void command(View view) 
{ 
    String me; 
    int a = 0; 
    try { 
     EditText inputTxt = (EditText) findViewById(R.id.input); 
     String str = inputTxt.getText().toString(); 
     Process command = Runtime.getRuntime().exec(str); 

     BufferedReader bReader = new BufferedReader(
       new InputStreamReader(command.getInputStream(), "UTF8")); 
     //char [] me = new char[40960]; 
     String inputLine; 
     while(1 == 1){ 
      if(a == 5) { 
       break;} 

     while ((inputLine = bReader.readLine()) != null) { 
      me += inputLine + "\n"; 
     } 
      while(bReader.readLine() == null) { 
       a++; 
       Thread.sleep(5000); 
       if (a == 5) { 
        break; 
       } 
      } 
        } 
     bReader.close(); 

     TextView textView = (TextView) findViewById(R.id.textView); 
     textView.setText(me); 

    } 
    catch (IOException e) { 
     Log.e("Run_Command", Log.getStackTraceString(e)); 
    } 
    catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
} 
+2

無意味です。 'readLine()'がnullを返すと、それだけです。あなたはDoomsdayまで今から眠ることができますが、これ以上の入力はありません。 – EJP

+0

出力を取得する方法が必要ですか?私は間違った方法でそれについて行っているかもしれません? – Fuion

関連する問題