0

I am trying to execute a batch file using PHP on Windows Server 2008.

The file is called NEWDNS.bat and its purpose is to write a new entry into a DNS zone file stored in c:\windows\system32\dns. The zone file is named motlocal.co.uk.

Here are the contents of newdns.bat


@echo off
cd windows\system32\dns\
dnscmd dsvr012345 /recordadd motlocal.co.uk mynewsubdomain A 88.208.200.221
dnscmd dsvr012345 /ZoneReload motlocal.co.uk

newdns.bat is stored in the same directory on the server as the PHP page that calls it:-

Here's the contents of the page that calls newdns.bat (the php page is called dnscreator.php)


<?php
if(isset($_POST['submit']))
{
    echo exec("cmd.exe /c newdns.bat");
    echo "Done!";
} else {
?>
    <form action="" method="post">
    <input type="submit" name="submit" value="DO IT!">
    </form>
<?php
}
?>

When I double click on the batch file from a windows folder - it runs and it executes its commands perfectly and creates the new subdomain in the dns zone file.

However - no matter how I try (and I have been on this for 2 days now) it it will NOT execut when I run dnscreator.php.

The form executes and displays the message "Done!" but the commands in the newdns.bat have not been carried out. I know the batch file works because as I said - I can run it from a windows folder (Double Click) and it does what its supposed to do.

PHP Safe mode is OFF, just in case you were wondering and I have tried moving the batch file into c:\windows\system32 and this didn't work either.

Any help / advice / pointers you can give is greatly appreciated - I am at my wits end! Many Thanks.

jww
  • 97,681
  • 90
  • 411
  • 885
MediaHaus
  • 1
  • 3
  • Related, [How do you run a .bat file from PHP?](http://stackoverflow.com/q/835941) – jww Nov 15 '16 at 03:59

2 Answers2

0

If your PHP Page is located in the same directory as the batch file, then the following wil do it:

exec("batchname.bat");

Reference;

Personal experience

Daryl Gill
  • 5,464
  • 9
  • 36
  • 69
  • Hi, Thanks for your quick response. - I tried that already and I just did it again to make sure - still not working. – MediaHaus Dec 05 '13 at 14:16
  • How do I check A) Who the PHP user is and B) What rights they have - thanks – MediaHaus Dec 05 '13 at 18:49
  • Hi Daryl - I tried it exactly as you said. Didn't work. – MediaHaus Dec 05 '13 at 18:50
  • @MediaHaus Sorry for the delay, try running: echo exec("whoami"); to see the user, then check the security tab on the file to see if apache has execute permissions – Daryl Gill Dec 05 '13 at 20:07
  • Hi Daryl - this is a WINDOWS server running IIS7 / PHP 5.4 / MySQL - no Apache installed. Don't really want to get into installing apache - ran your "whoami" command and it returned nothing. Presume thats down to lack of Apache. I am sure this can be done without Apache. Whats your thoughts? THanks – MediaHaus Dec 05 '13 at 20:32
  • @MediaHaus http://stackoverflow.com/questions/5729264/what-are-all-the-user-accounts-for-iis-asp-net-and-how-do-they-differ – Daryl Gill Dec 05 '13 at 20:41
  • Hi Daryl - thanks but didn't really help me - from everything I see the IUSR has read/execute permission on the folder where the batch file is - which is the same folder as the php calling it. – MediaHaus Dec 09 '13 at 02:00
0

1 - cd windows\system32\dns\ CD from where? it is a relative path from the current directory of the process calling the batch file

2 - You can run the batch file, but, can the account (under which the web server is running) do it?

3 - How do you know the batch file is running or not? Try to leave a log from inside the batch file ( echo inside batch > "%temp%\batchphp.log")

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Hi - I KNOW The batch file isnt running - if it was - it would execute the tasks specified in the batch file and create a new host entry in the DNS file. If I run this batch file from windows it runs fine and does what its supposed to do - but when I call it from the php file nothing happens inside the DNS file. I will try your suggestion and come back to you Thanks – MediaHaus Dec 05 '13 at 16:38
  • I also changed the path in the batch file to: cd c:\windows\system32\dns\ - still not working – MediaHaus Dec 05 '13 at 16:50
  • @MediaHaus: from the information given (what you have told), you KNOW that something is not working. DON'T KNOW if the batch file is not running or if the batch file is running but not doing what it should. PHP is running under some user account (maybe under the same that the webserver, other if running as fastcgi), and this account can have no rights to do what you want. Batch file will start but not do anything because it has no rights. – MC ND Dec 05 '13 at 17:42
  • I am logged onto the machine in question as the Administrator and running dnscreator.php locally in a browser. – MediaHaus Dec 05 '13 at 18:17
  • @MediaHaus: Silly question, but, how do you run your php in the browser? – MC ND Dec 05 '13 at 18:20
  • I don't - I meant to say I call it from another machine. Sorry :) - obviously you cant run it on a local machine as it needs to go via the webserver. – MediaHaus Dec 05 '13 at 18:53
  • @MediaHaus: Ok. You have a http request to the web server where the php code is executed. Under what credentials? What is the user the process of the webserver is running under? If php is configured as fastcgi, what user is php configured to run as ? – MC ND Dec 05 '13 at 19:21
  • Forgive me being a newbie to this - how do I find the credentials its running under? I believe it is fastcgi as the mapper handlings in IIS for the domain show 2 entries as 1 = PHP_via_FastCGI (enabled) and 2 = PHP53_via_FastCGI (enabled) - does that help? – MediaHaus Dec 05 '13 at 20:34
  • `` is a good start. But you should know the account that iis is using, if if is authenticating the user of using the IUSR_.. account (see [here](http://stackoverflow.com/a/3903024/2861476)), and in php.ini, the value of fastcgi.impersonate should be `1`, so php runs the scripts under the credentials of the user. – MC ND Dec 06 '13 at 07:54
  • yes the php.ini is set correctly. - Whats more I am able to to do all of the following on the server from my php script. **Create new directories** **Recursive copy files from one folder another** **Create & write to my batch files** **Store my batch files in a different location to php page location** Being able to do all of the above on the server from php through the webserver would lead me to believe that permissions are correct. Whats your thoughts. the batch file is located in the SAME directory as the PHP script trying to execute it. Still no joy in getting it to work. – MediaHaus Dec 09 '13 at 01:54