Jump to content

Colorized output of bash script in php


tRolex

Recommended Posts

I'm trying to make colourized output of bash script in php. So far I have this:

<?php
$cmd = '/home/thebalk/FiveM/manage.sh restart';
while (@ ob_end_flush()); // end all output buffers if any

$proc = popen($cmd, 'r');
echo '<pre>';
while (!feof($proc))
{
    echo fread($proc, 4096);
    @ flush();
}
echo '</pre>';

//
// Converts Bashoutput to colored HTML
//
function convertBash($cmd) {
    $dictionary = array(
        '[1;30m' => '<span style="color:black">',
        '[1;31m' => '<span style="color:red">', 
        '[1;32m' => '<span style="color:green">',   
        '[1;33m' => '<span style="color:yellow">',
        '[1;34m' => '<span style="color:blue">',
        '[1;35m' => '<span style="color:purple">',
        '[1;36m' => '<span style="color:cyan">',
        '[1;37m' => '<span style="color:white">',
        '[m'   => '</span>'
    );
    $htmlString = str_replace(array_keys($dictionary), $dictionary, $cmd);
    return $htmlString;
}
?>

and my output looks like this:

[1;36m BOT:[0;39m THE [1;32m TheBalkanRP-SRV01-test SERVER [0;39m WAS RUNNING [1;36m BOT:[0;39m STOPPING THE [1;32m TheBalkanRP-SRV01-test SERVER [1;36m BOT:[0;39m GTA V SERVER HAS STOPPED [1;36m BOT:[0;39m REMOVING CACHE FOLDER [1;36m BOT:[1;31m CACHE FOLDER HAS BEEN REMOVED [0;39m [1;36m BOT:[0;39m STARTING THE [1;32m TheBalkanRP-SRV01-test [0;39m SERVER [1;36m BOT:[1;32m SERVER HAS STARTED [0;39m

I know my function is not connected with the code above but what is the right way to do it? Thank you!

Link to comment
Share on other sites

20 minutes ago, requinix said:

You're missing that the escape sequence starts with a \033 (aka \e). And that's not a literal backslash, so if you add it into your $dictionary then you'll need to switch to double-quoted strings too.

I give up :) I need some working example 1st to get this understand better.

Edited by tRolex
Link to comment
Share on other sites

12 minutes ago, requinix said:

Black is


\e[1;30m

where \e is a control character, like \n, and not literally a backslash + 'e'.

ok now i get it  so i have to change this in bash script: WIT="\033[0;39m" ROOD="\033[1;31m" GROEN="\033[1;32m" but what about the part of code to use this function with the code above? Thank you for your help!

Edited by tRolex
Link to comment
Share on other sites

First, you need-- actually no, it can wait until you discover it for yourself.

convertBash() takes a string line of output as an argument and returns a new string with HTML markup. fread() reads from the process and returns a string line of output.

So instead of echoing the line of output, you run that line through convertBash and echo what it returns.

Link to comment
Share on other sites

34 minutes ago, requinix said:

First, you need-- actually no, it can wait until you discover it for yourself.

convertBash() takes a string line of output as an argument and returns a new string with HTML markup. fread() reads from the process and returns a string line of output.

So instead of echoing the line of output, you run that line through convertBash and echo what it returns.

I understand but cant make it right.. 

Link to comment
Share on other sites

I would like to make a script where admins can "start, restart" a game server "FiveM" this is the link of the script above: http://www.thebalkanrp.tk/start.php <-- 

My bash "start,restart" script looks like this:

#!/bin/bash

WIT="\033[0;39m"
ROOD="\033[1;31m"
GROEN="\033[1;32m"
GEEL="\033[1;33m"
PAARS="\033[1;35m"

LOL="\033[1;36m"


MSG_180="Une temp.te approche, dans 3 minutes la ville sera rasé !"
MSG_60="Une temp.te est aux portes de la ville, fuyez pauvres fous !"
MSG_30="Mon dieu !! Dans 30 secondes vous serez tous morts si vous ne fuyez pas !"

FIVEM_PATH=/home/thebalk/FiveM
SCREEN="TheBalkanRP-SRV01-test"

cd $FIVEM_PATH    
running(){
    if ! screen -list | grep -q "$SCREEN"
    then
        return 1
    else
        return 0
    fi
}    

case "$1" in
    # -----------------[ Start ]----------------- #
    start)
    if ( running )
    then
        echo -e ""
        echo -e "$LOL BOT:$GROEN $SCREEN $ROOD SERVER IS ALREADY RUNNING!! $WIT"
        echo -e ""
        echo -e "$LOL BOT:$PAARS STOP THE SERVER FIRST: $GEEL./manage.sh stop $WIT"
        echo -e "$LOL BOT:$WIT OR"
        echo -e "$LOL BOT:$PAARS RESTART SERVER:        $GEEL./manage.sh restart $WIT"
        sleep 2
    else
        echo -e ""
        echo -e "$LOL BOT:$WIT STARTING THE GTA V SERVER "
        screen -dm -S $SCREEN
        sleep 2
        screen -x $SCREEN -X stuff "cd /home/thebalk/FiveM/server-data && bash /home/thebalk/FiveM/run.sh +exec server.cfg
        "
        sleep 20
        screen -x $SCREEN -X stuff "restart sessionmanager
        "
        echo -e "$LOL BOT:$GROEN SERVER HAS STARTED $WIT"
        sleep 2
    fi
    ;;
    # -----------------[ Stop ]------------------ #
    stop)
    if ( running )
    then
        echo -e ""
        echo -e "$LOL BOT:$WIT STOPPING THE GTA V SERVER"
        screen -S $SCREEN -p 0 -X stuff "`printf "say $MSG_30\r"`";sleep 10
        screen -S $SCREEN -X quit
        sleep 5
        echo -e "$LOL BOT:$WIT THE $GROEN $SCREEN $WIT SERVER HAS STOPPED"
        sleep 5
        echo -e "$LOL BOT:$WIT REMOVING CACHE FOLDER"
        rm -R /home/thebalk/FiveM/server-data/cache/
        sleep 5
        echo -e "$LOL BOT:$ROOD CACHE FOLDER HAS BEEN REMOVED $WIT"
        sleep 5
        echo -e "$LOL BOT:$ROOD THE $GROEN $SCREEN SERVER $ROOD HAS STOPPED $WIT"
        sleep 2
    else
        echo -e ""
        echo -e "$LOL BOT:$ROOD THE $GROEN $SCREEN SERVER $ROOD IS NOT RUNNING!!! $WIT"
        echo -e ""
        echo -e "$LOL BOT:$PAARS USE THE COMMAND: $GEEL./manage.sh start $WIT"
        sleep 2
    fi
    ;;
    # ----------------[ Restart ]---------------- #
    restart)
    if ( running )
    then
        echo -e ""
        echo -e "$LOL BOT:$WIT THE $GROEN $SCREEN SERVER $WIT WAS RUNNING"
        sleep 5 
        echo -e "$LOL BOT:$WIT STOPPING THE $GROEN $SCREEN SERVER"
        screen -S $SCREEN -p 0 -X stuff "`printf "say $MSG_30\r"`"; sleep 10
        screen -S $SCREEN -X quit
        sleep 5
        echo -e "$LOL BOT:$WIT GTA V SERVER HAS STOPPED "
        sleep 5  
        echo -e "$LOL BOT:$WIT REMOVING CACHE FOLDER"
        rm -R /home/thebalk/FiveM/server-data/cache/
        sleep 5
        echo -e "$LOL BOT:$ROOD CACHE FOLDER HAS BEEN REMOVED $WIT"
        sleep 5
        echo -e "$LOL BOT:$WIT STARTING THE $GROEN $SCREEN $WIT SERVER"
        screen -dm -S $SCREEN
        sleep 5
        screen -x $SCREEN -X stuff "cd /home/thebalk/FiveM/server-data && bash /home/thebalk/FiveM/run.sh +exec server.cfg
        "
        sleep 20
        screen -x $SCREEN -X stuff "restart sessionmanager
        "
    else
        echo -e ""
        echo -e "$LOL BOT:$WIT THE $GROEN $SCREEN SERVER $WIT IS NOT RUNNING"
        sleep 5
        echo -e "$LOL BOT:$WIT STARTING THE $GROEN $SCREEN $WIT SERVER "
        screen -dm -S $SCREEN
        sleep 5
        screen -x $SCREEN -X stuff "cd /home/fivem/FiveM/server-data && bash /home/fivem/FiveM/run.sh +exec server.cfg
        "
        sleep 20
        screen -x $SCREEN -X stuff "restart sessionmanager
        "
    fi
        echo -e "$LOL BOT:$GROEN SERVER HAS STARTED $WIT"
        sleep 2
    ;;    
    # -----------------[ Status ]---------------- #
    status)
    if ( running )
    then
        echo -e "$GROEN THE $LOL $SCREEN $GROEN SERVER IS RUNNING $WIT"
    else
        echo -e "$ROOD THE $LOL $SCREEN $ROOD SERVER IS NOT RUNNING $WIT"
    fi
    ;;
    # -----------------[ Screen ]---------------- #
    screen)
        echo -e "$GROEN Server screen [$SCREEN]. $WIT"
        screen -R $SCREEN
    ;;
    *)
    echo -e "$GEEL Use: $WIT ./manage.sh {start|stop|status|screen|restart}"
    exit 1
    ;;
esac

exit 0


And I would like colourized output of the script when I run http://www.thebalkanrp.tk/start.php 

start.php should look something like:) 

<?php
$cmd = '/home/thebalk/FiveM/manage.sh restart';
while (@ ob_end_flush()); // end all output buffers if any

$proc = popen($cmd, 'r');
echo '<pre>';
while (!feof($proc))
{
    echo fread($proc, 4096);
    @ flush();
}
echo '</pre>';

//
// Converts Bashoutput to colored HTML
//
function convertBash($cmd) {
    $dictionary = array(
        '\e[1;30m' => '<span style="color:black">',
        '\e[1;31m' => '<span style="color:red">', 
        '\e[1;32m' => '<span style="color:green">',   
        '\e[1;33m' => '<span style="color:yellow">',
        '\e[1;34m' => '<span style="color:blue">',
        '\e[1;35m' => '<span style="color:purple">',
        '\e[1;36m' => '<span style="color:cyan">',
        '\e[1;37m' => '<span style="color:white">'
    );
    $htmlString = str_replace(array_keys($dictionary), $dictionary, $cmd);
    return $htmlString;
}
?>

 

Edited by tRolex
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.