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