?
Solved

Using exec in PHP to run program?

Posted on 2008-10-31
6
Medium Priority
?
1,215 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

762 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