【问题标题】:How execute an another program (tcpdump) in android application?如何在 android 应用程序中执行另一个程序(tcpdump)?
【发布时间】:2015-10-24 17:04:37
【问题描述】:

我想开发一个 android 应用程序,我可以通过 tcpdump 捕获网络数据包。我已经植根了我的 android 模拟器设备,并且我已经为 android 安装了 tcpdump。我可以从终端仿真器 regurarly 运行 tcpdump。

现在我想开发一个可以执行以下命令的安卓应用:

tcpdump > 测试

我的java类是:

package com.example.test1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends Activity 
{

    private Process p;
    private TextView error;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);     
    }

    public void tcpdump( View v )
    {
        try 
        {
            p = Runtime.getRuntime().exec( "su" );
            p = Runtime.getRuntime().exec( "tcpdump > test" );
        }
        catch ( Exception e ) 
        {
            error = (TextView) findViewById( R.id.error_message );  
            error.setText( "1" );
        }
    }

    public void stop ( View v )
    {
        p.destroy();
    }
}

我的 android xml 文件是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.test1.MainActivity" >

    <TextView
        android:id="@+id/error_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="27dp"
        android:layout_marginTop="124dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="Sniff"
        android:onClick="tcpdump" />

    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="88dp"
        android:text="Stop"
        android:onClick="stop" />

</RelativeLayout>

当应用程序运行时,我无法从命令中找到测试 txt 文件:

p = Runtime.getRuntime().exec("tcpdump > test")

谢谢。

【问题讨论】:

    标签: java android tcpdump


    【解决方案1】:

    首先,将您的 tcpdump 文件复制到一个众所周知的文件夹中。比方说,你使用/sdcard/myfolder

    然后,执行以下操作:

     Runtime.getRuntime().exec(new String[]{"su", "-c", "chmod 777 /sdcard/my_folder/tcpdump"});
     Runtime.getRuntime().exec(new String[]{"su", "-c", "/sdcard/my_folder/tcpdump > /sdcard/my_folder/test"});
    

    当然你也可以用getExternalstoragedirectory选择一个文件夹,把tcpdump文件复制进去

    【讨论】:

    • 这和你打算做的一样,但是因为我们使用的是 Linux,所以像 /sdcard/myfolder/tcpdump 这样的完全限定的文件路径更容易。此外,你知道你的输出文件将在哪里制作
    • 如果我不使用字符串数组,为什么不创建测试 txt 文件?对不起我的英语不好。
    • 该命令实际上并未执行 tcpdump。它实际上是在执行 su 命令。 “-c”是传递给su的标志,其余的是命令。因此,实际执行的是“su -c tcpdump > test”。希望对你有意义
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多