Executing PowerShell Script from Server Using PHP - php

I have a PowerShell script hosted on a server. I am calling PowerShell script from Php as below:
<?php
header('Content-Type: text/plain');
$csv = file_get_contents('http://domaincom/wp-content/uploads/csv-samples.csv');
echo $csv;
shell_exec('pwsh -File http://domaincom/wp-content/uploads/pscript.ps1');
$psPath = "powershell.exe";
$psDIR = "http://domaincom/wp-content/uploads/";
$psScript = "pscript.ps1";
$runScript = $psDIR. $psScript;
$runCMD = $psPath." ".$runScript." 2>&1";
echo "\$psPath $psPath <br>";
echo "\$psDIR $psDIR <br>";
echo "\$psScript $psScript <br>";
echo "\$runScript $runScript <br>";
echo "\$runCMD $runCMD <br>";
exec( $runCMD,$out,$ret);
echo "<pre>";
print_r($out);
print_r($ret);
echo "</pre>";
?>
Script executes and I can see csv-samples.csv results in browser, but the powershell script doesn't execute. I get below message in browser:
Site,URL,Category
Sitepoint,http://www.sitepoint.com/,Web development
Html.it,http://www.html.it/,Web development
Wamboo,http://www.wamboo.it/,Web development$psPath powershell.exe <br>$psDIR http://domaincom/wp-
content/uploads/ <br>$psScript pscript.ps1 <br>$runScript http://domaincom/wp-
content/uploads/pscript.ps1 <br>$runCMD powershell.exe http://domaincom/wp-
content/uploads/pscript.ps1 2>&1 <br><pre>Array
(
[0] => sh: 1: powershell.exe: not found
)
127</pre>
Your help is very much appreciated.
Thank You.

Related

Running a .PY file from a PHP script

I have this PHP code:
ini_set('display_errors', 1);
ob_start();
passthru('/usr/bin/python3 /home/domains/mydomain.pl/public_html/a.py /home/domains/mydomain.pl/public_html/code2.txt');
$output = ob_get_clean();
echo $output;
$message = exec("/usr/bin/python3 /home/domains/mydomain.pl/public_html/a.py /home/domains/mydomain.pl/public_html/code2.txt");
print_r($message);
$command = shell_exec('python3 /home/domains/mydomain.pl/public_html/a.py /home/domains/mydomain.pl/public_html/code2.txt');
echo $command;
$output=shell_exec('python3 /home/domains/mydomain.pl/public_html/a.py /home/domains/mydomain.pl/public_html/code2.txt');
echo "<pre>$output</pre>";
$command = escapeshellcmd('python3 /home/domains/mydomain.pl/public_html/a.py /home/domains/mydomain.pl/public_html/code2.txt');
$output = shell_exec($command);
echo $output;
exec('sudo -u www-data python3 /home/domains/mydomain.pl/public_html/a.py /home/domains/mydomain.pl/public_html/code2.txt');
system("cd /usr/lib/cgi-bin && sudo python3 /home/domains/mydomain.pl/public_html/a.py /home/domains/mydomain.pl/public_html/code2.txt");
I would like PHP to:
1. launched the a.py script
2. returned the result which the console will display from a.py and display it in the web browser.
At the moment, nothing is showing up. I do not have any error message or warning.
Does anyone know what is wrong in the above code?
My server allows running scripts with the console

How to run powershell command in PHP

