Run bat file which contains powershell script using php? - php

I want to run bat file via php.This bat file contains power shell script.I am able to run this via cmd but not via php.
AwsExecute.bat is,
powershell -command ./AWSExecuter.ps1
This AwsExecuter.ps1 which is inside the C:\AWS\Distributed-setup-3 folder.
In php I have tried like this,
Trail.php which is in http://localhost/performance folder.
<?php
exec('C:\AWS\Distributed-setup-3\AwsExecute.bat');
echo "Done";
?>
As well as,
<?php
exec('c:\WINDOWS\system32\cmd.exe /c START C:\AWS\Distributed-setup-3\AwsExecute.bat');
echo "Done";
?>
Both are not working here, I tried to another bat file which just creates a php file it works fine.
That is First.bat,
echo off
break>"C:\Users\Vidya\Desktop\performance.php"
echo done

Try running system
system("cmd /c C:\AWS\Distributed-setup-3\AwsExecute.bat");
For more on system read this

Related

how to run Python script from php

I want to run python script from php.
this is my python code. It is saved in /home/pi and name of file is hello.py
#! /usr/bin/python
import bluetooth
bd_addr="xx:xx:xx:xx:xx:xx"
port=1
sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr.port))
data=""
while 1:
try:
data +=sock.recv(1024)
data_end=data.find('\n')
if data_end!=-1:
rec=data[:data_end]
print datas
data=data[data_end+1:]
except KeyboardInterrupt:
break
And here is my php code. It is saved in /var/www/html and name of file is php.php
<?php
$output=shell_exec('ls -l /home/pi/hello.py');
echo "<pre>$output</pre>";
?>
And I insert localhost/php.php in chrome, it displays
-rw-r-r- 1 pi pi 378 Mar 8 12:07 /home/pi/hello.py
what is the problem??
As pointed out by Jon Stirling, you are using "ls" to only listing the content of the folder or to check whether the file exist in that folder. To run the Python code, you need to change the PHP file into something like this:
<?PHP
$output=shell_exec('./hello.py');
echo "<pre>$output</pre>";
?>
ls command is used to list files in a directory or to get information about a file. You are ls-ing on your python file and the result is correct. It is providing you with information about the file.
Just put the file name inside of shell_exec that is /home/pi/hello.py. If you do not want to depend on the shebang and the command python is available in your shell environment then you can use python /home/pi/hello.py instead of bare /home/pi/hello.py.
Again, you used the variable datas with print where you intended to use data - fix it.
php code:
<?php
$output=shell_exec('python /home/pi/hello.py');
echo "<pre>$output</pre>";
?>
or:
<?php
$output=shell_exec('/home/pi/hello.py');
echo "<pre>$output</pre>";
?>
python code:
#! /usr/bin/python
import bluetooth
bd_addr="xx:xx:xx:xx:xx:xx"
port=1
sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr.port))
data=""
while 1:
try:
data +=sock.recv(1024)
data_end=data.find('\n')
if data_end!=-1:
rec=data[:data_end]
print data
data=data[data_end+1:]
except KeyboardInterrupt:
break

How to run php script that executes the .bat file?

I facing error while running the below php script. My php script having the little code for executing the .bat file which is located on my local server.
PHP file code : -
<?php
system("cmd /c C:\Users\my_computer_name\Desktop\sample.bat");
?>
sample.bat file code : -
#echo off
title This is your first batch script!
echo Welcome to batch scripting!
start calc.exe
When i remove start calc.exe from .bat file code it works fine.
Thank you in Advance....

problems with chdir in PHP script to call a shell script

I am trying to use a PHP script to call a shell script and print the output to the browser.
I have confirmed that all permissions settings are set correctly, both the PHP and the bash script have full control.
If I run the shell script in the current directory (same directory as the PHP file) then it works fine. But if I try to run the shell script from /some/other/directory/shell.script, then it does not work. Why is this so?
I have tried to chdir() to the other directory, but after running a getcwd(), it never changed directories.
I have also tried the exec command by itself and it does not work.
<?php error_reporting(E_ALL); ini_set('display_errors',1);
echo "<h1>EDS Count Report</h1><p>";
$output=shell_exec('cd /app/script/catalog/ && ./EDScount_report.sh');
echo $output;
?>
contents of SHELL script:
#!/bin/bash
echo "this is a test!"
Try this please
<?php
$output=shell_exec('sh /path/to/otherdirectory/shellscript.sh');
echo $output;
?>
if you are certain that you can access that dicertory , you can use also this:
<?php
$output=shell_exec('cd /path/to/otherdirectory/ && ./shellscript.sh');
echo $output;
?>

Execute shellscript from PHP (Localhost, Mac OSX)

I want to run a shell script from a php-based website on my localhost (using MAMP on Mac) but it does not work, unfortunately.
Heres's the shell script:
#!/bin/bash
open /Users/my_username/Desktop/aiSee.app
If I run it from the terminal, it works fine and opens the app. Using this code in my website, it does not work:
<?php
echo exec('script.sh');
?>
No errors or something are displayed, it just doesn't work.
The script is located at the same source as the .php file for the website.
open in bash does not do what you think it does. You need cat instead:
#!/bin/bash
cat /Users/my_username/Desktop/aiSee.app
Has your issue been solved?
If not, try echo exec('sh script.sh'); or echo exec('bash script.sh');.
This question may solve your problem: how to run a .sh file from php?

exec not working in php script ( WAMP Server )

I am using WAMP server on my system to execute php scripts.
I want to execute a script test.php from my main script main.php.
For that I am using exec function like this exec('php test.php');. In test.php I have given one echo statement.
But when I run my main.php script from the browser I am not able to see output of test.php script.
What am I doing wrong ? please suggest.
You have to give the proper path of php.exe
exec("c:\wamp\<where ever you exe is>/php.exe test.php");
so it has to be a proper path
use this command
echo exec('php test.php', $output); //this will print your echo statement.
print_r($output);

Categories