Android(安卓)7.1使用以太网口共享本机4G网络给其他设备
16lz
2022-06-16
public String doEthernetShare() { String result = "Failure"; DataOutputStream dataOutputStream = null; BufferedReader errorStream = null; String networkType = getNetworkType(); try { MyLog("doEthernetShare.networkType=" + networkType); Process process = Runtime.getRuntime().exec("su"); dataOutputStream = new DataOutputStream(process.getOutputStream()); String commandStr = "mount -o rw,remount / " + " \n" +"mount -o rw,remount /system " + " \n" +"mkdir -p /var/lib/misc/" + " \n" +"touch /var/lib/misc/udhcpd.leases" + " \n" +"echo 0 > /proc/sys/net/ipv4/ip_forward" + " \n" +"busybox ifconfig eth0 up" + " \n" +"busybox ifconfig eth0 192.168.1.1 netmask 255.255.255.0" + " \n" +"/system/bin/iptables -t nat -A natctrl_nat_POSTROUTING -o " + networkType + " -j MASQUERADE" + " \n" +"/system/bin/iptables -A natctrl_FORWARD -i " + networkType + " -o eth0 -m state --state ESTABLISHED,RELATED -g natctrl_tether_counters" + " \n" +"/system/bin/iptables -A natctrl_FORWARD -i eth0 -o " + networkType + " -m state --state INVALID -j DROP" + " \n" +"/system/bin/iptables -A natctrl_FORWARD -i eth0 -o " + networkType + " -g natctrl_tether_counters" + " \n" +"/system/bin/iptables -A natctrl_tether_counters -i eth0 -o " + networkType + " -j RETURN" + " \n" +"/system/bin/iptables -A natctrl_tether_counters -i " + networkType + " -o eth0 -j RETURN" + " \n" +"/system/bin/iptables -D natctrl_FORWARD -j DROP" + " \n" +"/system/bin/iptables -A natctrl_FORWARD -j DROP" + " \n" +"ip route add 192.168.1.0/24 dev eth0 table local_network proto static scope link" + " \n" +"echo 1 > /proc/sys/net/ipv4/ip_forward" + " \n" +"busybox1.11 udhcpd -fS /etc/udhcpd.conf" + " \n" ; dataOutputStream.write(commandStr.getBytes(Charset.forName("utf-8"))); dataOutputStream.flush(); dataOutputStream.writeBytes("exit\n"); dataOutputStream.flush(); process.waitFor(); errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream())); String msg = ""; String line; // 读取命令的执行结果 while ((line = errorStream.readLine()) != null) { msg += line; } MyLog("msg" +msg); result = msg; } catch (Exception e) { Log.e(TAG_AZ, e.getMessage(), e); } finally { try { if (dataOutputStream != null) { dataOutputStream.close(); } if (errorStream != null) { errorStream.close(); } } catch (IOException e) { Log.e(TAG_AZ, e.getMessage(), e); } } return result; }
更多相关文章
- android 如何从sqlite读取数据到spinner下拉中显示
- 看完这篇,再也不怕被问到AsyncTask的原理
- Android怎样添加AT命令
- 转载 Android(安卓)通过adb shell命令查看内存,CPU,启动时间,电量等
- android后台执行定时任务(保活)&&使用AlarmManager的各种坑
- 解析ANDROID ps命令执行后各项参数的含义
- 解决Cygwin中的“died waiting for dll loading”错误
- Android动画效果 translate、scale、alpha、rotate
- 申请Android(安卓)google map的apiKey