Possible to manage linux services from website? - php

I have a couple services that id like to have access to from a website. Like maybe a simple website with login for users and access to start|Stop|Restart services if they are down.
IDK if this is possible within a webpage as im not all that experienced yet.
From a shell i can just type out service b3-2 start and the service will start. now i just want to be able todo that from within a webpage where the start|Stop|Restart are buttons and there's a info/status area and an output box below everything to show errors if any.
If anyone can help me with this that would be awesome. Mostly my questions are-
Is it possible to do this from a webpage?
If so how hard will it be to create?
What else can you tell me to better help me with this?
on a side note il be using this for mostly only my B3 services(Big Brother Bot).

I wrote long time ago an 'Eggdrop Webinterface' in PHP with http://de.php.net/manual/en/book.ssh2.php Module.
You can connect with this module via SSH to your linux server and submit shell commands. In my Webinterface I wrote an Shellscript that do all the jobs (Add / remove / start / stop and so on) and executed over php ssh2 module.
<?php
class ssh
{
private $sql;
private $con;
private static $instance;
public static function getInstance()
{
if (!self::$instance)
{
self::$instance = new ssh();
}
return self::$instance;
}
function __construct()
{
$this->sql = sql::getInstance();
}
/* Verbindung über SSH aufnehmen */
public function connect ($rootid)
{
global $cfg;
$q = $this->sql->query("SELECT ROOT_SSH_IP, ROOT_SSH_USER, ROOT_SSH_PASS, ROOT_SSH_PORT FROM ".prfx."rootserver WHERE ROOT_ID = '".$rootid."'");
$r = $this->sql->content($q);
$blowfish = new Blowfish($cfg["BLOWFISHKEY"]);
$pass = $blowfish->Decrypt($r["ROOT_SSH_PASS"]);
$this->ssh_connect($r["ROOT_SSH_IP"], $r["ROOT_SSH_PORT"], $pass, $r["ROOT_SSH_USER"]);
return true;
}
/* Disconnect */
public function my_ssh_disconnect($reason, $message, $language) {
printf("Server disconnected with reason code [%d] and message: %s\n",
$reason, $message);
return true;
}
/* Eigentliche ssh_connect Funktion */
public function ssh_connect($host, $port, $pass, $user="root")
{
$methods = array(
'kex' => 'diffie-hellman-group1-sha1',
'client_to_server' => array(
'crypt' => '3des-cbc',
'comp' => 'none'),
'server_to_client' => array(
'crypt' => 'aes256-cbc,aes192-cbc,aes128-cbc',
'comp' => 'none'));
$callbacks = array();
$this->con = ssh2_connect($host, $port, $methods, $callbacks);
if (!$this->con) die('Connection failed');
else {
if (!ssh2_auth_password($this->con, $user, trim($pass))) {
die("login failed.");
}
}
return true;
}
/* Befehle ausführen */
public function ssh_exec ($cmd, $stderr=true)
{
if ($stderr == true)
{
$stream = ssh2_exec($this->con, $cmd);
$err_stream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
stream_set_blocking($err_stream, true);
$result_err = stream_get_contents($err_stream);
if (empty($result_err))
{
stream_set_blocking($stream, true);
$out = stream_get_contents($stream);
return $out;
}
else {
return $result_err;
}
}
else {
$stream = ssh2_exec($this->con, $cmd);
stream_set_blocking($stream, true);
$out = stream_get_contents($stream);
return $out;
}
}
/* Verbindung beenden */
public function quit()
{
$stream = ssh2_exec($this->con, 'exit');
stream_set_blocking($stream, true);
$output = stream_get_contents($stream);
return true;
}
}
In my case I stored Serveraccounts in MySQL Table with encrypted password.
hope that little class helps

if you want to "write it yourself" you could mix bash,php and/or other scripting..

Related

Why is ldap_errno() feeded the connection here?

