Hey guys I have one php exec command that works on my remote linux server but not my windows (WAMP package) local server. I really want to create an identical workstation so I can test locally and then update my remote server. If anyone has any suggestions or solutions other than converting to linux, I would really appreciate it.
<?php
$safe_path = escapeshellarg('fake-virus.txt');
$command = '/usr/bin/clamscan --stdout ' . $safe_path;
$out = '';
$int = -1;
exec($command, $out, $int);
echo $int;
if ($int == 0) {
// all good;
} else {
echo 'o no';// VIRUS!;
}
?>
I really want to create an identical workstation so I can test locally and then update my remote server
Installing cygwin is the wrong way to go about this. Just use virtualbox, which you can get from here, and run an Ubuntu VM. Much easier (and nicer).
Use 'su' it works great for me when doing cool cross OS stuff
shell_exec('su Administrator -c "cmd/script"');
Its not that difficult to run cygwin with Apache PHP and more. Here is a pretty simple tutorial cygwin-apache-php Have fun!
At least in one instance I was able to use shell_exec() of php of wamp (on windows:-)) via cygwin. I had to install procps or something in the cygwin installer to have a top command of sorts in cygwin. Then I was able to use wamps php shell_exec to perform that cygwin top command using Kishans suggestion in http://php.net/manual/en/function.shell-exec.php:
<?php
echo $result = shell_exec("C:\cygwin64\bin\bash.exe --login -c '/cygdrive/c/cygwin64/bin/top.exe --b -n 1'");
?>
/////////////////
Kisha's remarks cited:
If you are on windows and has Cygwin, You can execute all your Cygwin binaries using shell_exec() as follows:
<?
$result = shell_exec("C:\cygwin\bin\bash.exe --login -c '/cygdrive/c/cygwin/bin/convert.exe --version'");
print($result);
?>
The key is the "--login" option which appends all the required common cygwin binary and library paths to $PATH. Without this option some of the cygwin binaries that links to other libraries ( Like ImageMagick binaries which links to X11 ) will not work and you may see errors like "cygX11-6.dll not found, Could not start convert.exe"
Related
I want to check if the user has install angular cli with running ng --version on PHP with exec, proc_open or shell_exec but return is empty.
I dont have problems if I execute in cmd or in terminal's Visual Studio Code in any path on Windows.
I'm use wampserver
There are multiple ways to do it, and you have to consider what you will do if there are more than one found. You can find it:
exec('where /r C:\ ng.exe', $paths);
And then if you really need the version, use the first one if it starts with C:\:
if(strpos($paths[0], 'C:\\') === 0) {
exec($paths[0] . ' --version', $version);
}
print_r($version);
If it isn't found then $paths[0] should contain:
INFO: Could not find files for the given pattern(s).
Unfortunately I do not have access to the droplet via console, only by Vesta panel and I need to set up cron job.
However, I can not find the right path to PHP. Now that command line looks that and it does not work:
/usr/local/vesta/bin/php5 -c /home/admin/web/ -q /home/admin/web/mysite.com/public_html/crop_scripts/cron.php
Please tell me what options instead of "/usr/local/vesta/bin/php5" I can try?
what does this code show when you access it through your web server (assuming exec call is allowed)
<?php
$output = array();
exec('which php',$output);
var_dump($output);
I'm trying to set up a centralized server which is in charge of monitoring my other servers. This centralized server needs to be able to collect particular information/metrics about a specific server (such as df -h and service httpd status); but it also needs to be able to restart Apache if needed.
If it wasn't for the Apache restart, I could write a listening script to provide a means of giving the centralized server the data it needs without having to SSH in. But because I also want it to be able to restart Apache, it needs to be able to log in and initiate scripts through a combination of PHP and Bash.
At the moment, I'm using PHP's shell_exec to execute this (very simple) Bash script:
#!/bin/sh
ssh -i /path/to/keyFile.pem ec2-user#x.x.x.x;
I'm accessing the external server (which is an EC2 instance) through a private IP. If I launch this script, I can log in without any problem - the problem comes, however, when I then want to send back the output for commands like the ones I've listed above.
In a Bash script, how would I output a command like df -h after SSHing into another server? Is this possible?
There is a PECL extension for SSH.
Other than that you'll probably want to either use the &$output parameter of exec() to grab the output:
$output = array();
exec('bash myscript.sh', $output);
print_r($output);
Or use output redirection
$output = '/path/to/output.txt';
exec("bash myscript.sh > $output");
if( file_exists($output) && is_readable($output) ) {
$mydata = file_get_contents($output);
}
and, of coure, this all assumes your script looks like what jeroen has in his answer.
You could use:
ssh -i /path/to/keyFile.pem ec2-user#x.x.x.x 'df -h'
or for multiple commands:
ssh -i /path/to/keyFile.pem ec2-user#x.x.x.x 'ls -al ; df -h'
That works from the command line but I have not tried it via php's exec (nor on Amazon to be honest...).
If you're doing ssh I'd suggest phpseclib, a pure PHP SSH implementation. It's a ton more portable than the PECL SSH extension and more reliable too.
I try to run the wkhtmltopdf (0.11.0 rc1) with php (5.4.2) on apache (2.4.2).
When I try to launch wkhtmltopdf-i386 --use-xserver http://www.google.com google.pdf 2>&1, I can find my pdf. Here my php code
<?php
$cmd= '/usr/bin/wkhtmltopdf-i386 http://www.google.com google.pdf 2>&1';
$ret = shell_exec($cmd);
echo $ret;
?>
It works with apache and as command line php test.php.
Because my target page contains many images and some "heavy" js charts. I have got a Segmentation Fault with the wkhtmltopdf command when I try to turn it into pdf.
The only way to make it work is to use xvfb as X11 emulator. The code looks like this :
<?php
$cmd= '/usr/bin/xvfb-run /usr/bin/wkhtmltopdf-i386 --use-xserver http://www.google.com google.pdf 2>&1';
$ret = shell_exec($cmd);
echo $ret;
?>
This script works with the command line php test.php but it doesn't work with apache. If I take a look into the apache's process with htop, I can see that there are two process (with php test.php) :
xvfb
wkhtmltopdf
When I launch with apache I have only xvfb process. It finish by a timeout from apache because it's waiting the wkhtmltopdf process.
I can make it works with apache (2.2.21) and php (5.3.10).
Is there something that I'm missing ? Maybe something in the apache's config-files ?
I was having the same problem. I was using the exec function, but the same applies to shell_exec. The function execution was disabled in php.ini.
SOLUTION: Remove the shell_exec string from the disable_functions at php.ini file.
I am not sure why your second version is not callable from Apache (must not be using the same shell, since shell_exec uses a shell?), but as a work-around could you (from Apache PHP) shell_exec("php test.php"); and get your intended result?
Perhaps also try one of the other process execution functions such as pcntl_exec.
it's mostly because of ownership and permissions, try
su www-data (for debian)
php test.php
you'll probably see the error.
I have installed the php extension ssh2 and it work fine. but when I want use it to get the remote server's nginx version or restart the nignx after I change the config file of the remote use the ssh2 too, It don't work as I wish.
my php code is like this:
<?php
$connection = ssh2_connect('115.238.73.136', '65000', array('hostkey'=>'ssh-rsa'));
if(!$connection) "error_1\n";
if(ssh2_auth_pubkey_file($connection, 'root','/data/web/control_center/sh/.ssh/id_rsa.pub','/data/web/control_center/sh/.ssh/id_rsa', 'test')){
$stream = ssh2_exec($connection, "/usr/local/nginx/sbin/nginx -v");
stream_set_blocking($stream, true);
while( !feof($stream) ) {
$cmd[] = fgets($stream);
}
var_dump($cmd);
}
there is nothing in the $cmd. and I change the "
/usr/local/nginx/sbin/nginx -v
to
/usr/local/nginx/sbin/nginx -t;
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`;
and I can't get anything too.
I guess the
/usr/local/nginx/sbin/nginx
can't be execute by the
My recommendation would be to stop using libssh2 and start using phpseclib, a pure PHP SSH implementation.
ssh2_exec() (using php5 ssh ext) sometimes never returns output, and sometimes it does! This is with setting blocking to true, and even adding wait periods, which is a no no. Try connection to LOCALHOST and sometimes you get output, sometimes not. Im running Karmic Ubuntu, and for the life of me pissed me off to no end. phpseclib, however, works perfect.