I want to run powershell through PHP program. In powershell I have to run the command "Get-FileHash -Algorithm sha256 .\SHYAM.jpeg" to generate hash value. Shyam.jpeg is a file which is located in the directory C:\wamp64\www\Dedup. Here is my code
<?php
$psPath = "C:\\Windows\\SysWOW64\WindowsPowerShell\v1.0\\powershell.exe";
$psDIR = "C:\\wamp64\\www\\Dedup";
$psScript = "SHYAM.JPEG";
$runScript = $psDIR. $psScript;
$runCMD = $psPath.'Get-FileHash -Algorithm sha256./'.$psDIR.$psScript;
$out= shell_exec($runCMD);
echo "<pre>";
print_r($out);
echo "</pre>";
?>
But it is not working. I am struggling to make it work. When I tried "Get-FileHash -Algorithm sha256 .\SHYAM.jpeg" command in powerShell it works fine. Please help me out.
The output is C:\Windows\SysWOW64\WindowsPowerShell
v1.0\powershell.exeGet-FileHash -Algorithm sha256./C:\wamp64\www\DedupSHYAM.JPEG
Missing a backslash escape in the path to PowerShell, no spaces, wrong folder path, it's not going to work.
Try something like:
<?php
$psPath = "C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe";
$fileDir = "C:\\wamp64\\www\\Dedup";
$fileName = "SHYAM.JPEG";
$runCMD = "$psPath -Command \"Get-FileHash -Algorithm SHA256 -Path '$fileDir\\$fileName' | Select-Object -ExpandProperty Hash\"";
$out= shell_exec($runCMD);
echo "<pre>";
print_r($out);
echo "</pre>";
?>
I haven't tried to exec it, but that at least prints a sensible looking command.
Replace the ./ with a space and add a space before the Get-FileHash.
$runCMD = $psPath.'Get-FileHash -Algorithm sha256./'.$psDIR.$psScript;
To
$runCMD = $psPath.' Get-FileHash -Algorithm sha256 '.$psDIR.$psScript;

Run scripts in php

I am running a python script through my php. It works fine on console but returns null when I run it on browser.I even tried writing output to a file, but it doesn't return anything.
The php code is:
<?php
$y = "python Code.py";
$output = shell_exec($y);
if($output!=null){
echo $output;
}
else {
echo "No output";
}
$myfile = fopen("test.txt", "w") or die("Unable to open file!");
fwrite($myfile, $output);
fclose($myfile);
?>
$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;
#!/usr/bin/env python **is this in the first line of you python script?**
chmod +x /usr/custom/test.py

sudo_exec returns nothing

I try to ping www.google.de with shell_exec and store the result into a variable but i get no result back from shell_exec.
<?php
$ping = 'sudo ping -c 4 ';
$url = 'www.google.de';
$command = $ping . $url;
$ping_result = shell_exec($command);
$datei = fopen("/var/www/myProject/result_ping","w") or die ("Could not open file!");
sleep(10);
if ($datei == false)
{
$ping_result = "Cannot open file!";
}
else
{
fwrite ($datei , $ping_result);
fclose ($datei);
}
echo $command; //Output: sudo ping -c 4 www.google.de
echo $ping_result; //Output: nothing
?>
The file result_ping has all rights (chmod 777).
Maybe the webserver is not allowed to execute ping?
Add 2>&1 to your command to ensure you're not getting an error message that shell_exec would filter off:
$command = $ping . $url . ' 2>&1';
shell_exec will return NULL in case of error. With that modification you redirect any error message to normal output, thus forcing shell_exec show every message you would normally get on a console session.

exec() phantomjs doesn't execute, no errors displayed

I'm on a Windows platform, I have the phantomjs executable on app/webroot/phantomjs and a js in the same folder.
When I do on php:
$response = exec($this->webroot . 'app/webroot/phantomjs/phantomjs getweb.js');
nothing happens. This is the content of the js:
console.log('Hello, world!');
phantom.exit();
I use absolute paths because this needs to be executed on a webpage online.
EDIT 1:
when using
$response = exec($this->webroot . 'app/webroot/phantomjs/phantomjs getweb.js 2>&1', $s, $o);
echo $response;
echo "<pre>";
print_r($s);
echo "</pre>";
echo "<pre>";
print_r($o);
echo "</pre>";
I get
Array
(
)
1
EDIT 2:
this works fine:
echo exec("whoami");
EDIT 3:
This does nothing:
$response = exec($this->webroot . 'app/webroot/phantomjs/phantomjs getweb.js 2>&1', $s, $o);
I was facing the same problem..
The thing is phantomjs requires complete path for all.
Here is the solution I came up with:
$getout = exec('D:\\xampp\\htdocs\\phantomjsdir\\phantomjs.exe D:\\xampp\\htdocs\\rasterisejsdir\\rasterize.js http://localhost/pagetobecaptured/test D:\\xampp\\htdocs\\outputfiledir\\test2.jpg "1800px*840px"',$o,$e);

Categories