I want to develop one android application, which i can capture network packets via tcpdump. I have rooted my android emulator device and i have installed tcpdump for android. I can run tcpdump from terminal emulator regurarly.
Now i want to develop one android app that it can execute the following command:
tcpdump > test
My java class is:
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();
}
}
and my android xml file is:
<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>
While application is running, i cant find test txt file, from command:
p = Runtime.getRuntime().exec( "tcpdump > test" )
Thanks.
First, copy your tcpdump file in a well-known folder. Lets say, you use /sdcard/myfolder
Then, do the following:
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"});
Of course, you can also use getExternalstoragedirectory
to choose a folder and copy the tcpdump file in that