Shell shell_script function is not working how to be fixed it?

I have installed apache24 and php in freebsd 12.2. I have created a html form which takes two parameter such as IP Address & Hostname and then in sending these data to php server code. The server code is taking ip address and hostname and put in a text file. File is created but I want to run a shell script with a button like shell_exec() but is working.

Code:
<!DOCTYPE HTML>
<head> DNS </head>
<body>
    Welcome To DNS Configuration
<form action="dns.php" method="POST">

  Enter IP Address:<br>

  <input type="text" name="ip">

  <br>

  Hostname:<br>

  <input type="text" name="hname">

  <br><br>

  <input type="submit" value="submit">

</form>
</body>
</html>

<?php
if(isset($_POST['ip']) && isset($_POST['hname']))
{
$data="{$_POST['ip']}\n{$_POST['hname']}\n";
$fp=fopen('dns.txt','w');
fwrite($fp,$data);
fclose($fp);
shell_exec('sh test.sh');
}
?>

how can execute shell_exec in php.
 

Attachments

  • 1.PNG
    1.PNG
    2.8 KB · Views: 139
  • 3..PNG
    3..PNG
    12.7 KB · Views: 134
  • 4.PNG
    4.PNG
    3.6 KB · Views: 133
  • 5.PNG
    5.PNG
    5 KB · Views: 129
You mess up all permissions under /usr/local which is not good at all. You need write permission only under working folder. For example
/usr/local/www/apache24/data/workfolder giving write permission to the entire data folder is a security risk.
in your script the shebang points to bash which is not installed by default if you execute the script locally it will fail. You can change it to #!/bin/sh or install bash.

mkdir /usr/local/www/apache24/data/workdir
chown www /usr/local/www/apache24/data/workdir

PHP:
<?php
    if(isset($_POST['ip']) && isset($_POST['hname']))
    {
        $data = "{$_POST['ip']}\n {$_POST['hname']}\n";
        $handle = fopen('workdir/dns.txt', 'w');
        if ( !$handle ) {
            $who = shell_exec ('whoami');
            echo "Error! Could't open the file dns.txt for writing under user $who";
        } else {
            fwrite($handle, $date);
            fclose($handle);
            shell_exec('sh test.sh');
            echo "Done";
        }
    }
?>

Code:
#!/bin/sh
touch workdir/dir

p.s.
I assume that your php is working you can test it by:
echo "<?php phpinfo(); ?>" >> /usr/local/www/apache24/data/test.php
and then navigate to http://yourserver/test.php
 
12.2 won't exist until it's branched off from 12-STABLE. That hasn't happened yet.
 
Back
Top