【Android 应用开发】Android 上实现非root的 Traceroute -- 非Root权限下移植可执行二进制文件 脚本文件(二)

简介: 【Android 应用开发】Android 上实现非root的 Traceroute -- 非Root权限下移植可执行二进制文件 脚本文件(二)

4. 代码示例



MainActivity 主程序代码 :



package cn.org.octopus.tracerouteandbusybox;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
/** 看不懂注释我就吃半斤狗粮 :-) */
public class MainActivity extends ActionBarActivity {
  private EditText et_cmd;
  private String app_path;
  private TextView tv_result;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.home_activity);
  /*初始化控件*/
  et_cmd = (EditText) findViewById(R.id.et_cmd);
  tv_result = (TextView) findViewById(R.id.tv_result);
  /* 获取app安装路径 */
  app_path = getApplicationContext().getFilesDir().getAbsolutePath();
  }
  /** 按钮点击事件 */
  public void onClick(View view) {
  int id = view.getId();
  switch (id) {
  case R.id.copy_busybox: /* 拷贝busybox可执行文件 */
    varifyFile(getApplicationContext(), "busybox");
    break;
  case R.id.copy_traceroute:/* 拷贝traceroute可执行文件 */
    varifyFile(getApplicationContext(), "traceroute");
    break;
  case R.id.exe_busybox:/* 将busybox命令添加到Editext中 */
    String cmd = "." + app_path + "/busybox";
    System.out.println(et_cmd);
    et_cmd.setText(cmd);
    break;
  case R.id.exe_traceroute:/* 将traceroute命令添加到Editext中 */
    cmd = "." + app_path + "/traceroute 8.8.8.8";
    et_cmd.setText(cmd);
    break;
  case R.id.exe: /* 执行Editext中的命令 */
    cmd = et_cmd.getText().toString();
    /* 执行脚本命令 */
    List<String> results = exe(cmd);
    String result = "";
    /* 将结果转换成字符串, 输出到 TextView中 */
    for(String line : results){
    result += line + "\n";
    }
    tv_result.setText(result);
    break;
  default:
    break;
  }
  }
  /** 验证文件是否存在, 如果不存在就拷贝 */
  private void varifyFile(Context context, String fileName) {
        try {
          /* 查看文件是否存在, 如果不存在就会走异常中的代码 */
          context.openFileInput(fileName);
        } catch (FileNotFoundException notfoundE) {
            try {
              /* 拷贝文件到app安装目录的files目录下 */
                copyFromAssets(context, fileName, fileName);
                /* 修改文件权限脚本 */
                String script = "chmod 700 " + app_path + "/" + fileName;
                /* 执行脚本 */
                exe(script);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  /** 将文件从assets目录中拷贝到app安装目录的files目录下 */
  private void copyFromAssets(Context context, String source,
    String destination) throws IOException {
  /* 获取assets目录下文件的输入流 */
  InputStream is = context.getAssets().open(source);
  /* 获取文件大小 */
  int size = is.available();
  /* 创建文件的缓冲区 */
  byte[] buffer = new byte[size];
  /* 将文件读取到缓冲区中 */
  is.read(buffer);
  /* 关闭输入流 */
  is.close();
  /* 打开app安装目录文件的输出流 */
  FileOutputStream output = context.openFileOutput(destination,
    Context.MODE_PRIVATE);
  /* 将文件从缓冲区中写出到内存中 */
  output.write(buffer);
  /* 关闭输出流 */
  output.close();
  }
  /** 执行 shell 脚本命令 */
  private List<String> exe(String cmd) {
  /* 获取执行工具 */
  Process process = null; 
  /* 存放脚本执行结果 */
        List<String> list = new ArrayList<String>();  
        try {  
          /* 获取运行时环境 */
          Runtime runtime = Runtime.getRuntime();
          /* 执行脚本 */
            process = runtime.exec(cmd); 
            /* 获取脚本结果的输入流 */
            InputStream is = process.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line = null;  
            /* 逐行读取脚本执行结果 */
            while ((line = br.readLine()) != null) {  
                list.add(line); 
            }
            br.close(); 
        } catch (IOException e) {  
            e.printStackTrace();  
        } 
        return list;
  }
}




home_activity.xml 布局文件代码 :



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/copy_busybox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="拷贝busybox"
            android:textSize="7dp"
            android:textStyle="bold" />
        <Button
            android:id="@+id/copy_traceroute"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="拷贝traceroute"
            android:textSize="7dp"
            android:textStyle="bold" />
        <Button
            android:id="@+id/exe_busybox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="执行busybox"
            android:textSize="7dp"
            android:textStyle="bold" />
        <Button
            android:id="@+id/exe_traceroute"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="执行traceroute"
            android:textSize="7dp"
            android:textStyle="bold" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <EditText
            android:id="@+id/et_cmd"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:hint="输入要执行的命令"
            android:textStyle="bold" />
        <Button
            android:id="@+id/exe"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onClick"
            android:text="执行"
            android:textSize="10dp"
            android:textStyle="bold" />
    </LinearLayout>
    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000"
        android:textColor="#FFF"
        android:textSize="10dp"
        android:textStyle="bold" />
</LinearLayout>



5. 执行结果



执行 busybox 程序 :

image.png





执行 traceroute 程序 :

image.png






示例代码下载 :


-- CSDN : http://download.csdn.net/detail/han1202012/7639253;


-- GitHub : https://github.com/han1202012/TracerouteAndBusybox ;


目录
相关文章
|
7月前
|
Android开发 开发者
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
本文详细介绍了如何通过自定义 `attrs.xml` 文件实现 Android 自定义 View 的属性配置。以一个包含 TextView 和 ImageView 的 DemoView 为例,讲解了如何使用自定义属性动态改变文字内容和控制图片显示隐藏。同时,通过设置布尔值和点击事件,实现了图片状态的切换功能。代码中展示了如何在构造函数中解析自定义属性,并通过方法 `setSetting0n` 和 `setbackeguang` 实现功能逻辑的优化与封装。此示例帮助开发者更好地理解自定义 View 的开发流程与 attrs.xml 的实际应用。
178 2
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
|
7月前
|
Java Android开发
Android studio中build.gradle文件简单介绍
本文解析了Android项目中build.gradle文件的作用,包括jcenter仓库配置、模块类型定义、包名设置及依赖管理,涵盖本地、库和远程依赖的区别。
620 19
|
10月前
|
移动开发 安全 Java
Android历史版本与APK文件结构
通过以上内容,您可以全面了解Android的历史版本及其主要特性,同时掌握APK文件的结构和各部分的作用。这些知识对于理解Android应用的开发和发布过程非常重要,也有助于在实际开发中进行高效的应用管理和优化。希望这些内容对您的学习和工作有所帮助。
975 83
|
7月前
|
存储 XML Java
Android 文件数据储存之内部储存 + 外部储存
简介:本文详细介绍了Android内部存储与外部存储的使用方法及核心原理。内部存储位于手机内存中,默认私有,适合存储SharedPreferences、SQLite数据库等重要数据,应用卸载后数据会被清除。外部存储包括公共文件和私有文件,支持SD卡或内部不可移除存储,需申请权限访问。文章通过代码示例展示了如何保存、读取、追加、删除文件以及将图片保存到系统相册的操作,帮助开发者理解存储机制并实现相关功能。
1787 2
|
ARouter Android开发
Android不同module布局文件重名被覆盖
Android不同module布局文件重名被覆盖
|
Java Android开发 C++
Android Studio JNI 使用模板:c/cpp源文件的集成编译,快速上手
本文提供了一个Android Studio中JNI使用的模板,包括创建C/C++源文件、编辑CMakeLists.txt、编写JNI接口代码、配置build.gradle以及编译生成.so库的详细步骤,以帮助开发者快速上手Android平台的JNI开发和编译过程。
1035 1
|
存储 数据库 Android开发
安卓Jetpack Compose+Kotlin,支持从本地添加音频文件到播放列表,支持删除,使用ExoPlayer播放音乐
为了在UI界面添加用于添加和删除本地音乐文件的按钮,以及相关的播放功能,你需要实现以下几个步骤: 1. **集成用户选择本地音乐**:允许用户从设备中选择音乐文件。 2. **创建UI按钮**:在界面中创建添加和删除按钮。 3. **数据库功能**:使用Room数据库来存储音频文件信息。 4. **更新ViewModel**:处理添加、删除和播放音频文件的逻辑。 5. **UI实现**:在UI层支持添加、删除音乐以及播放功能。
|
存储 监控 数据库
Android经典实战之OkDownload的文件分段下载及合成原理
本文介绍了 OkDownload,一个高效的 Android 下载引擎,支持多线程下载、断点续传等功能。文章详细描述了文件分段下载及合成原理,包括任务创建、断点续传、并行下载等步骤,并展示了如何通过多种机制保证下载的稳定性和完整性。
577 1
|
开发工具 git 索引
repo sync 更新源码 android-12.0.0_r34, fatal: 不能重置索引文件至版本 ‘v2.27^0‘。
本文描述了在更新AOSP 12源码时遇到的repo同步错误,并提供了通过手动git pull更新repo工具来解决这一问题的方法。
578 1
|
ARouter Android开发
Android不同module布局文件重名被覆盖
Android不同module布局文件重名被覆盖
812 0