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,ChenThis file is part of QBPWCF.QBPWCF is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe 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 ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong 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"){#debugvar_dump($getArgu);#stopexit;}#if end/*#debugvar_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);#helphelp();#stopexit();}#if end/*#debugvar_dump($parseArgu);exit;*/#如果每沒有任何參數if(!isset($parseArgu["content"])){#說明用法help();#stopexit;}#if end/*#debugvar_dump($parseArgu["content"]);*/#針對每個參數foreach($parseArgu["content"] as $pN=>$pV){#/*#debugecho $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));#跳出switchbreak;#如果是以連線uuidcase "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();#stopexit;}#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"){#debugvar_dump($echoMultiLine);#stopexit;}#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"){#debugvar_dump($nmcli_list);#stopexit;}#if end#/*#debugvar_dump($nmcli_list);#exit;#*/#根據每個網路連線設定檔名稱foreach($nmcli_list["content"] as $index=>$infos){#/*#debugecho "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"];#/*#debugecho "connection name:".$$checkFrom.PHP_EOL;echo "currunet connection name:".$pV.PHP_EOL;#*/#如果是要找的連線設定檔if($$checkFrom===$pV){#/*#debugecho "found connection ".$checkFrom.PHP_EOL;#*/#如果設備名稱不爲空,代表已經連線了if($cdev!=="--"){#/*#debugecho "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);#debugecho "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);#debugecho "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"){#debugvar_dump($nmcli_reconnect);#stopexit;}#if end}#if end}#if end#/*#debugecho "connection is truely on".PHP_EOL;#*/#確認連線是活的continue 2;}#if end#反之設備名稱爲空,代表沒有建立連線。else{#/*#debugecho "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"]);#/*#debugvar_dump($nmcli_reconnect);#*/#如果執行失敗if($nmcli_reconnect["status"]==="false"){#debugvar_dump($nmcli_reconnect);#stopexit;}#if end#確認連線是活的continue 2;}#else end#跳出 foreachbreak;}#if end}#foreach end}#while end}#function makeSureNetWorkFine end?>