Android应用很多时候都会涉及到网络,在请求网络出错时,我们可以通过抓包来分析网络请求,返回的数据等,通常我们是用tcpdump这个工具来抓包,再通过wireshark工具来分析生成的文件,关于tcpdump的使,可以从网上查一下,有很多介绍,比如:http://www.cnblogs.com/likwo/archive/2012/09/06/2673944.html。关于如何用wireshark来分析文件,本文不作介绍。

使用adb的命令来操作,还是比较麻烦,所以我写了一个应用,把这些命令封装了起来。实现的最根本的原理是通过Runtime.exec来执行linux命令。

运行截图

实现代码

CommandsHelper.java
/**
*
* @author lihong06
* @since 2014-3-3
*/
public class CommandsHelper {
private static final String NAME = "tcpdump";
private static final String TAG = "CommandsHelper";
public static final String DEST_FILE = Environment.getExternalStorageDirectory() + "/capture.pcap"; public static boolean startCapture(Context context) {
InputStream is = null;
OutputStream os = null;
boolean retVal = false;
try {
AssetManager am = context.getAssets();
is = am.open(NAME);
File sdcardFile = Environment.getExternalStorageDirectory();
File dstFile = new File(sdcardFile, NAME);
os = new FileOutputStream(dstFile); copyStream(is, os); String[] commands = new String[7];
commands[0] = "adb shell";
commands[1] = "su";
commands[2] = "cp -rf " + dstFile.toString() + " /data/local/tcpdump";
commands[3] = "rm -r " + dstFile.toString();
commands[4] = "chmod 777 /data/local/tcpdump";
commands[5] = "cd /data/local";
commands[6] = "tcpdump -p -vv -s 0 -w " + DEST_FILE; execCmd(commands);
} catch (IOException e) {
e.printStackTrace();
Log.i(TAG, " error: " + e.getMessage());
} finally {
closeSafely(is);
closeSafely(os);
} return retVal;
} public static void stopCapture(Context context) {
// 找出所有的带有tcpdump的进程
String[] commands = new String[2];
commands[0] = "adb shell";
commands[1] = "ps|grep tcpdump|grep root|awk '{print $2}'";
Process process = execCmd(commands);
String result = parseInputStream(process.getInputStream());
if (!TextUtils.isEmpty(result)) {
String[] pids = result.split("\n");
if (null != pids) {
String[] killCmds = new String[pids.length];
for (int i = 0; i < pids.length; ++i) {
killCmds[i] = "kill -9 " + pids[i];
}
execCmd(killCmds);
}
}
} public static Process execCmd(String command) {
return execCmd(new String[] { command }, true);
} public static Process execCmd(String[] commands) {
return execCmd(commands, true);
} public static Process execCmd(String[] commands, boolean waitFor) {
Process suProcess = null;
try {
suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
for (String cmd : commands) {
if (!TextUtils.isEmpty(cmd)) {
os.writeBytes(cmd + "\n");
}
}
os.flush();
os.writeBytes("exit\n");
os.flush();
} catch (IOException e) {
e.printStackTrace();
} if (waitFor) {
boolean retval = false;
try {
int suProcessRetval = suProcess.waitFor();
if (255 != suProcessRetval) {
retval = true;
} else {
retval = false;
}
} catch (Exception ex) {
Log.w("Error ejecutando el comando Root", ex);
}
} return suProcess;
} private static void copyStream(InputStream is, OutputStream os) {
final int BUFFER_SIZE = 1024;
try {
byte[] bytes = new byte[BUFFER_SIZE];
for (;;) {
int count = is.read(bytes, 0, BUFFER_SIZE);
if (count == -1) {
break;
} os.write(bytes, 0, count);
}
} catch (IOException e) {
e.printStackTrace();
}
} private static void closeSafely(Closeable is) {
try {
if (null != is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} private static String parseInputStream(InputStream is) {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
StringBuilder sb = new StringBuilder();
try {
while ( (line = br.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
} return sb.toString();
}
}

MainActivity.java

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final TextView textView = (TextView) findViewById(R.id.textView1);
String oldText = textView.getText().toString();
textView.setText(oldText + "\n\n" + "目标文件: " + CommandsHelper.DEST_FILE); findViewById(R.id.start_capture).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setEnabled(false);
new Thread(new Runnable() {
@Override
public void run() {
final boolean retVal = CommandsHelper.startCapture(MainActivity.this);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "startCapture result = " + retVal, Toast.LENGTH_SHORT).show();
}
});
}
}).start();
}
}); findViewById(R.id.stop_capture).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CommandsHelper.stopCapture(MainActivity.this);
findViewById(R.id.start_capture).setEnabled(true);
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

说明

