Executing bash with PHP - php

I want to execute bash command with script. If I echo the command, I get the proper response. But if I execute it from browser, it does not work. If I echo the command ls, it is executed and shown. I have granted all permissions. If I write command in terminal it works.
<?php
$banlista = $_POST['banlista'];
$ip = $_POST['ip'];
$command = "fail2ban-client set $banlista banip $ip";
$sporocilo = shell_exec("$command");
?>

It's better to run the command within the php code. You can get a response
from the actual command and verify that it worked.
If you have your code this way
<?php
$banlista = $_POST['banlista'];
$ip = $_POST['ip'];
$command = "fail2ban-client set $banlista banip $ip";
$sporocilo = shell_exec("$command");
?>
You can try to add this:
<?php
exec("sudo user /usr/bin/fail2ban-client set $banlista banip $ip", $output, $return);
echo "Failtoban client returned $return, and output:\n";
var_dump($output);
?>
You're probably missing a sudo and a user who has the right to run the
command from the browser.

Related

Php exec Powershell script Azure AZ commands do not run, all the other functions do

My PHP script
exec('powershell.exe -ExecutionPolicy Unrestricted -NoProfile -InputFormat none -file "..\..\scripts\addcustomer.ps1"', $output);
AZ function in the script:
$file = “$PSScriptRoot\Azure\pass.txt”
$azureuser = “user#contoso.com”
$Password = Get-Content $file | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential($azureuser, $Password)
Login-AzAccount -Credential $credential
New-AzDnsRecordSet -Name "$name" -RecordType A -ZoneName "contoso.co" -ResourceGroupName "RG" -Ttl 3600 -DnsRecords (New-AzDnsRecordConfig -IPv4Address "$ipaddress")
The $output does not display any output of this function. I confirm that if I run the script manually, everything works.
Many thanks.
Use exec($your_command, $output, $error_code) and see what $error_code contains. It may just be because powershell.exe isn't in the PATH env variable in PHP.
Try to put the full path to your PowerShell executable, typically something like this:
<?php
// A default powershell path in case "where powershell" doesn't work.
define('POWERSHELL_EXE', 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe');
// Find the path to powershell with the "where" command.
$powershell_exe = exec('where powershell');
if ($powershell_exe === false) {
$powershell_exe = POWERSHELL_EXE;
}
// Also check that the path to your script can be found.
// If it doesn't then you can use realpath() to get the
// full path to your script.
$cmd = $powershell_exe .
' -ExecutionPolicy Unrestricted -NoProfile -InputFormat none' .
' -file "..\..\scripts\addcustomer.ps1"';
$last_line = exec($cmd, $full_output, $error_code);
// Then check if $last_line !== false and check $error_code to see
// what happened.
var_export([
'$cmd' => $cmd,
'$last_line' => $last_line,
'$full_output' => $full_output,
'$error_code' => $error_code,
]);
Another important point: Has the user running your PHP code enought rights to do what you are doing in your PS1 script?
You may need to run your PowerShell script with elevation privilege or another user. I never did that but perhaps this topic could help:
PHP command to execute powershell script as admin

How to run Fast lane in terminal using php? exec() or shell_exec()

i was trying run fast lane commands in terminal using php, the commands like cd, pwd, ls and chmod working fine in php using exec or shell exec functions but when i try to run fast lane command it throws error 127, how can i run fast lane using php?
function terminal($command)
{
$output = [];
$return_var = '';
//exec
if (function_exists('exec')) {
exec($command, $output, $return_var);
} else {
$output = 'Command execution not possible on this system';
$return_var = 1;
}
return array('output' => $output, 'status' => $return_var);
}
// $path = "cd /Applications/XAMPP/xamppfiles/htdocs/mystudiomobile/cordova7/platforms/ios/fastlane";
// $path_c = "fastlane init"; path and path_c present in test.sh
$command = "/Applications/XAMPP/xamppfiles/htdocs/php1/test.sh";
$path_change = terminal("$command");
if($path_change['status'] == 0)
{
echo json_encode($path_change['output']);
echo $path_change['status'];
}
else
{
echo "some problem";
echo $path_change['status'];
}
I found out that there are limitations in php to perform http and https protocols using system functions like exec. which could not run fast lane, the best practise is to go with bash script which access php instead of vice versa .

How do I fix php code not executing with input from a form?

I am new here and to php.
I am running a simple php code to ssh into a remote server and execute a long command:
ldapsearch -b o=Devices -h 24.33.42.43 -D "uid=aaa_user,ou=AppUsers,o=services" -w "authAuthAcc01" rrWiFiDeviceMAC="A4-F1-E8-24-6B-28"
for a specific MAC Address A4-F1-E8-24-6B-28. I used a form to input a MAC Address, then submit to execute ssh into the linux server before executing the command above. echo $CMD will display the full command on the page indicating $var=$_POST[] works. However, the code won't run to ssh2 function. It's like $var becomes empty or something. But if I hardcode $var and run from PHP command line it works.
Please, what am I doing wrong!
Thanks for your help.
<form action="MAC_search.php" method="post">
Enter MAC Address to Search:<input type="text" name="mac">
<input type="submit" name='submit' value ='Search' />
</form>
<?php
if ($_POST["submit"] ){
$MACD=$_POST['mac'];
//$MACD='F8-62-14-E9-F7-51'; *****Hardcoding here works*****
$CMD = "ldapsearch -b o=Devices -h 24.33.33.5 -D \"uid=a .
aa_user,ou=AppUsers,o=services\" -w \"authAuthAcc01\"
rrWiFiDeviceMAC=\"$MACD\"";
echo $CMD; **** Here echos on the page also *******
$connection = ssh2_connect("x.x.x.x", 22);
ssh2_auth_password($connection, $username, $password);
$stream = ssh2_exec($connection, "$CMD");
stream_set_blocking($stream, true);
$output = stream_get_contents($stream);
$resultfile = fopen("data.txt", "w") or die("unable to open
file");
fwrite($resultfile, $output);
fclose($myfile);
}
?>

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

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.

Categories