2011-07-31 17 views
0

私は、Iptablesでエントリを追加するために自分のコードでshellコマンドを実行しようとしています。以下は、ここでのiptablesを更新するためのシェルコマンドとwriteIptablesを()構築するための2つの方法がすなわち、createshellCommand()がある アンドロイドでシェルコマンドを実行するには?

private void createShellCommand(String uidValue_param, String interface_param) { 
    // TODO Auto-generated method stub 
    StringBuilder cmdScript=new StringBuilder(); 
    script.append("iptables -A OUTPUT " + interface_param + "-m owner --uid-owner " + uidValue_param + "-j REJECT"); 
    writeIptables(cmdScript); 

} 


private void writeIptables(StringBuilder scriptfile) { 
    // TODO Auto-generated method stub 
    String cmd=scriptfile.toString(); 
    if(RootTools.isRootAvailable()) 
    { 
     Process exec; 
     try { 
      exec = Runtime.getRuntime().exec(new String[]{"su","-c"}); 
      final OutputStreamWriter out = new OutputStreamWriter(exec.getOutputStream()); 
      out.write(cmd); 

      // Terminating the "su" session 
      out.write("exit\n"); 
      out.flush();  
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      Log.e("IPtables updation failed", "Iptable write failed"+e); 
     } 

    } 
    else 
    { 
     Log.e("Root Access denied", "Access Denied"); 
    } 
} 

コード

の私の作品です。私は実行するたびしかし、プログラムlogcatは、次の警告

「拒否W 19913 SU要求(0-> 0 /システム/ binに/ SH)」

が表示されているしかし、私は手動でコマンドプロンプトを通じてエントリを追加することができますし、そのiptablesに追加するが、上記のコードを使用して更新しないでください。私の電話は根っこがあり、私はLinuxカーネル2.6.29でアンドロイド2.3.4を使用しています。そして私はルートアクセスをチェックするために外部ライブラリ "Roottools"を使用しています。

エラーを修正するのを手伝ってください。

答えて

4

この作品:

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    execute_reboot(); 

} 
void execute_reboot() 
{ 
    Process reboot; 
    try { 
     reboot=Runtime.getRuntime().exec("su"); 
      DataOutputStream os = 
       new DataOutputStream(reboot.getOutputStream()); 
      os.writeBytes("reboot\n"); 
      os.flush(); 

       os.writeBytes("exit\n"); 
       os.flush(); 

      reboot.waitFor(); 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

このコードは正常に動作します。あなたのプログラムには小さな間違いがいくつかあります。貼り付けたものを試してみてください。その働きの魅力。ではごきげんよう。私はそれを理解しやすいようにできるだけシンプルにしました。あなたはまだあなたのコーディングを飾るために配列や他のものを使用することができます。

と同じものでも、複数の引数を渡す必要があるのchmodコマンドのために働くYAA

。このため

だけ

"os.writeBytes(" リブートを\ n ")を交換;"

"chmodの777の/ dev/video0 \ n"(または任意の他のシステムファイル)と

ありがとうございました。私が何かできることがあれば教えてください。

+0

提案をいただきありがとうございます... – Unnikrishnan

0

(sudoをしておよびなし)iptablesコマンドを使用して、だけではなく、ファイルをconfigのiptablesのつかうしようとしています。

1
public static void rebootNow() { 
    Log.d(TAG, "Rebooting"); 
    try { 
     @SuppressWarnings("unused") 
     Process proc = Runtime.getRuntime().exec(
       new String[] { "su", "-c", "reboot" }); 
    } catch (Exception ex) { 
     Log.d(TAG, "Rebooting failed (Terminal Error)"); 
    } 
} 

この1つはあなたが追加することができ、もう少しコンパクト

ある "proc.waitForを();"プロセスproc ...行の後に未使用の警告を取り除くためにデバイスを再起動するのに数秒かかりますし、UIスレッドで数秒間いくつかの機能を無効にしたい場合は、プロセスが終了するために。

関連する問題