Phpseclib on error execute different command - php

I am using Phpseclib 1.0 to execute multiple commands on remote server . I know how configure it to exit on error but is there any way to setup that if there is error it will stop and execute different commands ? For example I have 8 commands to execute when it all works it's don't show any output so it's all good . if for example after 4th command it gets error I need that it stop to execute the rest commands and execute command to delete what it did as well another command to run another script to notify admins about accident. The current code I using is
<?php
include('Net/SSH2.php');
ini_set('display_errors', 1);
error_reporting(E_ALL);
$ssh = new Net_SSH2('host');
if (!$ssh->login('user', 'password')) {
exit('Login Failed');
}
echo '<pre>' . $ssh->exec('create vlan “TEST”') . '</pre>';
echo '<pre>' . $ssh->exec('configure vlan TEST tag 666') . '</pre>';
echo '<pre>' . $ssh->exec('configure vlan TEST add ports 8 untagged') . '</pre>';
echo '<pre>' . $ssh->exec('create l2vpn vpws TEST fec-id-type pseudo-wire 666') . '</pre>';
// Execute on error
echo '<pre>' . $ssh->exec('delete l2vpn vpws “TEST”') . '</pre>';
echo '<pre>' . $ssh->exec('delete vlan “TEST”') . '</pre>';
?>

You could check stderr by doing $ssh->getStdError() and seeing if it's empty. There's also $ssh->getExitStatus().

Related

User not allowed to use crontab

I want to edit cron tab daily to delete / add some jobs .. so I added cron job ( helper job ) to run php script to handle these edits.
When I run this script by browser works fine .. but when it runs using the helper job not running and I receive notification mail from cpanel with this error :
Please note I am on shared hosting plan with C-Panel so I have no root access .. but the code working fine when run from browser.
Error:
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
PHP Script:
exec('crontab -l', $crontab);
$record_found = false;
foreach($crontab as $key => $value){
if(strpos($value, 'record_extra.php') !== false){
//echo $key . ' ' . $value . '<br>';
unset($crontab[$key]);
$record_found = true;
}
if(strpos($value, 'record.php') !== false){
//echo $key . ' ' . $value . '<br>';
unset($crontab[$key]);
}
}
if($record_found){
file_put_contents('/tmp/crontab.txt', arrayToString($crontab) . $firstJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output . $secondJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
} else {
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output . $firstJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output . $secondJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
}
Need your help.
Thanks in advance.
Seems like the user isn't allowed to run crontab. Look into the files "/etc/cron.allow" and "/etc/cron.deny". If cron.allow exists, then only users in that list can use crontab. Users in cron.deny, can't use. If neither exist then only root user can use crontab.
I've looked at numerous "can't run crontab" posts and none of them addressed my problem. I want to report my unique solution.
My service provider copied my system to another place, and apparently they literally used the "cp" command to do it, which meant that no setuid bits were preserved. /usr/bin/crontab was one of several files for which I had to restore the setuid bit.
You can't run crontab if it doesn't have the setuid bit set.

shell_exec does not execute from browser requests

After the user submits a data via POST, I show a temporary page and do the process on the background with shell_exec, atleast that's what I'm trying to do.
Here's my page setup:
C:\laragon\www\index.php
<?php
try {
shell_exec("php " . __DIR__ . "/test.php");
} catch(Exception $e) {
echo "Exception: " . $e;
}
?>
C:\laragon\www\test.php
<?php
$myfile = fopen(__DIR__ . "/testfile.txt", "w");
echo "test";
?>
If I go to localhost or localhost/index.php, the second script doesn't run. However, when I try to call both scripts from cmd, it works with both of them.
php C:\laragon\www\index.php
php C:\laragon\www\test.php
They both work and create a file called testfile.txt
Your webserver runs as a specific user and needs the path to php.exe as there is no path environment variable for the webserver user:
shell_exec("c:\path\to\php " . __DIR__ . "/test.php");

Connection between php and scilab (FuzzyToolbox)

I want to do connection between php and scilab, this my code
try {
$path = 'C:\\wamp64\\apps\\scilab-5.5.2\\bin\\Scilex.exe';
$path_script = "ea=loadfls('C:\\wamp64\\www\\scilab\\estilosaprendizaje.fls');res=evalfls([-11,11],ea); disp(res);exit;";
$command = $path . ' -nb -e "' . $path_script.'"';
echo $command;
exec($command, $output);
foreach ($output as $line) {
print_r($line);
echo "<br />";
}
} catch (Exception $e) {
echo 'Excepción capturada: ', $e->getMessage();
}
but when I run the php, it does not work, it keeps loading, I do not have error messages or anything.
In scilab my code it works.
My output in scilab
Try adding -nw to $command to lauch scilab in console mode. It may be because scilab try to launch itself in graphical mode. I dont have php nor fuzzy toolbox, so I could not test it.

Working Python script throw out error when PHP calls it with exec

I have a simple php code:
<?php echo exec('/opt/anaconda2/bin/python test2.py 2>&1'); ?><br>
And test2.py simply import a library called theano
import theano
It works under ssh but throw out:
KeyError: 'PATH'
when looking at the php in browser.
What's happeneing here? Is there any way that I can see full trace of error msg?
You can try this for the PHP side of things, but I think KeyError is a Python thing:
<?php
$output = array();
exec('/opt/anaconda2/bin/python test2.py 2>&1', $output, $returnCode);
echo 'Output is: ' . PHP_EOL;
var_dump($output);
echo 'Return code is: ' . PHP_EOL;
var_dump($returnCode);
?>

Call to undefined function readline()?

I am integrating Dropbox into my PHP based website. When i try to run the following code. i got this Fatal error: Call to undefined function readline() on the last line.
require_once "dropbox-sdk/Dropbox/autoload.php";
use \Dropbox as dbx;
$appInfo = dbx\AppInfo::loadFromJsonFile("app-info.json");
echo "<pre>";
print_r($appInfo);
echo "</pre>";
$webAuth = new dbx\WebAuthNoRedirect($appInfo, "PHP-Example/1.0");
echo "<pre>";
print_r($webAuth);
echo "</pre>";
$authorizeUrl = $webAuth->start();
echo "1. Go to: " . $authorizeUrl . "\n<br>";
echo "2. Click \"Allow\" (you might have to log in first).\n<br>";
echo "3. Copy the authorization code.\n<br>";
$authCode = \trim(\readline("Enter the authorization code here: "));
I have come through different forum where people said it will work in Command line , But I don't understand how? Any idea ?
Or just use this to simulate it
if(!function_exists("readline")) {
function readline($prompt = null){
if($prompt){
echo $prompt;
}
$fp = fopen("php://stdin","r");
$line = rtrim(fgets($fp, 1024));
return $line;
}
}
readline() is for running on the command line not via the web browser.
To check you have it installed on your server, type:
php -i | grep Configure
Probably, you have not installed it and you should compile it yourself or ask your hosting admin to if they allow this.
Just had this issue on Ubuntu.
Found this answer: https://askubuntu.com/a/264329/166
The solution in this scenario was to install the package php5-readline.

Categories