php using shell commands

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

sheepz
Forum Commoner
Posts: 58
Joined: Wed Jul 06, 2005 11:35 pm

Post by sheepz »

i can't seem to exec simple windows shell commands. i'm using XPsp2, IIS6, PHP5. can't seem to get any of these functions working: `backtick` system() exec() shell_exec()

Code: Select all

<?php

echo shell_exec(`%computername%`);
echo exec(`%computername%`);
echo system(`C:\WINDOWS\system32\cmd.exe %computername%`); 

$result = `ping.exe 127.0.0.1`;
echo "$result";

?>
i dont get any errors but i dont get any results either. all i get is a blank screen? How do i give PHP access to shell commands?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You're executing two commands per line.

a backtick string is an alias to running system()

Check your error logs.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Some further examples:

Code: Select all

[feyd@home]>php -r "echo `echo %computername%`;"
FEYD

[feyd@home]>php -r "echo `echo %date%`;"
Mon 04/17/2006

[feyd@home]>php -r "echo `echo %time%`;"
12:30:30.45
sheepz
Forum Commoner
Posts: 58
Joined: Wed Jul 06, 2005 11:35 pm

Post by sheepz »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


i can't seem to execute cmd commands.  i must be doing it wrong. using backticks, shell_exec(), exec(), system() and no luck

Code: Select all

<?php

echo shell_exec(`%computername%`);
echo exec(`%computername%`);
echo system(`C:\WINDOWS\system32\cmd.exe %computername%`);

$result = `ping.exe 127.0.0.1`;
echo "$result";

?>
i dont any errors but i dont get any results either. i just get a blank screen. =T how do i check if my php is actually processing the shell commands?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you haven't changed a thing in that script since you last post. Carefully examine the examples I posted. Compare these examples against your code.
sheepz
Forum Commoner
Posts: 58
Joined: Wed Jul 06, 2005 11:35 pm

Post by sheepz »

feyd, i'm so sorry, there must have been a mistake becuase both posts are almost identical. i was having posting problems. when i posted the first time i didn't see my post and must have reposted again. i'm definitely going compare codes, thanks for the help. =)
sheepz
Forum Commoner
Posts: 58
Joined: Wed Jul 06, 2005 11:35 pm

Post by sheepz »

i have this for testing

Code: Select all

<?php

echo `echo %computername%`;

?>
and i'm getting this error =T

Warning: shell_exec() [function.shell-exec]: Unable to execute 'echo %computername%' in c:\Inetpub\wwwroot\shell.php on line 10
sheepz
Forum Commoner
Posts: 58
Joined: Wed Jul 06, 2005 11:35 pm

Post by sheepz »

i tried putting this into my cmd console

php -r "echo `echo %date%`;"

but i get a 'php' is not a recognized command. i know this is a stupid question but would this be preventing my php from executing shell commands?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

No. That means that the PHP executable is not in your path. Try navigating to the bin folder in your PHP installation directory and attempting, or adding that directory to your path (alternatively, you can drop a bat file in your WINDOWS directory with the contents along the line of C:\path\to\php.exe %*)
Post Reply