Subversion Repositories qbpwcf-lib(archive)

Rev

Rev 464 | Rev 495 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/php
<?php

/*
        QBPWCF, Quick Build PHP website Component base on Fedora Linux.
    Copyright (C) 2015~2023 Min-Jhin,Chen

    This file is part of QBPWCF.

    QBPWCF is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QBPWCF is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
    
*/

#設置命名
namespace qbpwcf;

#以該檔案的實際位置的 lib path 為 include path 首位
exec("cd ".pathinfo(__FILE__)["dirname"]."/../../;pwd;",$output,$status);
set_include_path($output[0].PATH_SEPARATOR.get_include_path());

#匯入外部套件
include("allInOne.php");

#函式說明:
#抓取命令列的參數.
#回傳結果:
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
#$reuslt["error"],執行不正常結束的錯訊息陣列.
#$result["function"],當前執行的函式名稱.
#$result["argu"],使用的參數陣列.
#$result["content"],要回傳的參數陣列.
#$result["count"],參數的數量.
#必填參數:
#無
#可省略參數:
#$conf["echo"],"true"代表要將抓到的參數一個個印出來,"false"代表用回傳的方式,預設為"false".
#$conf["echo"]="false";
$conf=array();
$getArgu=cmd::getArgu($conf);
unset($conf);

#如果解析參數失敗
if($getArgu["status"]==="false"){

        #debug
        var_dump($getArgu);

        #stop
        exit;

        }#if end

/*
#debug
var_dump($getArgu);
exit;
*/

#如果存在第二個參數
if(isset($getArgu["content"][1])){

        #如果有help參數存在
        if( $getArgu["content"][1]==="--help" || $getArgu["content"][1]==="-h"){

                #說明用法
                help();

                }#if end

        }#if end
        
#函式說明:
#解析參數.
#回傳結果:
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
#$reuslt["error"],執行不正常結束的錯訊息陣列.
#$result["function"],當前執行的函式名稱.
#$result["content"],解析好的參數陣列.
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
#$result["program"],字串,執行的程式名稱.
#必填參數:
#無
#可省略參數:
#無
#備註:
#僅能在命令列底下執行.
#建議:
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
$conf=array();
$parseArgu=cmd::parseArgu($conf);
unset($conf);

#如果解析參數失敗
if($parseArgu["status"]==="false"){

        #debug
        #var_dump($parseArgu);

        #help
        help();

        #stop
        exit();

        }#if end

/*      
#debug
var_dump($parseArgu);
exit;
*/

#如果每沒有任何參數
if(!isset($parseArgu["content"])){

        #說明用法
        help();

        #stop
        exit;

        }#if end

/*
#debug
var_dump($parseArgu["content"]);
*/

#針對每個參數
foreach($parseArgu["content"] as $pN=>$pV){

        #/*
        #debug
        echo $pN.PHP_EOL;
        var_dump($pV);
        #exit;
        #*/

        #fix array to string error
        $pV=$pV[0];

        #判斷參數
        switch($pN){
        
                #如果是以連線名稱
                case "conn":
                
                        #確認網路正常
                        /*
                        ["DEVICE"]=>
                        string(2) "--"
                        ["TYPE"]=>
                        string(6) "bridge"
                        ["UUID"]=>
                        string(36) "55f65234-ee44-4531-9c13-0ececc75e7cf"
                        ["NAME"]=>
                        string(12) "dockerBridge" 

                        $cdev=&$infos["DEVICE"];
                        $ctype=&$infos["TYPE"];
                        $cid=&$infos["UUID"];
                        $cname=&$infos["NAME"];

                        可省略參參數:
                        $checkFrom,字串,可以為"cname","cid".
                        */
                        var_dump(makeSureNetWorkFine("cname",$pV));             
                                        
                        #跳出switch
                        break;
                
                #如果是以連線uuid
                case "uuid":
                
                        #確認網路正常
                        /*
                        ["DEVICE"]=>
                        string(2) "--"
                        ["TYPE"]=>
                        string(6) "bridge"
                        ["UUID"]=>
                        string(36) "55f65234-ee44-4531-9c13-0ececc75e7cf"
                        ["NAME"]=>
                        string(12) "dockerBridge" 

                        $cdev=&$infos["DEVICE"];
                        $ctype=&$infos["TYPE"];
                        $cid=&$infos["UUID"];
                        $cname=&$infos["NAME"];

                        可省略參參數:
                        $checkFrom,字串,可以為"cname","cid".
                        */
                        var_dump(makeSureNetWorkFine("cid",$pV));       
                
                default:
                        
                        #提示用法
                        help();
                        
                        #stop
                        exit;
        
                }#switch

        }#foreach end

