How to use tcpdump to capture packets and code in android

Source: Internet
Author: User
Objective c () cannot be redirected. it has been entangled for two days. Fortunately, google has found a useful article: String [] cmd {sh,-c, tcpdump-vv-l/sdcard/lishuo2.txt}; Runtime. getRun

Objective c () cannot be redirected. it has been entangled for two days. Fortunately, google has found a useful article:

String [] cmd = {"sh", "-c ","Tcpdump-Vv-l>/sdcard/lishuo2.txt "};

Runtime.getruntime(cmd.exe c (cmd );Key period, used to store the captured packet data in the lishuo2.txt file

Private void find_and_modify_text_view () throws IOException{

FileReader fr = new FileReader ("/mnt/sdcard/lishuo2.txt ");

BufferedReader br = new BufferedReader (fr );

String str = null;

TextView text_view = (TextView) findViewById (R. id. text_view );

While (str = br. readLine ())! = Null ){

Str = br. readLine ();

Text_view.append ("\ n" + str );

}

} Key sentence, used to read data into the text_view control

 

How to End tcpdump is still under study. the specific interface is ugly. you need to add functions later.

MainActivity. java:

Package lishuo. hook;

Import java. io. IOException;

Import android. app. Activity;

Import android. content. Intent;

Import android. OS. Bundle;

Import android. util. Log;

Import android. view. View;

Import android. widget. Button;

Public class MainActivity extends Activity {

 @ Override

 Public void onCreate (BundlesavedInstanceState ){

 Super. onCreate (savedInstanceState );

 SetContentView (R. layout. main );

 Start_button ();

 Stop_button ();

 Read_button ();

 }

 Private void start_button (){

 Button button = (Button) findViewById (R. id. start_button );

 Button. setOnClickListener (start_button_listener );

 }

 Private void stop_button (){

 Button button = (Button) findViewById (R. id. stop_button );

 Button. setOnClickListener (stop_button_listener );

 }

 Private void read_button (){

 Button button = (Button) findViewById (R. id. read_button );

 Button. setOnClickListener (read_button_listener );

 }

 Private Button. OnClickListenerstart_button_listener = new Button. OnClickListener (){

 Public void onClick (View v ){

Try {

String [] cmd = {"sh", "-c", "tcpdump-Vv-l>/sdcard/lishuo2.txt "};

Runtime.getruntime(cmd.exe c (cmd );

} Catch (IOException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

 } 

 };

 Private Button. OnClickListenerstop_button_listener = new Button. OnClickListener (){

 Public void onClick (View v ){

Try {

Runtime.getruntime(cmd.exe c ("killall tcpdump ");

} Catch (IOException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

 } 

 };

 Private Button. OnClickListenerread_button_listener = new Button. OnClickListener (){

 Public void onClick (View v ){

 //Try {

 //String [] cmd = {"sh", "-c", "tcpdump-r/sdcard/lishuo1.txt>/sdcard/lishuo8.txt "};

// Runtime.getruntime(cmd.exe c (cmd );

//} Catch (IOException e ){

// TODO Auto-generated catch block

// E. printStackTrace ();

//}

 Intent intent = new Intent ();

 Intent. setClass (MainActivity. this, ViewTextActivity. class );

 StartActivity (intent );

 } 

 };

}

ViewTextActivity. java

Package lishuo. hook;

Import android. app. Activity;

Import android. content. Intent;

Import android. OS. Bundle;

Import android. util. Log;

Import android. view. View;

Import android. widget. Button;

Import android. widget. CheckBox;

Import android. widget. TextView;

Import java. io .*;

Public class ViewTextActivity extends Activity {

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetTitle ("ViewTextActivity ");

SetContentView (R. layout. text );

Try {

Find_and_modify_text_view ();

} Catch (IOException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

}

Private void find_and_modify_text_view () throws IOException{

FileReader fr = new FileReader ("/mnt/sdcard/lishuo2.txt ");

BufferedReader br = new BufferedReader (fr );

String str = null;

TextView text_view = (TextView) findViewById (R. id. text_view );

// CharSequence text_view_old = text_view.getText ();

While (str = br. readLine ())! = Null ){

Str = br. readLine ();

Text_view.append ("\ n" + str );

}

}

}

In this case, you are too lazy to typeset. you need to improve the functions later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.