私の人生では、アプリが su シェル内から busybox を呼び出すプロセスからの応答を取得することはできません。
3つの異なる方法を試し、3つの方法を組み合わせて動作させようとしましたが、busyboxを使用して何かから出力を取得することはできず、残りのコマンドのみを取得しました.
より具体的には、ls /data
やのようなコマンドを返すようにできますcat suchandsuch.file
が、「busybox」で始まるもの (つまり、busybox マウント、busybox フリー) は何も表示されません。
これは私に最も近い方法でした。このコードは で動作しますls /data
が、「busybox free」では動作しません
これはコマンドを実行し (ほとんどの場合)、入力ストリームから無限にループする代わりに空の文字列を返します。
Process p;
try {
p = Runtime.getRuntime().exec(new String[]{"su", "-c", "/system/bin/sh"});
DataOutputStream stdin = new DataOutputStream(p.getOutputStream());
stdin.writeBytes("ls /data\n");
DataInputStream stdout = new DataInputStream(p.getInputStream());
byte[] buffer = new byte[4096];
int read = 0;
String out = new String();
while(true){
read = stdout.read(buffer);
out += new String(buffer, 0, read);
if(read<4096){
break;
}
}
Toast.makeText(getApplicationContext(), out, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
下部近くのトーストには からすべてが表示ls /data
されますが、busybox の何かに変更されると、空白または null になります。
私もこれらの両方を試しましたが、どちらも機能しませんでした。(コマンドの実行後にプロセスを渡していました。)
これらはどちらも、メソッドのボタンを押すと、常にアプリがフリーズする結果になります。
String termReader(Process process){
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
try {
int i;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((i = reader.read(buffer)) > 0)
output.append(buffer, 0, i);
reader.close();
return output.toString();
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}
}
String processReader(Process process){
InputStream stdout = process.getInputStream();
byte[] buffer = new byte[1024];
int read;
String out = new String();
while(true){
try {
read = stdout.read(buffer);
out += new String(buffer, 0, read);
if(read<1024){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return out;
}
作業するスタック トレースがないため、少し困惑し始めています。
以下に提案されたコードで編集されました.
これは、入力ストリームを読み込もうとするとフリーズしstdin.writeBytes("exit\n")
ます。ストリームを読み込もうとする前に呼び出すと、端末を閉じることから空白の答えが返されます。後で呼び出すと、無限にループします。
void Run() {
String command = "busybox traceroute\n";
StringBuffer theRun = null;
try {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream stdin = new DataOutputStream(process.getOutputStream());
stdin.writeBytes(command);
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
theRun = output.append(buffer, 0, read);
}
reader.close();
process.waitFor();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Toast.makeText(getApplicationContext(), theRun, Toast.LENGTH_SHORT).show();
}
最初の行 (コマンドを呼び出すたびに取得する busybox 情報行) をスキップし、残りのデータをキャッチしていないようです。これを正しく機能させるために、考えられるすべてのバリエーションを試しました:/
誰かがこれについて洞察を持っているなら、私は大いに感謝しています:)