2011-07-31 14 views
0

iptables에서 항목을 추가하기 위해 코드를 통해 shell 명령을 실행하려고합니다. 다음은 여기에 iptables에 업데이트를위한 쉘 명령과 writeIptables()를 구축하기위한 두 가지 방법 즉, createshellCommand()가 android에서 셸 명령을 실행하는 방법은 무엇입니까?

private void createShellCommand(String uidValue_param, String interface_param) { 
    // TODO Auto-generated method stub 
    StringBuilder cmdScript=new StringBuilder(); 
    script.append("iptables -A OUTPUT " + interface_param + "-m owner --uid-owner " + uidValue_param + "-j REJECT"); 
    writeIptables(cmdScript); 

} 


private void writeIptables(StringBuilder scriptfile) { 
    // TODO Auto-generated method stub 
    String cmd=scriptfile.toString(); 
    if(RootTools.isRootAvailable()) 
    { 
     Process exec; 
     try { 
      exec = Runtime.getRuntime().exec(new String[]{"su","-c"}); 
      final OutputStreamWriter out = new OutputStreamWriter(exec.getOutputStream()); 
      out.write(cmd); 

      // Terminating the "su" session 
      out.write("exit\n"); 
      out.flush();  
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      Log.e("IPtables updation failed", "Iptable write failed"+e); 
     } 

    } 
    else 
    { 
     Log.e("Root Access denied", "Access Denied"); 
    } 
} 

코드

의 내 작품이다. 하지만 프로그램 로그 캣은 "거부 19,913 스와 요청 W (0-> 0/시스템/빈/SH)"다음과 같은 경고

를 표시하고 실행하지만 수동으로 명령 프롬프트를 통해 항목을 추가 할 수 있습니다 언제 그 iptables에 추가하지만 위의 코드를 사용하여 업데이 트되지 않습니다. 내 전화는 루틴이고 리눅스 커널 2.6.29와 함께 안드로이드 2.3.4를 사용하고 있습니다. 그리고 루트 접근을 검사하기 위해 외부 라이브러리 "Roottools"를 사용하고 있습니다.

오류를 수정하는 데 도움을주십시오.

답변

4

이 작품 :

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    execute_reboot(); 

} 
void execute_reboot() 
{ 
    Process reboot; 
    try { 
     reboot=Runtime.getRuntime().exec("su"); 
      DataOutputStream os = 
       new DataOutputStream(reboot.getOutputStream()); 
      os.writeBytes("reboot\n"); 
      os.flush(); 

       os.writeBytes("exit\n"); 
       os.flush(); 

      reboot.waitFor(); 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

이 코드는 잘 작동합니다. 프로그램에 몇 가지 작은 실수가 있습니다. 붙여 넣은 것을 사용해보십시오. 그것의 일하는 매력. 모두 제일 좋다. 나는 그것을 이해하기 쉽도록 가능한 한 간단하게 유지했다. 배열과 다른 것들을 사용하여 코딩을 멋지게 할 수 있습니다.

그리고 두 개 이상의 인수를 전달해야하는 chmod 명령에도 동일하게 적용됩니다.

이 들어

그냥

"os.writeBytes ("재부팅 \ n을 ");"교체

"chmod를 777는/dev/video0 \ 없음"(또는 다른 시스템 파일)와

.

감사합니다. 내가 할 수있는 일이 있으면 알려주세요. "proc.waitFor를();"

+0

제안 해 주셔서 감사합니다 ... – Unnikrishnan

0

iptables 설정 파일을 혼동하지 않고 iptables 명령 (sudo와 함께 사용)을 사용하려고합니다.

1
public static void rebootNow() { 
    Log.d(TAG, "Rebooting"); 
    try { 
     @SuppressWarnings("unused") 
     Process proc = Runtime.getRuntime().exec(
       new String[] { "su", "-c", "reboot" }); 
    } catch (Exception ex) { 
     Log.d(TAG, "Rebooting failed (Terminal Error)"); 
    } 
} 

이것은 당신은 추가 할 수 있습니다

좀 더 컴팩트 프로세스 proc ... 후 사용하지 않는 경고를 없애기 위해 장치를 재부팅하는 데 몇 초가 걸리고 UI 스레딩에서 몇 초 동안 일부 기능을 사용하지 않으려면 기다리지 않는 것이 좋습니다. 프로세스가 종료됩니다.

관련 문제