#說明
function help(){
        
        #說明陣列
        $comments=array("Usage:");
        $comments[]="";
        $comments[]="help:";
        $comments[]=$_SERVER["PHP_SELF"]." --help";
        $comments[]=$_SERVER["PHP_SELF"]." -h";
        $comments[]="";
        $comments[]="keep net conn:";
        $comments[]=$_SERVER["PHP_SELF"]." --conn [connect name/UUID]";
        
        #函式說明:
        #印出多行文字,結尾自動換行.
        #回傳的結果:
        #$result["status"],執行是否成功,"true"代表成功,"false"代表失敗.
        #$result["function"],當前執行的函數名稱.
        #$result["error"],錯誤訊息陣列.
        #必填的參數:
        #$conf["outputStringArray"],字串陣列,每行要印出的文字內容.
        $conf["outputStringArray"]=$comments;
        $echoMultiLine=cmd::echoMultiLine($conf);
        unset($conf);

        #如果執行失敗
        if($echoMultiLine["status"]==="false"){
        
                #debug
                var_dump($echoMultiLine);
                
                #stop
                exit;
        
                }#if end
        
        }#function help end
        
#確認網路正常
/*
["DEVICE"]=>
string(2) "--"
["TYPE"]=>
string(6) "bridge"
["UUID"]=>
string(36) "55f65234-ee44-4531-9c13-0ececc75e7cf"
["NAME"]=>
string(12) "dockerBridge" 

$cdev=&$infos["DEVICE"];
$ctype=&$infos["TYPE"];
$cid=&$infos["UUID"];
$cname=&$infos["NAME"];
$pV=查詢到的連線名稱或uuid變數的數值內容

可省略參參數:
$checkFrom,字串,可以為"cname","cid".
*/
function makeSureNetWorkFine($checkFrom="cname",$pV){

        #永久執行
        while(true){

                #睡覺30秒
                sleep(30);

                #函式說明:
                #fedora的nmcli指令,取得所有網路連線設定的資訊.
                #回傳結果:
                #$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
                #$result["error"],錯誤訊息.
                #$result["function"],當前執行的函數名稱.
                #$result["argu"],使用的參數.
                #$result["content"],連線資訊.有DEVICE,TYPE,UUID,NAME.
                #必填參數
                #$conf["fileArgu"],字串,__FILE__的內容.
                $conf["fileArgu"]=__FILE__;
                #可省略參數:
                #無.
                #備註:
                #僅能在命令列環境下執行.
                $nmcli_list=cmd::nmcli_list($conf);
                unset($conf);

                #如果執行失敗
                if($nmcli_list["status"]==="false"){

                        #debug
                        var_dump($nmcli_list);

                        #stop
                        exit;

                        }#if end

                #/*
                #debug
                var_dump($nmcli_list);
                #exit;
                #*/
                
                #根據每個網路連線設定檔名稱
                foreach($nmcli_list["content"] as $index=>$infos){

                        #/*
                        #debug
                        echo "No:".$index.PHP_EOL;
                        echo "Info:".PHP_EOL;
                        var_dump($infos);
                        #*/

                        /*
                        ["DEVICE"]=>
                        string(2) "--"
                        ["TYPE"]=>
                        string(6) "bridge"
                        ["UUID"]=>
                        string(36) "55f65234-ee44-4531-9c13-0ececc75e7cf"
                        ["NAME"]=>
                        string(12) "dockerBridge" 
                        */
                        $cdev=&$infos["DEVICE"];
                        $ctype=&$infos["TYPE"];
                        $cid=&$infos["UUID"];
                        $cname=&$infos["NAME"];
                        
                        #/*
                        #debug
                        echo "connection name:".$$checkFrom.PHP_EOL;
                        echo "currunet connection name:".$pV.PHP_EOL;
                        #*/

                        #如果是要找的連線設定檔
                        if($$checkFrom===$pV){

                                #/*
                                #debug 
                                echo "found connection ".$checkFrom.PHP_EOL;
                                #*/

                                #如果設備名稱不爲空,代表已經連線了
                                if($cdev!=="--"){
                                        
                                        #/*
                                        #debug
                                        echo "connection is on".PHP_EOL;
                                        #*/
                                        
                                        #測試連線是否存活
                                        #涵式說明:
                                        #依據提供查詢IP服務的網站取得伺服器對外的IP.
                                        #回傳的結果:
                                        #$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
                                        #$result["function"],當前執行的函數名稱.
                                        #$result["error"],錯誤訊息陣列.
                                        #$result["content"],伺服端對外的IP.
                                        #必填參數:
                                        #$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
                                        $conf["fileArgu"]=__FILE__;
                                        $getServerRealIP=csInformation::getServerRealIP($conf);
                                        unset($conf);
                                        
                                        #debug
                                        echo "ping test info:".PHP_EOL;
                                        var_dump($getServerRealIP);
                                        
                                        #如果取得對外IP失敗
                                        if($getServerRealIP["status"]==="false"){
                                        
                                                #涵式說明:
                                                #透過 ping -c 3 -I [ppp0/網路界面名稱] 1.1.1.1 ,來判斷是否該界面有跟外部連線。
                                                #回傳結果:
                                                #$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
                                                #$result["error"],錯誤訊息.
                                                #$result["function"],當前執行的函數名稱.
                                                #$result["argu"],所使用的參數.
                                                #$result["content"],執行的結果.
                                                #必填參數:
                                                #$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
                                                $conf["fileArgu"]=__FILE__;
                                                #$conf["interface"],字串,網路界面名稱.
                                                $conf["interface"]=$cdev;
                                                #可省略參數:
                                                #$conf["pingTarget"],"字串",要ping的目標,預設為"1.1.1.1".
                                                #$conf["pingTarget"]="";
                                                #備註:
                                                #待測試
                                                $pingOutsideTest=cmd::pingOutsideTest($conf);
                                                unset($conf);
                                                
                                                #debug
                                                echo "ping test info:".PHP_EOL;
                                                var_dump($pingOutsideTest);
                                                
                                                #如果 ping 失敗
                                                if($pingOutsideTest["status"]==="false"){
                                                
                                                        #代表連線是無效的應該要重新連線
                                                        #函式說明:
                                                        #fedora的nmcli指令,重新連線指定的連線。
                                                        #回傳結果:
                                                        #$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
                                                        #$result["error"],錯誤訊息.
                                                        #$result["function"],當前執行的函數名稱.
                                                        #$result["argu"],使用的參數.
                                                        #$result["content"],連線資訊.有DEVICE,TYPE,UUID,NAME.
                                                        #必填參數
                                                        #$conf["fileArgu"],字串,__FILE__的內容.
                                                        $conf["cmd::nmcli_reconnect"]["fileArgu"]=__FILE__;
                                                        #$conf["name-uuid"],字串,"代表用來識別連線的名稱或UUID".
                                                        $conf["cmd::nmcli_reconnect"]["name-uuid"]=$pV;
                                                        #可省略參數:
                                                        #無.
                                                        #備註:
                                                        #僅能在命令列環境下執行.
                                                        $nmcli_reconnect=cmd::nmcli_reconnect($conf["cmd::nmcli_reconnect"]);
                                                        unset($conf["cmd::nmcli_reconnect"]);
                                                
                                                        #如果執行失敗
                                                        if($nmcli_reconnect["status"]==="false"){

                                                                #debug
                                                                var_dump($nmcli_reconnect);

                                                                #stop
                                                                exit;

                                                                }#if end
                                                
                                                        }#if end
                                                
                                                }#if end
                                        
                                        #/*
                                        #debug
                                        echo "connection is truely on".PHP_EOL;
                                        #*/

                                        #確認連線是活的
                                        continue 2;
                                        
                                        }#if end
                                
                                #反之設備名稱爲空,代表沒有建立連線。
                                else{
                                        
                                        #/*
                                        #debug
                                        echo "connection is off".PHP_EOL;
                                        #*/
                                        
                                        #建立連線
                                        #函式說明:
                                        #fedora的nmcli指令,重新連線指定的連線。
                                        #回傳結果:
                                        #$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
                                        #$result["error"],錯誤訊息.
                                        #$result["function"],當前執行的函數名稱.
                                        #$result["argu"],使用的參數.
                                        #$result["content"],連線資訊.有DEVICE,TYPE,UUID,NAME.
                                        #必填參數
                                        #$conf["fileArgu"],字串,__FILE__的內容.
                                        $conf["cmd::nmcli_reconnect"]["fileArgu"]=__FILE__;
                                        #$conf["name-uuid"],字串,"代表用來識別連線的名稱或UUID".
                                        $conf["cmd::nmcli_reconnect"]["name-uuid"]=$pV;
                                        #可省略參數:
                                        #無.
                                        #備註:
                                        #僅能在命令列環境下執行.
                                        $nmcli_reconnect=cmd::nmcli_reconnect($conf["cmd::nmcli_reconnect"]);
                                        unset($conf["cmd::nmcli_reconnect"]);

                                        #/*
                                        #debug
                                        var_dump($nmcli_reconnect);
                                        #*/

                                        #如果執行失敗
                                        if($nmcli_reconnect["status"]==="false"){

                                                #debug
                                                var_dump($nmcli_reconnect);

                                                #stop
                                                exit;

                                                }#if end
                                        
                                        
                                        #確認連線是活的
                                        continue 2;
                                        
                                        }#else end

                                #跳出 foreach
                                break;

                                }#if end
        
                        }#foreach end

                }#while end     

        }#function makeSureNetWorkFine end

?>