Find the answer to your Linux question:
Results 1 to 7 of 7
what is the function in php that used to execute files in linux. i want to execute script file that contains iptables rules...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2008
    Posts
    38

    php function


    what is the function in php that used to execute files in linux. i want to execute script file that contains iptables rules

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,746

  3. #3
    Just Joined!
    Join Date
    Apr 2008
    Posts
    38

    php function 2

    how i can use exec function in php to execute iptables command

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,746
    Wow...just wow...

    Exec Function - PHP Manual

    Copy/paste from the PHP function manual:
    Code:
    <?php
    // outputs the username that owns the running php/httpd process
    // (on a system with the "whoami" executable in the path)
    echo exec('whoami');
    ?>

  5. #5
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251

    use php to run iptables command

    With iptables, things are a bit more complicated than the command whoami due to the fact that only root can execute iptables command. But we can get around this with sudoers. You need to add the web server user (for example, apache) to /etc/sudoers like this

    apache ALL=NOPASSWD:/sbin/iptables

    then in your php script you put your command something like

    $cmd="sudo /sbin/iptables Blah Blah Blah";
    exec($cmd);

    if you don't see /etc/sudoers, you need to get it installed first.

  6. #6
    Just Joined!
    Join Date
    Apr 2008
    Posts
    38

    iptables2

    i add this line to sudoers
    apache ALL=NOPASSWD:/sbin/iptables
    then i write php script like this
    <?php
    $cmd="sudo /sbin/iptables -A INPUT -p tcp -s 172.16.58.10 -j REJECT";
    exec($cmd);
    ?>
    but i didn't get the result.
    thanks for help.

  7. #7
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    shyma,
    You need to find out the what the www user is first. If you are using apache, look under /etc/apache/uid.conf. Replace "apache" in "apache ALL=NOPASSWD:/sbin/iptables" with the User setting in uid.conf and try again. Also if you can post the error message returned by php if you need further help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •