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


目录
相关文章
|
2月前
|
Java Android开发 C++
Android Studio JNI 使用模板:c/cpp源文件的集成编译,快速上手
本文提供了一个Android Studio中JNI使用的模板,包括创建C/C++源文件、编辑CMakeLists.txt、编写JNI接口代码、配置build.gradle以及编译生成.so库的详细步骤,以帮助开发者快速上手Android平台的JNI开发和编译过程。
91 1
|
2月前
|
存储 安全 Android开发
"解锁Android权限迷宫:一场惊心动魄的动态权限请求之旅,让你的应用从平凡跃升至用户心尖的宠儿!"
【8月更文挑战第13天】随着Android系统的更新,权限管理变得至关重要。尤其从Android 6.0起,引入了动态权限请求,增强了用户隐私保护并要求开发者实现更精细的权限控制。本文采用问答形式,深入探讨动态权限请求机制与最佳实践,并提供示例代码。首先解释了动态权限的概念及其重要性;接着详述实现步骤:定义、检查、请求权限及处理结果;最后总结了六大最佳实践,包括适时请求、解释原因、提供替代方案、妥善处理拒绝情况、适应权限变更及兼容旧版系统,帮助开发者打造安全易用的应用。
47 0
|
16天前
|
存储 API Android开发
"解锁Android权限迷宫:一场惊心动魄的动态权限请求之旅,让你的应用从平凡跃升至用户心尖的宠儿!"
随着Android系统的更新,权限管理成为应用开发的关键。尤其在Android 6.0(API 级别 23)后,动态权限请求机制的引入提升了用户隐私保护,要求开发者进行更精细的权限管理。
43 2
|
2月前
|
开发工具 git 索引
repo sync 更新源码 android-12.0.0_r34, fatal: 不能重置索引文件至版本 ‘v2.27^0‘。
本文描述了在更新AOSP 12源码时遇到的repo同步错误,并提供了通过手动git pull更新repo工具来解决这一问题的方法。
57 1
|
2月前
|
存储 监控 数据库
Android经典实战之OkDownload的文件分段下载及合成原理
本文介绍了 OkDownload,一个高效的 Android 下载引擎,支持多线程下载、断点续传等功能。文章详细描述了文件分段下载及合成原理,包括任务创建、断点续传、并行下载等步骤,并展示了如何通过多种机制保证下载的稳定性和完整性。
35 0
|
4月前
|
Java 开发工具 Android开发
详细解读Android开发DNK开发将.c文件打包成os
详细解读Android开发DNK开发将.c文件打包成os
25 0
|
4月前
|
缓存 Android开发 Kotlin
【安卓app开发】kotlin Jetpack Compose框架 | 先用OKhttp下载远程音频文件再使用ExoPlayer播放
使用 Kotlin 的 Jetpack Compose 开发安卓应用时,可以结合 OkHttp 下载远程音频文件和 ExoPlayer 进行播放。在 `build.gradle` 添加相关依赖后,示例代码展示了如何下载音频并用 ExoPlayer 播放。代码包括添加依赖、下载文件、播放文件及简单的 Compose UI。注意,示例未包含完整错误处理和资源释放,实际应用需补充这些内容。
|
4月前
|
存储 Android开发 Kotlin
开发安卓app OKhttp下载后使用MediaPlayer播放
在Android Jetpack Compose应用程序中,要使用OkHttp下载远程音频文件并在本地播放,你需要完成以下几个步骤: 1. **添加依赖**:确保`build.gradle`文件包含OkHttp和Jetpack Compose的相关依赖。 2. **下载逻辑**:创建一个`suspend`函数,使用OkHttp发起网络请求下载音频文件到本地。 3. **播放逻辑**:利用`MediaPlayer`管理音频播放状态。 4. **Compose UI**:构建用户界面,包含下载和播放音频的按钮。
|
Web App开发 Android开发
Android 使用 aria2c + 百度网盘助手下载百度云文件
首先表明,这是一篇我转载的文章 原为知乎专题 :https://zhuanlan.zhihu.com/p/26873167?group_id=846415883457949696 闲的无聊,突然想到自己在 Android 上下百度云的东西很麻烦,而且不想用山寨云什么的,如果能在 Android 也能实现 aria2c + 百度网盘助手的那一套就好了 于是试了一下,那当然是可以的 ( 首先,你去 aria2c 的 github-release** 页面,那里就有 aria2c for Android 的二进制包,都不用自己编译了多好。
2343 0
下一篇
无影云桌面