I have the following code that, when a button is pressed, should send a post request to the following PHP file through ajax and run a command that uploads a hex file to an Arduino. But, when I press the button, nothing happens.
<?php
if (isset($_POST['button'])) {
$file = fopen('src/sketch.ino', 'w+');
$code = $_POST['code'];
fwrite($file, $code);
echo "Form was submitted";
shell_exec("avrgirl-arduino flash -f firmware.hex -a uno -p /dev/cu.usbmodem1421");
} else {
echo "Form has not been submitted";
}
shell_exec("platformio run");
?>
Related
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");
I'm creating a button on my web page.I want that when someone presses this button an execution of a process of Linux commands on my first server (like "cd file" "./file_to_execute"..) when these commandes are done and finished i want to connect on another server by ssh and to execute another commands.
the probleme is how can i know that the commands before are already finished to proceed to the second part which is to connect on another server .
to resume :
first step connect on the first server , execute some commands
=> when these commands are done ( the part i dont know how to do it )
second step : to connect on another server and execute some others commands.
I'm searching for a way that will allows me to add some pop up to inform the user of my web page that he finished the first step and he started the second.
<?php
$hostname = '192.177.0.252';
$username = 'pepe';
$password = '*****';
$commande = 'cd file && ./file_one.sh';
if (false === $connection_first = ssh2_connect($hostname, 22)) {
echo 'failed<br />';
exit();
}
else {
echo 'Connected<br />';
}
if (false === ssh2_auth_password($connection_first, $username, $password)) {
echo 'failed<br />';
exit();
}
else {
echo 'done !<br />';
}
if (false === $stream = ssh2_exec($connection_first, $commande)) {
echo "error<br />";
}
?>
Thanks
PS: sorry for my English, I'm from Barcelone
To handle events where an exception occurs i would recommend using a try/catch statement, like the one below:
try {
echo inverse(5) . "\n";
echo inverse(0) . "\n";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
When you're trying to handle events and need to know when they finish, there are a few ways to achieve this. You can either set a boolean to true after the command has been executed (like what you are already doing). OR you can return output from the command by printing the output to a txt file and then echoing out the returns of this file. See code below:
exec ('/usr/bin/flush-cache-mage > /tmp/.tmp-mxadmin');
$out = nl2br(file_get_contents('/tmp/.tmp-mxadmin'));
echo $out;
At this point you can create conditions based off of what is returned in the $out variable.
When running
ls | php script.php
fgets(STDIN) is not waiting for user input:
<?php
$pipe = stream_get_contents(STDIN);
echo "Enter something";
$line = fgets(STDIN);
But if I run
php script2.php
With script2.php :
<?php
echo "Enter something";
$line = fgets(STDIN);
The script pauses waiting for my input.
How can I get pipe & wait for user input?
Instead use readline function of php, it will work fine,
$string=readline("Enter value: ");
echo $string;
I want to know: How to send mail via Shell command line through this code
#!/usr/local/vesta/php/bin/php
<?php
error_reporting(NULL);
if (empty($argv[1])) {
echo "Error: not enough arguments\n";
exit(3);
}
$options = getopt("s:f:");
define('NO_AUTH_REQUIRED',true);
include("/usr/local/vesta/web/inc/main.php");
// Set system language
exec (VESTA_CMD . "v-list-sys-config json", $output, $return_var);
$data = json_decode(implode('', $output), true);
if (!empty( $data['config']['LANGUAGE'])) {
$_SESSION['language'] = $data['config']['LANGUAGE'];
} else {
$_SESSION['language'] = 'en';
}
require_once('/usr/local/vesta/web/inc/i18n/'.$_SESSION['language'].'.php');
// Define vars
$from = 'Vesta Control Panel <vesta#'.gethostname().'>';
$to = $argv[3]."\n";
$subject = $argv[2]."\n";
$mailtext = file_get_contents("php://stdin");
// Send email
if ((!empty($to)) && (!empty($subject))) {
send_email($to,$subject,$mailtext,$from);
}
This code is available on my server, link file: /usr/local/vesta/web/inc/mail-wrapper.php and my server use Vesta CP Thank for helping me.
In an assignment I'm having trouble running a php script with page handling. It's outputting actual php code when submitted through another php page but works fine on its own.
I have a html login page which submits via submit buttons rather than form submit [a requirement]. This submits to login.php.
Seperately I have testBalance.php which checks a file balance.txt on my server which simply has an amount (1000). testBalance.php calls a function in getBalance.php to return the amount here.
THE PROBLEM IS when I run testBalance.php by itself it works just fine. Displaying "Account Balance: 1000.00" but when I attempt to set (in login.php) testBalance.php as the redirect url, the page literally displays code from my testBalance.php page: "Account balance: "); printf ( "%01.2f", $returnValue ); echo ("
"); ?> " I know it's convoluted, this is an intro to php portion of an web prog. class. I'm guessing it has to do with the value pairs that are being passed through to the pages. Can anyone help?
LOGIN.HTML snippit
<input type="button" name="sub_but" id="bal" value="check balance"
onclick="location.href = 'login.php' + '?' + 'name='+ document.forms[0].username.value +
'&redirectURL=' + 'bal';" />
LOGIN.PHP
<?php
$NAME=$_GET["name"];
$PAGE=$_GET["redirectURL"];
$DESTINATION="";
if ($NAME == ''){ /* HANDLES NAME ERRORS */
echo "PLEASE RETURN AND ENTER A NAME.";
}
elseif (ctype_alpha(str_replace(' ', '', $NAME)) === false) {
echo "$NAME is not a valid name. Name must contain letters and spaces only";
}
else{
if($PAGE=='with'){
$DESTINATION = "withdraw.html";
}
elseif($PAGE=='bal'){
//$DESTINATION = "balance.html";
$DESTINATION = "testBalance.php";
}
elseif($PAGE=='depos'){
$DESTINATION = "deposit.html";
}
elseif($PAGE=='weath'){
$DESTINATION = "weather.html";
}
elseif($PAGE=='xchang'){
$DESTINATION = "currency.html";
}
/*echo("$DESTINATION\r\n");*/
header("Content-Length: " .
strlen(file_get_contents($DESTINATION)));
header("Cache-Control: no-cache");
readfile($DESTINATION);
}
?>
testBalance.php body snippit
<?php
include 'getBalance.php';
$returnValue = readBalance();
echo "<p>Account balance: ";
printf( "%01.2f", $returnValue );
echo "</p>";
?>
getBalance.php
<?php
function readBalance(){
$file = "balance.txt";
$fp = fopen($file, "r");
if (!$fp){
echo "<p>Could not open the data file.</p>";
$balance = 0;
}
else{
$balance = fgets($fp);
fclose ($fp);
}
return $balance;
}
?>
readfile() doesn't EXECUTE anything it reads. It's literally just slurping in the file's bytes and spitting them out to the client. It's basically doing
echo file_get_contents(...);
If you want your other files to be executed, you need to include() or require() them instead. Or you could try eval(), but you really don't want to go down that route. eval() is evil and dangerous.