I have a process in lumen project (Laravel 6.2) where a user is identified through an LDAP.
The code looks like this:
<?php
namespace App\Http\Helpers;
// Currently unused
// use App\User;
// use Firebase\JWT\JWT;
use Illuminate\Support\Facades\Log;
class LDAP
{
private $connection, $password;
protected $domain, $username, $ldap_address, $ldap_port;
/**
* Constructs the ldap connector with data used for the connection and
* bind process.
*/
function __construct()
{
$this->domain = env("LDAP_DOMAIN");
$this->username = env("LDAP_USERNAME");
$this->password = env("LDAP_PASSWORD");
$this->ldap_address = env("LDAP_ADDRESS");
$this->ldap_port = env("LDAP_PORT");
}
/**
* Establishes a connection to the ldap server and saves it in
* #var Resource $connection.
*
* #return true
* On success
* #return false
* On failure
*/
private function connect()
{
$this->connection = ldap_connect($this->ldap_address, $this->ldap_port);
if($this->connection)
{
Log::info("Connection established");
ldap_set_option($this->connection, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($this->connection, LDAP_OPT_REFERRALS, 0);
$bind = ldap_bind($this->connection, $this->domain . "\\" . $this->username, $this->password);
if($bind)
{
Log::info("Bind valid");
return true;
}
else
{
Log::info("Bind failed");
return false;
}
}
else
{
Log::info("Connection failed");
return false;
}
}
private function disconnect()
{
ldap_unbind($this->connection);
}
/**
* Searches for a specific person in the LDAP-Directory and returns important
* data from this person which will be used later in the application.
*
* #param String $person
* The person to search for
* #return Array $result
* The persons data
*/
public function getUser($username, $password)
{
try
{
$is_connected = $this->connect();
if(!$is_connected)
{
$this->disconnect();
return false;
}
$dn = "OU=Benutzer,OU=sdfsfd,DC=sfdsfsf,DC=de";
$fields = "(|(samaccountname=*$username*))";
$search = ldap_search($this->connection, $dn, $fields);
$result = ldap_get_entries($this->connection, $search);
if($result)
{
$bind = ldap_bind($this->connection, $this->domain . "\\" . $username, $password);
if($bind && strlen($password) > 0)
{
return mb_convert_encoding($result, 'UTF-8');
}
else
{
return "Invalid credentials!";
}
}
else
{
return "User does not exist!";
}
}
catch(\Exception $e)
{
$errno = ldap_errno($this->connection);
if ($errno) {
$ret = array("ldap_error" => $errno, "message" => ldap_err2str($errno));
}else{
$ret = array("exception_code" => $e->getCode(), "message" => $e->getMessage());
}
return $ret;
}
finally
{
$this->disconnect();
}
}
}
Now, we faced some issues when handling errors from the ldap_bind().
The error code thrown by the ldap functions couldnt be evaluated by Lumen, so we had to catch them and evaluate it manually through the ldap_errno functionality.
What puzzles me is that the $this->connection is passed to the ldap_errno() function. Why isnt it the $bind?
After all, its the bind which failed, not the connect. AFAIK the ldap_connect() doesnt even establish a connection, but instead verifies whether the credentials are plausible.
However, it works ^^ But why? What is happening in ldap_errno that the connection is passed to it, not the bind?
Because ldap_connect returns an internal handle that is identifying the "connection". ldap_errno and ldap_error then return information regarding the last error on that "connection".
So when you call them after an ldap_bind (which returns true or false depending on the outcome) you need the connection that this happened on, not the result of the bind.
Please note that "connection" does not necessarily mean that the connection to the server has already been established.

Can't run more than 3 gearman workers on same server

When i run gearman upto three workers on single server is working fine but when i start the 4th one php code is working fine but can't detect the new worker and also not clearing the job queue.
protected function createWorker()
{
$this->worker = new \GearmanWorker();
$config = $this->app->config->job_remote;
$this->worker->addServer($config['host'], $config['port']);
return $this->worker;
}
public function listen($eventType, $callback)
{
if (!($this->worker instanceof \GearmanWorker)){
$this->worker = $this->createWorker();
}
$this->worker->addFunction($eventType, $callback);
return $this->worker;
}
public function doWork($worker)
{
if (!($worker instanceof \GearmanWorker)){
$worker = $this->createWorker();
}
$this->worker = $worker;
while (1) {
$this->worker->work();
$this->app->log->debug($this->worker->returnCode());
if ($this->worker->returnCode() != \GEARMAN_SUCCESS) {
break;
}
}
}
First I am calling 'listen' method and then 'doWork' method
Client Side Code:
protected function createClient()
{
$this->client = new \GearmanClient();
$config = $this->app->config->job_remote;
$this->client->addServer($config['host'], $config['port']);
return $this->client;
}
public function addTask($eventType, array $params)
{
if (!($this->client instanceof \GearmanClient)){
$this->client = $this->createClient();
}
// add single task in queue
$this->client->addTaskBackground($eventType, serialize($params));
// Run task
$this->client->runTasks();
}
Using GearmanManager You can run upto any no. of workers untill it slows down your computer and uses 100% cpu and 100% memory.
Gearman.ini
[GearmanManager]
worker_dir=./workers
count=50
dedicated_count=1
max_worker_lifetime=3600
auto_update=1
log_file=./logs/WebAnalyzerWorker.log
max_runs_per_worker=3
timeout=600
Run Gearman worker
./vendor/brianlmoon/gearmanmanager/pecl-manager.php -c ./gearman.ini -vvvvv
Worker CLass
<?php
include dirname(__DIR__)."/vendor/autoload.php";
use Services\Log;
use Services\ServiceInitialization;
use Lib\Technology\FindWhoisRecords;
use Illuminate\Database\Capsule\Manager as Capsule;
class DomainDetailFetchJob{
public function beforeRun()
{
ServiceInitialization::loadConfig();
ServiceInitialization::loadDatabaseConfiguration();
}
public function run($job, &$log)
{
$log[] = "Starting whois job";
$collection = "whois";
$this->beforeRun();
ServiceInitialization::$config->all();
$workload = $job->workload();
//workload
$whois = new FindWhoisRecords($workload);
if($whois->whois_result["err_code"]==0) {
$log[] = "Whois Info fetch successful";
//success save the details
$whois->whois_result["result"]["workload"] = $workload;
Capsule::table($collection)->where("workload", $workload)->update($whois->whois_result["result"], ['upsert' => true]);
}
else {
$log[] = "Whois Info fetch failed";
$logger = new Log();
$logger->write($whois->whois_result["err_msg"], "error", "Whois Record Job");
unset($logger);
}
}
}
Gearman Client
$client = new GearmanClient();
$client->addServer();
$client->doBackground("DomainDetailFetchJob", $url);

Subscribing and reading data from redis channels using php-redis simultaneously

I have to subscribe to all the channels of my Redis db and simultaneously read data from another hash in the same db node. The following is the code I have written for this using phpredis:
$notif = new Worker;
try {
// connect redis
$notif->connectRedisServer();
// start the worker processing
$notif->startWorkerProcess();
}
catch(Exception $e) {
exit('Exception: '.$e->getMessage());
}
class Worker {
private $redis;
public $recentHash = 'tracker.data.recent';
public $notifHash = 'tracker.settings.data';
public $serverConnectionDetails = { //Redis server connection details
};
private $redis;
// redis database used for getting recent record of every imei
const RECENTDATA_DB = 1;
public function connectRedisServer() {
if(!empty($this->redis))
return $this->redis; // already connected
$this->redis = new Redis();
if(!$this->redis->pconnect(
$this->serverConnectionDetails['redis']['host'],
$this->serverConnectionDetails['redis']['port']
))
return 'Redis connection failed.';
if(isset($this->serverConnectionDetails['redis']['password'])
&&
!$this->redis->auth($this->serverConnectionDetails['redis']['password'])
)
return 'Redis authentication failed.';
return $this->redis;
}
public function startWorkerProcess() {
$this->redis->select(self::RECENTDATA_DB);
$this->redis->pSubscribe(array('*'), array($this, 'processRedisData'));
}
public function processRedisData($redis, $pattern, $chan, $msg) {
$message = rtrim((string) $msg,",");
$tData = json_decode($message, true);
$tId = (int) $tData['tID'];
echo "Recent Published Data:";
var_dump($tData);
$data = $this->getNotifSettings($tId);
if(!empty($data)) {
echo "Redis Settings: ";
var_dump($trackerSettings);
}
}
public function getNotifSettings($tId) {
$data = $this->redis->hGet($this->notifHash, $tId); //This command doesn't return any data even if it exists for the $tId
if($data === false)
return null;
$data = json_decode($data, true);
return $data; // Always comes up as null
}
}
The problem here is that once I get subscribed to all the channels on db1 in Redis. I don't get any result if I try to run HGET, even though the data for the given key exists in the db. I have put additional comments in the code above to explain where the problem is. Check getNotifSettings() function.
Any help will be appreciated.

jaxl return to function that it's called from

I have library for xmpp transactions used jaxl libraries:
class xmpp{
public function register_user($username, $password){
require_once 'JAXL/jaxl.php';
$this->client = new JAXL(array(
'jid' => 'localhost',
'log_level' => JAXL_ERROR
));
$this->username = $username;
$this->password = $password;
$this->client->require_xep(array(
'0077' // InBand Registration
));
$thisClassObject =& $this;
$this->client->add_cb('on_stream_features', function($stanza) use(&$thisClassObject) {
$thisClassObject->client->xeps['0077']->get_form('localhost');
return array($thisClassObject, 'wait_for_register_form');
});
$this->client->start();
return;
}
public function wait_for_register_response($event, $args) {
if($event == 'end_stream') {
return;
}
else if($event == 'stanza_cb') {
$stanza = $args[0];
if($stanza->name == 'iq') {
if($stanza->attrs['type'] == 'result') {
echo "registration successful".PHP_EOL."shutting down...".PHP_EOL;
$this->client->end_stream();
return 'logged_out';
}
else if($stanza->attrs['type'] == 'error') {
$error = $stanza->exists('error');
echo "registration failed with error code: ".$error->attrs['code']." and type: ".$error->attrs['type'].PHP_EOL;
echo "error text: ".$error->exists('text')->text.PHP_EOL;
echo "shutting down...".PHP_EOL;
$this->client->end_stream();
return "logged_out";
}
}
}
}
public function wait_for_register_form($event, $args) {
$stanza = $args[0];
$query = $stanza->exists('query', NS_INBAND_REGISTER);
if($query) {
$form = array();
$instructions = $query->exists('instructions');
if($instructions) {
echo $instructions->text.PHP_EOL;
}
$this->client->xeps['0077']->set_form($stanza->attrs['from'], array('username' => $this->username, 'password' => $this->password));
return array($this, "wait_for_register_response");
}
else {
$this->client->end_stream();
return "logged_out";
}
}
}
these code are same as register_user.php, but implemented in a class;
i use this class in my code in this way:
$xmppObj = new xmpp();
$xmppObj('user','password');
/*
some more code after this
/*
when it execute , create user successfully but it's print a message ('registration successful ...') and application exited and it doesn't execute "some code after this" after the class function, in the other word it doesn't follow the code...
What can I do for solve this problem, a person can help me that familiar with JAXL library.
Looks like you are pretty much using the same code as found inside examples/register_user.php. Once user registration is successful, script closes XMPPStream as evident from this section of the code:
if($stanza->attrs['type'] == 'result') {
echo "registration successful".PHP_EOL."shutting down...".PHP_EOL;
$this->client->end_stream();
return 'logged_out';
}
You MUST instead call $client->send_end_stream(); and not $client->end_stream();. This will make sure underlying XMPPStream makes proper FSM state transition. Also add a callback for on_disconnect event, inside this callback you can again try to connect back with newly registered XMPP account and it should just work fine.
Note: Kindly checkout latest code from the repository. I made some updates which will allow core JAXLLoop to be re-initialized. If you are interested in details, here is the commit log.

Creating helper for my application

I have been creating a helper class for the Facebook PHP API in order to avoid reusing a lot of code. The helper works but the only problem is that its very slow.. and I also figured out why! when I initialize the class, the constructor is called twice! I checked in my code and the other elements which use this class only call it once (It's something inside the class itself) Could you please help me figure out what the problems could be?? Thanks!
class FbHelper
{
private $_fb;
private $_user;
function __construct()
{
// Initalize Facebook API with keys
$this->_fb = new Facebook(array(
'appId' => 'xxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxx',
'cookie' => true,
));
// set the _user variable
//
$this->doLog("Called Constructor");
//
$this->_user = $this->UserSessionAuthorized();
return $this;
}
function doLog($text)
{
// open log file <----- THIS GETS CALLED TWICE EVERY TIME I INITIALIZE THE CLASS!!
$filename = "form_ipn.log";
$fh = fopen($filename, "a") or die("Could not open log file.");
fwrite($fh, date("d-m-Y, H:i")." - $text\n") or die("Could not write file!");
fclose($fh);
}
function getUser() { return $this->_user; }
function getLoginUrl() { return $this->_fb->getLoginUrl(); }
function getLogoutUrl() { return $this->_fb->getLogoutUrl(); }
function UserSessionAuthorized()
{
// Checks if user is authorized, if is sends back user object
$user = null;
$session = $this->_fb->getSession();
if (!$session) return false;
try {
$uid = $this->_fb->getUser();
$user = $this->_fb->api('/me');
if ($user) return $user;
else return false;
}
catch (FacebookApiException $e) { return false; }
}
private function _rebuildSelectedFriends($selected_friends)
{
// Creates a new array with less data, more useful and less malicious
$new = array();
foreach ($selected_friends as $friend)
{
$f = array('id' => $friend['id'], 'name' => $friend['name']);
$new[] = $f;
}
return $new;
}
function GetThreeRandomFriends()
{
$friends = $this->_fb->api('/me/friends');
$n = rand(1, count($friends['data']) - 3);
$selected_friends = array_slice($friends['data'], $n, 3);
return $this->_rebuildSelectedFriends($selected_friends);
}
function UserExists($user_id)
{
try { $this->_fb->api('/' . $user_id . '/'); return true; }
catch (Exception $e) { return false; }
}
}
You must be calling the FbHelper class twice as your doLog function is in the constructor, therefore the repetition is somewhere higher up in your application and not in this class itself.

Categories