Jump to content


Photo

shell commands in PHP?


  • Please log in to reply
3 replies to this topic

#1 sheepz

sheepz
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 18 April 2006 - 01:56 AM

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

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

i had to leave off the terminator for almost all the lines ; or else this forum wont let me post
<?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 any ideas?

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 18 April 2006 - 01:20 PM

Tested on my WinXP development machine and working
<?php
exec("ping 192.168.1.1",$result)
echo "<pre>";
print_r($result);
echo "</pre>";
?>
Output:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Array
(
[0] =>
[1] => Pinging 192.168.1.1 with 32 bytes of data:
[2] =>
[3] => Reply from 192.168.1.1: bytes=32 time=1ms TTL=255
[4] => Reply from 192.168.1.1: bytes=32 time=1ms TTL=255
[5] => Reply from 192.168.1.1: bytes=32 time=1ms TTL=255
[6] => Reply from 192.168.1.1: bytes=32 time=1ms TTL=255
[7] =>
[8] => Ping statistics for 192.168.1.1:
[9] => Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
[10] => Approximate round trip times in milli-seconds:
[11] => Minimum = 1ms, Maximum = 1ms, Average = 1ms
)[/quote]

EDIT: I also had to exclude the semi-colon from my exec() line, server would not allow it - forbidden message.

#3 sheepz

sheepz
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 24 April 2006 - 04:46 PM

[!--quoteo(post=365987:date=Apr 18 2006, 06:20 AM:name=SemiApocalyptic)--][div class=\'quotetop\']QUOTE(SemiApocalyptic @ Apr 18 2006, 06:20 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Tested on my WinXP development machine and working
<?php
exec("ping 127.0.0.1",$result)
echo "<pre>";
print_r($result);
echo "</pre>";
?>
Output:

EDIT: I also had to exclude the semi-colon from my exec() line, server would not allow it - forbidden message.
[/quote]


sorry for the late response been really hectic at work and totally forgot =( thanks for the help =)
i copied the code exactly and added the ; to the end of the exec() line. i'm still getting a

Warning: exec() [function.exec]: Unable to fork [ping 127.0.0.1] in c:\Inetpub\wwwroot\shell.php on line 11

Array
(
)

do i need to setup my php.ini or have my computer allow php to access shell commands?

#4 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 24 April 2006 - 07:54 PM

You shouldn't have to change any settings for this to work - Mine is set-up pretty much default. Maybe you are running an older version of PHP4? Searching for "Unable to fork" on Google turns up quite a few resources on the error - Maybe you could solve it better then?

Or maybe try out the system() command instead of exec() - You should just be able to swap the word exec for system, they both except the same paramiters.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users