1、手机必须要Root,没有root的我没有测试过,另外,我也只测试了我一台手机,没有覆盖到所有的手机。
2、大家如果发现问题,请告知于我,谢谢。

Android tcpdump抓包应用实现的更多相关文章

  1. Android常用抓包工具之TcpDump

    ➠更多技术干货请戳:听云博客 做为一个测试人员,工作中经常会用到数据抓包工具来进行数据分析和验证,下面就简单介绍一下工作中常用的抓包工具. TcpDump抓包 Tcpdump是一个用于截取网络分组,并 ...

  2. [转] Android利用tcpdump抓包

    原文链接:http://mysuperbaby.iteye.com/blog/902201 Android利用tcpdump抓包 博客分类: Android AndroidAccessGoHTML  ...

  3. Android手机tcpdump抓包

    在开发过程中遇到问题时,无法非常方便的获取到数据包,导致分析解决问题比较麻烦.这里介绍如何在Android手机上实现tcpdump抓包.   1.root机器  在用tcpdump抓包过程中,需要使用 ...

  4. Android利用tcpdump抓包,用wireshark分析包。

    1.前言 主要介绍在android手机上如何利用tcpdump抓包,用wireshark分析包. android tcpdump官网: http://www.androidtcpdump.com/ t ...

  5. Android 常用抓包工具介绍之Charles

    ➠更多技术干货请戳:听云博客 Charles是一款抓包修改工具,相比起TcpDump,charles具有界面简单直观,易于上手,数据请求控制容易,修改简单,抓取数据的开始暂停方便等等优势!前面介绍了如 ...

  6. [转] Android实时抓包分析 : 善用adb调试桥

    Android实时抓包分析 : 善用adb调试桥   谈到android网络抓包,很多人都能想到牛逼轰轰的神器tcpdump.方法就是在android机器上面安装tcpdump,然后通过-w参数把抓包 ...

  7. tcpdump抓包命令

    本文转自 : http://www.cnblogs.com/ggjucheng/archive/2012/01/14/2322659.html http://www.itshouce.com.cn/l ...

  8. TCPdump抓包命令详解--摘

    http://blog.csdn.net/s_k_yliu/article/details/6665673/ http://starsliao.blog.163.com/blog/static/890 ...

  9. Wireshark和TcpDump抓包分析心得

    Wireshark和 TcpDump抓包分析心得  1. Wireshark与tcpdump介绍 Wireshark是一个网络协议检测工具,支持Windows平台和Unix平台,我一般只在Window ...

随机推荐

  1. 把luasocket集成到c++中

    建一个项目pro_test,创建一个运行目录test: 把luasocket/src文件夹中的*.lua拷贝到test/src文件夹中: 把socket.dll,mime.dll,lua5.1.dll ...

  2. python2.7学习记录之二

    一.高级特性 1.切片取前3个元素用L[0:3]表示,从索引0开始取,直到索引3为止,但不包括索引3.如果第一个索引是0可省略.前10个数 每两个取一个L[:10:2],所有数 每5个取一个L[::5 ...

  3. java代码中 路径符号的写法

    String path="D:\\新建文件夹\\2.png"; File file=new File(path); System.out.println(file.exists() ...

  4. hdu_1558_Segment set(并查集+计算几何)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 题意:P为画线段,Q为询问当前这条线段所在的集合有多少线段 题解:如果两条线段有交点,那么就连接 ...

  5. The List ADT

    1.Definiation A list is a sequence.  a0, a1,a2,..., aN (N>0) 2.Character For any list except the ...

  6. Centos下 Nginx安装与配置

    网上找了好多资料.都很难找全,这里以这个目录为主,进行备注. Nginx是一款轻量级的网页服务器.反向代理服务器.相较于Apache.lighttpd具有占有内存少,稳定性高等优势.它最常的用途是提供 ...

  7. Redis和Memcached的区别【转】

    如果简单地比较Redis与Memcached的区别,大多数都会得到以下观点:1 Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,hash等数据结构的存储.2 Redis支持数据 ...

  8. centos 6.2安装bind 9.8.2 master、slave与自动修改后更新

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://dl528888.blog.51cto.com/2382721/1249311 随 ...

  9. js连续赋值、指针

    jq的源码中有很多连续赋值,类似这样的: var a = {n:1}; var b = a; // 持有a,以回查 a.x = a = {n:2}; alert(a.x);// --> unde ...

  10. jsp:usebean 常用注意事项

    bean就是一个Java类,一般来说写这个类应该注意以下几点: 1.必须有包名,不要使用裸类 2.类的第一个名字小写 3.尽量不要使用公共成员变量,使用私有的,通过set.get方法来操作类中的变量 ...