Page 1 of 2

php using shell commands

Posted: Thu Apr 13, 2006 1:04 am
by sheepz
i'm using a script that was an example in php.net somewhere on how to use "shell_exec()" basically i want to exec shell commands and capture the value and display it on the site

Code: Select all

<?
  $result = shell_exec("date");
  echo $result;
?>
getting this error:

Warning: shell_exec() [function.shell-exec]: Unable to execute 'date' in c:\Inetpub\www\shell.php on line 2

not sure what's going on =/

Posted: Thu Apr 13, 2006 1:59 am
by jmut
As you are using windows maybe try using exec() instead.

Posted: Thu Apr 13, 2006 6:16 am
by timvw
The problem is that "date" seems to be a built-in function of cmd.exe

I believe there is a "date.exe" athttp://sourceforge.net/projects/unxutils which you can use.

Posted: Thu Apr 13, 2006 11:27 am
by sheepz
I'm sorry for the lack of information, yes I am using WinXP SP2 with IIS6. i've tried using the exec() i'm not sure if i used it correctly

<?
$result = exec('date');
echo $result;
?>

still generating errors


Should the "date" work if it's a built in function of cmd.exe? I thought that's how it should work, if it's a built in function, therefore it would execute. thanks for the help =)

Posted: Thu Apr 13, 2006 12:22 pm
by feyd
If you want to run stuff through the command line interpreter, cmd.exe to run it.

http://www.ss64.com/nt/cmd.html

Posted: Thu Apr 13, 2006 12:45 pm
by sheepz
i'm sorry, i'm really new, don't understand exactly. should the code be something like this?

<?
$result = shell_exec("cmd.exe date");
echo $result;
?>

i'm pretty sure thats wrong =(

Posted: Thu Apr 13, 2006 4:58 pm
by Ambush Commander
Ermm... why aren't we just using date()?

The stdin might also be a problem. On XP, after invoking date, the command line asks for a new date. Could that be the problem?

Posted: Thu Apr 13, 2006 5:08 pm
by feyd
although I also wonder why sheepz isn't using date() as well, there is

Code: Select all

cmd /c"echo %date%"

Posted: Thu Apr 13, 2006 5:33 pm
by sheepz
oh i didn't know there was a function called date() i'm gonna look that up in php.net. also i want to learn how to run cmd commands thru php. i saw a vague description wich used that little

$result = shell_exec("date");

didn't explain anything much just said it would use the command interperter and display the date. was searching the foras.

Posted: Thu Apr 13, 2006 5:34 pm
by Ambush Commander
Mm... unlike Perl, which encourages the use of shell calls to get things done, PHP has internal functions that should be used to do about anything.

Posted: Thu Apr 13, 2006 6:06 pm
by sheepz
i'm sorry i must be asking the question wrong but i would like to see an example of using php to capture information from a shell command and display it thru php. i got this from php.net

<?php
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
?>

the "ls -lart" command is clearly for linux so i tried to change it to fit windows

<?
$result = shell_exec("%date%");
echo "$result";
?>

i would like to run shell commands, use the variable to hold the string and then display it but can't seem to get the syntax correct.

Posted: Thu Apr 13, 2006 6:10 pm
by Ambush Commander
Actually, you're barking up the wrong tree. ;-)

Check this out:

Code: Select all

C:\Documents and Settings\Edward>%date%
'Thu' is not recognized as an internal or external command,
operable program or batch file.
The attempt of accessing the variable works, it's just that the shell is now trying to execute it. Use echo and...

Code: Select all

C:\Documents and Settings\Edward>echo %date%
Thu 04/13/2006
Voila! The corresponding PHP code is:

Code: Select all

<?php
$output = `echo %date%`;
?>

Posted: Thu Apr 13, 2006 6:17 pm
by sheepz
<?php
$output = `echo %date%`;
echo $output;
?>

I did a cut and paste of and just added the echo $output; but the page displays nothing. sorry i probably misunderstanding, this is called the "backtick" fucntion yes?

Posted: Thu Apr 13, 2006 6:19 pm
by Ambush Commander
Works for me. :? Yes, this is backticks. Are you sure PHP is installed correctly and has access to shell?

Posted: Thu Apr 13, 2006 6:21 pm
by sheepz
i'm not sure if my php has access to shell. everything should be on default. how would i check? do i change the php.ini file?