unknown1 Posted December 29, 2009 Share Posted December 29, 2009 Hello, Just wanted to see if someone could tell me how to execute shell commands via php I tired with exec() and shell_exec() but it doesn't execute anything? Can someone who is familiar with this explain how this works and why it won't work when I tried. Here is an example of what I'm trying to execute. e.g. echo shell_exec('Xvfb :2 -screen 0 1024x768x24&'); echo shell_exec('export DISPLAY=localhost:2.0'); echo shell_exec('khtml2png2 --sw 100 --sh 100 http://www.example.com example.png'); Thanks in advance!! Quote Link to comment https://forums.phpfreaks.com/topic/186545-shell-commands-with-php/ Share on other sites More sharing options...
oni-kun Posted December 29, 2009 Share Posted December 29, 2009 You probably didn't define a path. Try something such as $exe = exec('/bin/cal 1995', $result); echo $result; Also you may want to look at escapeshellcmd Quote Link to comment https://forums.phpfreaks.com/topic/186545-shell-commands-with-php/#findComment-985185 Share on other sites More sharing options...
trq Posted December 29, 2009 Share Posted December 29, 2009 Firstly, are you sure xvfb & khtml2png2 are installed? There not exactly standard apps. Quote Link to comment https://forums.phpfreaks.com/topic/186545-shell-commands-with-php/#findComment-985284 Share on other sites More sharing options...
unknown1 Posted December 29, 2009 Author Share Posted December 29, 2009 Yes, I have installed them. Just not sure if I can execute just exec() with login or do I need something like licssh2 and a login script or can I just execute the commands... Quote Link to comment https://forums.phpfreaks.com/topic/186545-shell-commands-with-php/#findComment-985285 Share on other sites More sharing options...
trq Posted December 29, 2009 Share Posted December 29, 2009 You just need to make sure the user Apache runs under has sufficient permissions to execute the commands in question. Quote Link to comment https://forums.phpfreaks.com/topic/186545-shell-commands-with-php/#findComment-985338 Share on other sites More sharing options...
unknown1 Posted December 31, 2009 Author Share Posted December 31, 2009 This is my current script, does anyone have any ideas of why this won't work?? Take note it runs fine from command line and produces the results needed but doesn't work from script. Thanks in advance!! <?php $script1 = "Xvfb :2 -screen 0 1024x768x24&"; $script2 = "export DISPLAY=localhost:2.0"; $script3 = "khtml2png2 --sw 100 --sh 100 http://www.ask.com ask.png"; $connection = ssh2_connect('server.xxxxxxxxxxx.com', 22); ssh2_auth_password($connection, 'root', 'xxxxxxxxxxxx'); $stream = ssh2_exec($connection, $script1); sleep(10); $stream = ssh2_exec($connection, $script2); $stream = ssh2_exec($connection, $script3); sleep(5); fclose($stream); echo "All done!!"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/186545-shell-commands-with-php/#findComment-986245 Share on other sites More sharing options...
optikalefx Posted December 31, 2009 Share Posted December 31, 2009 run the commands in terminal first. if they work in terminal then they will work with shell_exec() Quote Link to comment https://forums.phpfreaks.com/topic/186545-shell-commands-with-php/#findComment-986248 Share on other sites More sharing options...
unknown1 Posted December 31, 2009 Author Share Posted December 31, 2009 run the commands in terminal first. if they work in terminal then they will work with shell_exec() Okay but the script produces a screen shot and works fine from terminal and doesn't produce the screen shot when I run it from shell_exec(). Any ideas of why this maybe?? Quote Link to comment https://forums.phpfreaks.com/topic/186545-shell-commands-with-php/#findComment-986249 Share on other sites More sharing options...
unknown1 Posted December 31, 2009 Author Share Posted December 31, 2009 Anyone at all have an idea Quote Link to comment https://forums.phpfreaks.com/topic/186545-shell-commands-with-php/#findComment-986346 Share on other sites More sharing options...
Mchl Posted December 31, 2009 Share Posted December 31, 2009 [removed] Didn't notice you were connecting to remote server. Quote Link to comment https://forums.phpfreaks.com/topic/186545-shell-commands-with-php/#findComment-986360 Share on other sites More sharing options...
optikalefx Posted December 31, 2009 Share Posted December 31, 2009 ok try running a basic shell command like shell_exec('pwd'); and see if you get output. If you do, then it will mostly likely be a PHP permission problem. Quote Link to comment https://forums.phpfreaks.com/topic/186545-shell-commands-with-php/#findComment-986558 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.