android系统中使用tcpdump抓包的方法及代码 - Linux - 服务器之家

服务器之家

专注于服务器技术!
当前位置:首页 > 服务器系统 > Linux

android系统中使用tcpdump抓包的方法及代码

发布时间:2014-04-19 来源:服务器之家

整了4,5天,终于有了点样子,好歹是能够读出来了,而且不是乱码.由于Runtime.getRuntime().exec()不能进行重定向,纠结了2天,还好是google给力找到了有用的文章:

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

Runtime.getRuntime().exec(cmd);   关键句子,用来将抓到的包数据放到lishuo2.txt文件中

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);

}

}                 关键句子,用来将数据读出放到text_view控件中

 

如何结束tcpdump还在研究中,具体的界面比较难看,反正以后还要增加功能,先不管了.

学习心得4:在android中用tcpdump抓包并且显示

学习心得4:在android中用tcpdump抓包并且显示

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(Bundle savedInstanceState) {

        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.OnClickListener start_button_listener = new Button.OnClickListener(){

    public void onClick(View v){

try {

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

Runtime.getRuntime().exec(cmd);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

    }   

    };

    private Button.OnClickListener stop_button_listener = new Button.OnClickListener(){

    public void onClick(View v){

try {

Runtime.getRuntime().exec("killall tcpdump");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

    }   

    };

    private Button.OnClickListener read_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().exec(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);

}

}

}

先这样,懒得再排版,反正以后还要完善功能.