busybox在android开发中的应用
2019獨角獸企業重金招聘Python工程師標準>>>
最近在一個項目中用到了busybox,開始上網查找相關文檔,把自己整理的資料在這里分享下。
????1、busybox是什么?
????Busybox:是一個集成了許多常用linux命令和工具的軟件,可以用來做許多事,這里以項目中用例來說明。
????2、安裝busybox:
??????參考文檔:http://www.cnblogs.com/xiaowenji/archive/2011/03/12/1982309.html。
????3、使用busybox查看網絡接口狀態:
????
????
????參數詳解:
????eth0:表示網卡一;
????HWaddr:表示網卡的物理地址; inet addr:表示網卡的ip地址; Bcast:表示廣播地址;Mask:掩碼地址;
????eth1:表示網卡二;
????lo:表示localhoat,即127.0.0.1;
????p2p0:表示網絡接口,關于p2p0詳情見:http://blog.csdn.net/mirkerson/article/details/38276629;
????wlan0:表示無線網卡。
????4、busybox在android項目中的使用:
????在android項目中需要進行網口測試,即創建網橋實現局域網內互聯,測試網口使用狀況,結合實例代碼說明如下:
?String[]?net_set_cmds?=?new?String[]?{?"busybox?ifconfig",?"busybox?ifconfig?eth0?up","busybox?ifconfig?eth1?up",?"busybox?brctl?addbr?br0",?"busybox?brctl?addif?br0?eth0","busybox?brctl?addif?br0?eth1",?"busybox?ifconfig?eth0?0.0.0.0","busybox?ifconfig?eth1?0.0.0.0","busybox?ifconfig?br0?192.168.88.2?netmask?255.255.255.0","busybox?route?add?default?gw?192.168.88.254",?"busybox?ifconfig"?};String[]?net_restore_cmds?=?new?String[]?{?"busybox?ifconfig",?"busybox?brctl?delif?br0?eth0","busybox?brctl?delif?br0?eth1",?"busybox?ifconfig?br0?down",?"busybox?brctl?delbr?br0","busybox?ifconfig?eth0?up",?"busybox?ifconfig?eth1?up",?"busybox?ifconfig"?};private?void?netSetting(final?int?step,?final?String[]?cmd)?{Log.i(TAG,?"[netSetting].......................A");if?(step?<?cmd.length)?{handler.post(new?MyRunnable(cmd[step],?new?MyOnCommandResultListener(step,?cmd)));}Log.i(TAG,?"[netSetting].......................C");}class?MyOnCommandResultListener?implements?OnCommandResultListener?{int?step;String[]?cmd;public?MyOnCommandResultListener(int?step,?String[]?cmd)?{this.step?=?step;this.cmd?=?cmd;}@Overridepublic?void?onResult(String?result)?{netSetting(++step,?cmd);}}class?MyRunnable?implements?Runnable?{String?cmd;OnCommandResultListener?linstener;public?MyRunnable(String?strcmd,?OnCommandResultListener?onCommandResultListener)?{cmd?=?strcmd;linstener?=?onCommandResultListener;}@Overridepublic?void?run()?{String?result?=?CommonUtils.getInstance().executeCommand(cmd);if?(linstener?!=?null)?{linstener.onResult(result);}}}????這里使用java代碼實現了一個網橋的創建,主要看下其中的命令,net_set_cmds這個字符串數組是創建網橋的命令。
????busybox ifconfig //會輸出當前網絡接口的情況
????busybox ifconfig eth0 up //啟動eth0設備
????busybox ifconfig eth1 up //啟動eth1設備
????busybox brctl addbr br0? //建立一個邏輯網段 delbr 刪除網段??
????busybox brctl addif br0 eth0 //讓eth0成為br0的一個端口
????busybox brctl addif br0 eth1 //讓eth1成為br0的一個端口
????busybox ifconfig eth0 0.0.0.0 //網橋的每個物理網卡作為一個端口,運行于混雜模式,而且是在鏈路層工作,所以就不需要IP了。
????busybox ifconfig eth1 0.0.0.0 //
????busybox ifconfig br0 192.168.88.2 netmask 255.255.255.0 //給br0配置ip和子網掩碼
????busybox route add default gw 192.168.88.254 //添加默認網關
????調用netSetting(0, net_set_cmds)就可以實現網橋的創建,之后去ping相關ip查看是否ping通,就可檢測網口狀況。
????當然也可以刪除網橋,恢復網絡狀態,其中net_restore_cmds這個字符串數組就是刪除網橋的命令。
????busybox brctl delif br0 eth0 //從br0中刪除eth0端口
????busybox brctl delif br0 eth1 //從br0中刪除eth1端口
????busybox ifconfig br0 down //關閉邏輯網段br0
????busybox brctl delbr br0 //刪除邏輯網段br0
????busybox ifconfig eth0 up //啟動eth0設備
????busybox ifconfig eth1 up //啟動eth1設備
????值得注意的是:ifconfig 可以用來配置網絡接口的IP地址、掩碼、網關、物理地址等;用ifconfig 為網卡指定IP地址,這只是用來調試網絡用的,并不會更改系統關于網卡的配置文件。如果您想把網絡接口的IP地址固定下來,目前有三個方法:一是通過各個 發行和版本專用的工具來修改IP地址;二是直接修改網絡接口的配置文件;三是修改特定的文件,加入ifconfig 指令來指定網卡的IP地址,比如在redhat或Fedora中,把ifconfig 的語名寫入/etc/rc.d/rc.local文件中;
轉載于:https://my.oschina.net/u/1999544/blog/314752
總結
以上是生活随笔為你收集整理的busybox在android开发中的应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 乱码问题解决方法
- 下一篇: 创建一个提供数据 API 的 Node.