Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7534181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:56:35+00:00 2026-05-30T05:56:35+00:00

I am trying to execute shell command through my code for adding entry in

  • 0

I am trying to execute shell command through my code for adding entry in Iptables. The following is my piece of code

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");
    }
}

Here there are two methods i.e, createshellCommand() for building the shell command and writeIptables() for updating the iptables. But whenever I run the program logcat is displaying the following warning

“W 19913 su request rejected (0->0 /system/bin/sh)”

But I can manually add the entry through command prompt and its adding to the iptables but by using the above code its not updating. My phone is rooted and I am using android 2.3.4 with linux kernel 2.6.29. And I am using external library “Roottools” for checking the root access.

Please help me to rectify the error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:56:36+00:00Added an answer on May 30, 2026 at 5:56 am

    This Works:

    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();
        }
    }
    

    This Code Works fine. There are couple of small mistakes in your program. Try the one i pasted. Its working charm. All the best. I kept it as simple as possible so that it is easy to understand. You can still use arrays and other stuff to fancy your coding.

    And yaa the same one also works for chmod command where you need to pass more than one argument.

    For this Just replace

    “os.writeBytes(“reboot\n”);”

    with

    “chmod 777 /dev/video0\n”(or any other system file).

    Thanks. LEt me know if there is something tat i can do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to execute the following command at shell prompt: nohup sqlplus DB_ID/DB_PWD@DOMAIN
Trying to execute shell command in background using pythons commands module >>>import commands >>>output
I'm trying to execute a shell command from a java application, on the GNU/Linux
I am trying to execute a shell command via: <php echo shell_execute(tac /home/kusmuk/access-logs/kusmuk.org); ?>
I'm trying to execute a command through system() in PHP. The command is /usr/bin/unzip,
I am trying to execute a command in a shell and pipe the output
While trying to execute the following lines only the last two statements are displayed(Here
I am trying to execute this SQL command: SELECT page.page_namespace, pagelinks.pl_namespace, COUNT(*) FROM page,
I'm trying to execute the following line: exit | sqlplus username/password@sid @test.sql Works great
I'm trying to execute a SQL command, but I just can't find out why

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.