Solved

Using exec in PHP to run program?

Posted on 2008-10-31
6
1,209 Views
Last Modified: 2013-12-13
Hello.

I'm trying to make use of the exec feature in PHP. I'm trying to run the following command:
C:\psshutdown.exe @c:\shutdown.txt -u administrator -p password -t 60 -f -k -c -m "This PC Will Blow Up In 60 Seconds!  Run You Fool!?"

The above command works prefectly in the command line, so i put together the code in the code snippet, although when i run it, it just 'thinks' for about 10 seconds then finishes, but the command isn't working. There is nothing output from '$output' and if i print '$return_var' it shows '-1073741502'.

i tried running a process manager to see what it was running and got these results:

http://img148.imageshack.us/my.php?image=ssinternalsuh7.jpg

It appears to show that it's run it, but it hasn't. I've noticed that it closed cmd very quickly after the command was run, perhaps not being it enough time to run?

Anyone have any suggestions why it's not working?

Thanks.

<?php
 
$output = array();
$return_var = 0;
 
 $command='C:\psshutdown.exe @c:\shutdown.txt -u administrator -p password -t 60 -f -k -c -m "This PC Will Blow Up In 60 Seconds!  Run You Fool!?"';
exec($command, $output, $return_var);
 
sleep(10);
 exec("exit(0)"); 
 
foreach($output as $outputline){
    echo("$outputline<br>");
}
 
?>

Open in new window

0
Comment
Question by:alumwell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 16

Expert Comment

by:hankknight
ID: 22848595
To see the acual command-line error, you should add 2>&1 to the end of your command.

Also, you are sleeping for only 10 seconds, and I think you will need to sleep fore more than 60.


<?php
 
ignore_user_abort ();
set_time_limit (0);
 
$output = array();
$return_var = 0;
 
$command='C:\psshutdown.exe @c:\shutdown.txt -u administrator -p password -t 60 -f -k -c -m "This PC Will Blow Up In 60 Seconds!  Run You Fool!?"';
echo shell_exec($command . ' 2>&1');
 
sleep(70);
echo shell_exec("exit(0) 2>&1"); 
 
?>

Open in new window

0
 

Author Comment

by:alumwell
ID: 22848731
Thanks for your reply.

Just tried the code you gave me, unfortunatly it waits the time but the command isn't being run and it doesn't display anything once it's finished.

Any other ideas?

thanks again.
0
 
LVL 16

Expert Comment

by:hankknight
ID: 22849612
Does this give you an error you can read?
<?php
 
ignore_user_abort ();
set_time_limit (0);
 
$command='C:\psshutdown.exe @c:\shutdown.txt -u administrator -p password -t 60 -f -k -c -m "This PC Will Blow Up In 60 Seconds!  Run You Fool!?"';
echo shell_exec($command . ' 2>&1');
 
?>

Open in new window

0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:alumwell
ID: 22849834
Nope. Nothing. Any ideas?

Thanks.
0
 
LVL 16

Expert Comment

by:hankknight
ID: 22850408
I have only one more idea.

Create a batch file called "psshut.bat" and put it here:
C:\psshut.bat

The batch file should contain one line only:
C:\psshutdown.exe @c:\shutdown.txt -u administrator -p password -t 60 -f -k -c -m "This PC Will Blow Up In 60 Seconds!  Run You Fool!?"

Then exicute the batch file from PHP:

<?php
$command='C:\psshut.bat';
echo shell_exec($command . ' 2>&1');
?>

If that does not work, I cannot help.

What you are trying to do could be considered dangerous from a security standpoint.  It is possible that a firewall or some other security software is preventing you from doing this.  Beyond that, I am sorry, without a specific error message I am shooting in the dark.
0
 

Accepted Solution

by:
alumwell earned 0 total points
ID: 22850949
Thats now outputting:

C:\www\htdocs>C:\psshutdown.exe @C:\shutdown.txt -u administrator -p password -t 60 -f -k -c -m "This PC Will Blow Up In 60 Seconds! Run You Fool!?"
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question