[問題] 在app中執行ifconfig

看板AndroidDev作者 (翊玥)時間12年前 (2013/12/04 17:00), 編輯推噓0(009)
留言9則, 3人參與, 最新討論串1/1
android和linux新手,請教各位前輩,希望問題描述得正確: 我在板子上灌Android系統,用rndis module讓板子與手機能以USB相連。 但是每次重插USB之後ip都會消失,要再重新設定一次ip, 也就是要在minicom中手動打:ifconfig usb0 x.x.x.x來對板子設定ip。 所以我在app當中寫了一段程式碼來自動設ip,當我開這個app時就會先幫我把ip設好, 然後執行其他的功能。 找了一些資料後寫出下面的程式碼,可以達到我的需要: public void ifconfigUsb0IP(String strIP){ Process process = null; DataOutputStream os = null; try { process = Runtime.getRuntime().exec("su"); os = new DataOutputStream(process.getOutputStream()); os.writeBytes("ifconfig usb0 "+strIP+"\n"); os.flush(); } catch (Exception e) { e.getMessage(); } } 可是我想讓這個app不要使用到root權限。 所以我去版子的/system/bin裡面把ifconfig檔權限設為system system rwxrwxrwx 然後改寫程式碼為 public void ifconfigUsb0IP(String strIP){ Process process = null; DataOutputStream os = null; try { process = Runtime.getRuntime().exec("ifconfig usb0 "+strIP); } catch (Exception e) { e.getMessage(); } } 可是不work。 我試過幾種方法改寫都不成功,不曉得是一開始改ifconfig權限的想法就錯了, 還是單純是在Runtime.getRuntime().exec("ifconfig usb0 "+strIP);語法上的錯誤呢? 謝謝回覆:) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.61.240.28

12/04 19:43, , 1F
你要不要單純寫一個native程式或bash script再用termin
12/04 19:43, 1F

12/04 19:43, , 2F
al app之類的執行它
12/04 19:43, 2F

12/04 19:45, , 3F
感覺一個ifconfig指令就要用java寫有點厚工
12/04 19:45, 3F

12/05 09:14, , 4F
噢噢,不是單純為了ifconfig寫app的,是app要工作前有這
12/05 09:14, 4F

12/05 09:14, , 5F
個需要,所以我順便把它加進去而已:P
12/05 09:14, 5F

12/05 15:13, , 6F
我在process = Runtime.getRuntime().exec("ifconfig usb0
12/05 15:13, 6F

12/05 15:14, , 7F
這行之前先執行"su"讓它得到root權限,仍沒辦法讓這行指令
12/05 15:14, 7F

12/05 15:15, , 8F
發生作用。所以直接在exec()中寫ifconfig ...是錯誤寫法?
12/05 15:15, 8F

12/10 02:46, , 9F
你的app有拿到system身分嗎
12/10 02:46, 9F
文章代碼(AID): #1IdkyTvi (AndroidDev)