Subversion Repositories qbpwcf-lib(archive)

Rev

Rev 922 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 liveuser 1
#!/usr/bin/php
2
<?php
3
 
464 liveuser 4
/*
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
620 liveuser 6
    Copyright (C) 2015~2024 Min-Jhin,Chen
464 liveuser 7
 
8
    This file is part of QBPWCF.
9
 
10
    QBPWCF is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
 
15
    QBPWCF is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
    GNU General Public License for more details.
19
 
20
    You should have received a copy of the GNU General Public License
21
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
22
 
23
*/
24
 
507 liveuser 25
/*
26
 
27
確認網路連線是否正常,若有必要會重新進行連線
28
 
29
*/
30
 
1 liveuser 31
#設置命名
32
namespace qbpwcf;
33
 
954 liveuser 34
#取得 lib path
35
exec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/libexec/folderOfUsrLib.php"),$output,$status);
36
 
37
#如果執行失敗
38
if($status!==0){
39
 
40
	#debug
41
	var_dump(__LINE__,$output);
42
 
43
	#結束執行,回傳shell 1.
44
	exit(1);
45
 
46
	}#if end
47
 
48
#儲存lib path
49
$folderOfUsrLib=$output[0];
50
 
466 liveuser 51
#以該檔案的實際位置的 lib path 為 include path 首位
954 liveuser 52
$output=array();
53
exec("cd ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../".$folderOfUsrLib."/qbpwcf").";pwd;",$output,$status);
54
 
55
#如果執行失敗
56
if($status!==0){
57
 
58
	#debug
59
	var_dump(__LINE__,$output);
60
 
61
	#結束執行,回傳shell 1.
62
	exit(1);
63
 
64
	}#if end
65
 
66
#設置 include path 
466 liveuser 67
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
68
 
464 liveuser 69
#匯入外部套件
466 liveuser 70
include("allInOne.php");
1 liveuser 71
 
72
#函式說明:
73
#抓取命令列的參數.
74
#回傳結果:
75
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
76
#$reuslt["error"],執行不正常結束的錯訊息陣列.
77
#$result["function"],當前執行的函式名稱.
78
#$result["argu"],使用的參數陣列.
79
#$result["content"],要回傳的參數陣列.
80
#$result["count"],參數的數量.
81
#必填參數:
82
#無
83
#可省略參數:
84
#$conf["echo"],"true"代表要將抓到的參數一個個印出來,"false"代表用回傳的方式,預設為"false".
85
#$conf["echo"]="false";
86
$conf=array();
87
$getArgu=cmd::getArgu($conf);
88
unset($conf);
89
 
90
#如果解析參數失敗
91
if($getArgu["status"]==="false"){
92
 
93
	#debug
94
	var_dump($getArgu);
95
 
96
	#stop
97
	exit;
98
 
99
	}#if end
100
 
101
/*
102
#debug
103
var_dump($getArgu);
104
exit;
105
*/
106
 
107
#如果存在第二個參數
108
if(isset($getArgu["content"][1])){
109
 
110
	#如果有help參數存在
111
	if( $getArgu["content"][1]==="--help" || $getArgu["content"][1]==="-h"){
112
 
113
		#說明用法
114
		help();
115
 
116
		}#if end
117
 
118
	}#if end
119
 
120
#函式說明:
121
#解析參數.
122
#回傳結果:
123
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
124
#$reuslt["error"],執行不正常結束的錯訊息陣列.
125
#$result["function"],當前執行的函式名稱.
126
#$result["content"],解析好的參數陣列.
127
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
128
#$result["program"],字串,執行的程式名稱.
129
#必填參數:
130
#無
131
#可省略參數:
132
#無
133
#備註:
134
#僅能在命令列底下執行.
135
#建議:
136
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
104 liveuser 137
$conf=array();
138
$parseArgu=cmd::parseArgu($conf);
139
unset($conf);
1 liveuser 140
 
141
#如果解析參數失敗
142
if($parseArgu["status"]==="false"){
143
 
144
	#debug
145
	#var_dump($parseArgu);
146
 
147
	#help
148
	help();
149
 
150
	#stop
151
	exit();
152
 
153
	}#if end
154
 
155
/*	
156
#debug
157
var_dump($parseArgu);
158
exit;
159
*/
160
 
161
#如果每沒有任何參數
162
if(!isset($parseArgu["content"])){
163
 
164
	#說明用法
165
	help();
166
 
167
	#stop
168
	exit;
169
 
170
	}#if end
171
 
172
/*
173
#debug
174
var_dump($parseArgu["content"]);
175
*/
176
 
177
#針對每個參數
178
foreach($parseArgu["content"] as $pN=>$pV){
179
 
180
	#/*
181
	#debug
182
	echo $pN.PHP_EOL;
183
	var_dump($pV);
184
	#exit;
185
	#*/
186
 
187
	#fix array to string error
188
	$pV=$pV[0];
189
 
190
	#判斷參數
191
	switch($pN){
192
 
193
		#如果是以連線名稱
194
		case "conn":
195
 
196
			#確認網路正常
197
			/*
198
			["DEVICE"]=>
199
			string(2) "--"
200
			["TYPE"]=>
201
			string(6) "bridge"
202
			["UUID"]=>
203
			string(36) "55f65234-ee44-4531-9c13-0ececc75e7cf"
204
			["NAME"]=>
205
			string(12) "dockerBridge" 
206
 
207
			$cdev=&$infos["DEVICE"];
208
			$ctype=&$infos["TYPE"];
209
			$cid=&$infos["UUID"];
210
			$cname=&$infos["NAME"];
211
 
212
			可省略參參數:
213
			$checkFrom,字串,可以為"cname","cid".
214
			*/
215
			var_dump(makeSureNetWorkFine("cname",$pV));		
216
 
217
			#跳出switch
218
			break;
219
 
220
		#如果是以連線uuid
221
		case "uuid":
222
 
223
			#確認網路正常
224
			/*
225
			["DEVICE"]=>
226
			string(2) "--"
227
			["TYPE"]=>
228
			string(6) "bridge"
229
			["UUID"]=>
230
			string(36) "55f65234-ee44-4531-9c13-0ececc75e7cf"
231
			["NAME"]=>
232
			string(12) "dockerBridge" 
233
 
234
			$cdev=&$infos["DEVICE"];
235
			$ctype=&$infos["TYPE"];
236
			$cid=&$infos["UUID"];
237
			$cname=&$infos["NAME"];
238
 
239
			可省略參參數:
240
			$checkFrom,字串,可以為"cname","cid".
241
			*/
495 liveuser 242
			var_dump(makeSureNetWorkFine("cid",$pV));
243
 
244
		#如果是以連線type
245
		case "type":
1 liveuser 246
 
495 liveuser 247
			#確認網路正常
248
			/*
249
			["DEVICE"]=>
250
			string(2) "--"
251
			["TYPE"]=>
252
			string(6) "bridge"
253
			["UUID"]=>
254
			string(36) "55f65234-ee44-4531-9c13-0ececc75e7cf"
255
			["NAME"]=>
256
			string(12) "dockerBridge" 
257
 
258
			$cdev=&$infos["DEVICE"];
259
			$ctype=&$infos["TYPE"];
260
			$cid=&$infos["UUID"];
261
			$cname=&$infos["NAME"];
262
 
263
			可省略參參數:
264
			$checkFrom,字串,可以為"cname","cid".
265
			*/
266
			var_dump(makeSureNetWorkFine("ctype",$pV));
267
 
1 liveuser 268
		default:
269
 
270
			#提示用法
271
			help();
272
 
273
			#stop
274
			exit;
275
 
276
		}#switch
277
 
278
	}#foreach end
279
 
280
#說明
281
function help(){
282
 
283
	#說明陣列
284
	$comments=array("Usage:");
285
	$comments[]="";
286
	$comments[]="help:";
287
	$comments[]=$_SERVER["PHP_SELF"]." --help";
288
	$comments[]=$_SERVER["PHP_SELF"]." -h";
289
	$comments[]="";
290
	$comments[]="keep net conn:";
495 liveuser 291
	$comments[]=$_SERVER["PHP_SELF"]." --conn [connect name]";
292
	$comments[]=$_SERVER["PHP_SELF"]." --uuid [connect UUID]";
293
	$comments[]=$_SERVER["PHP_SELF"]." --type [pppoe]";
1 liveuser 294
 
295
	#函式說明:
296
	#印出多行文字,結尾自動換行.
297
	#回傳的結果:
298
	#$result["status"],執行是否成功,"true"代表成功,"false"代表失敗.
299
	#$result["function"],當前執行的函數名稱.
300
	#$result["error"],錯誤訊息陣列.
301
	#必填的參數:
302
	#$conf["outputStringArray"],字串陣列,每行要印出的文字內容.
303
	$conf["outputStringArray"]=$comments;
304
	$echoMultiLine=cmd::echoMultiLine($conf);
305
	unset($conf);
306
 
307
	#如果執行失敗
308
	if($echoMultiLine["status"]==="false"){
309
 
310
		#debug
311
		var_dump($echoMultiLine);
312
 
313
		#stop
314
		exit;
315
 
316
		}#if end
317
 
318
	}#function help end
319
 
320
#確認網路正常
321
/*
322
["DEVICE"]=>
323
string(2) "--"
324
["TYPE"]=>
325
string(6) "bridge"
326
["UUID"]=>
327
string(36) "55f65234-ee44-4531-9c13-0ececc75e7cf"
328
["NAME"]=>
329
string(12) "dockerBridge" 
330
 
331
$cdev=&$infos["DEVICE"];
332
$ctype=&$infos["TYPE"];
333
$cid=&$infos["UUID"];
334
$cname=&$infos["NAME"];
335
$pV=查詢到的連線名稱或uuid變數的數值內容
336
 
337
可省略參參數:
495 liveuser 338
$checkFrom,字串,可以為"cname","cid","ctype"
1 liveuser 339
*/
340
function makeSureNetWorkFine($checkFrom="cname",$pV){
341
 
342
	#永久執行
343
	while(true){
344
 
345
		#睡覺30秒
346
		sleep(30);
347
 
348
		#函式說明:
349
		#fedora的nmcli指令,取得所有網路連線設定的資訊.
350
		#回傳結果:
351
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
352
		#$result["error"],錯誤訊息.
353
		#$result["function"],當前執行的函數名稱.
354
		#$result["argu"],使用的參數.
355
		#$result["content"],連線資訊.有DEVICE,TYPE,UUID,NAME.
356
		#必填參數
357
		#$conf["fileArgu"],字串,__FILE__的內容.
358
		$conf["fileArgu"]=__FILE__;
359
		#可省略參數:
360
		#無.
361
		#備註:
362
		#僅能在命令列環境下執行.
363
		$nmcli_list=cmd::nmcli_list($conf);
364
		unset($conf);
365
 
366
		#如果執行失敗
367
		if($nmcli_list["status"]==="false"){
368
 
369
			#debug
370
			var_dump($nmcli_list);
371
 
372
			#stop
373
			exit;
374
 
375
			}#if end
376
 
377
		#/*
378
		#debug
379
		var_dump($nmcli_list);
380
		#exit;
381
		#*/
382
 
383
		#根據每個網路連線設定檔名稱
384
		foreach($nmcli_list["content"] as $index=>$infos){
385
 
386
			#/*
387
			#debug
388
			echo "No:".$index.PHP_EOL;
389
			echo "Info:".PHP_EOL;
390
			var_dump($infos);
391
			#*/
392
 
393
			/*
394
			["DEVICE"]=>
395
			string(2) "--"
396
			["TYPE"]=>
397
			string(6) "bridge"
398
			["UUID"]=>
399
			string(36) "55f65234-ee44-4531-9c13-0ececc75e7cf"
400
			["NAME"]=>
401
			string(12) "dockerBridge" 
402
			*/
403
			$cdev=&$infos["DEVICE"];
404
			$ctype=&$infos["TYPE"];
405
			$cid=&$infos["UUID"];
406
			$cname=&$infos["NAME"];
407
 
408
			#/*
409
			#debug
410
			echo "connection name:".$$checkFrom.PHP_EOL;
411
			echo "currunet connection name:".$pV.PHP_EOL;
412
			#*/
413
 
414
			#如果是要找的連線設定檔
415
			if($$checkFrom===$pV){
416
 
417
				#/*
418
				#debug 
419
				echo "found connection ".$checkFrom.PHP_EOL;
420
				#*/
421
 
422
				#如果設備名稱不爲空,代表已經連線了
423
				if($cdev!=="--"){
424
 
425
					#/*
426
					#debug
427
					echo "connection is on".PHP_EOL;
428
					#*/
429
 
430
					#測試連線是否存活
431
					#涵式說明:
432
					#依據提供查詢IP服務的網站取得伺服器對外的IP.
433
					#回傳的結果:
434
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
435
					#$result["function"],當前執行的函數名稱.
436
					#$result["error"],錯誤訊息陣列.
437
					#$result["content"],伺服端對外的IP.
438
					#必填參數:
439
					#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
440
					$conf["fileArgu"]=__FILE__;
441
					$getServerRealIP=csInformation::getServerRealIP($conf);
442
					unset($conf);
443
 
444
					#debug
445
					echo "ping test info:".PHP_EOL;
446
					var_dump($getServerRealIP);
447
 
448
					#如果取得對外IP失敗
449
					if($getServerRealIP["status"]==="false"){
450
 
451
						#涵式說明:
452
						#透過 ping -c 3 -I [ppp0/網路界面名稱] 1.1.1.1 ,來判斷是否該界面有跟外部連線。
453
						#回傳結果:
454
						#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
455
						#$result["error"],錯誤訊息.
456
						#$result["function"],當前執行的函數名稱.
457
						#$result["argu"],所使用的參數.
458
						#$result["content"],執行的結果.
459
						#必填參數:
460
						#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
461
						$conf["fileArgu"]=__FILE__;
462
						#$conf["interface"],字串,網路界面名稱.
463
						$conf["interface"]=$cdev;
464
						#可省略參數:
465
						#$conf["pingTarget"],"字串",要ping的目標,預設為"1.1.1.1".
466
						#$conf["pingTarget"]="";
467
						#備註:
468
						#待測試
469
						$pingOutsideTest=cmd::pingOutsideTest($conf);
470
						unset($conf);
471
 
472
						#debug
473
						echo "ping test info:".PHP_EOL;
474
						var_dump($pingOutsideTest);
475
 
476
						#如果 ping 失敗
477
						if($pingOutsideTest["status"]==="false"){
478
 
479
							#代表連線是無效的應該要重新連線
480
							#函式說明:
481
							#fedora的nmcli指令,重新連線指定的連線。
482
							#回傳結果:
483
							#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
484
							#$result["error"],錯誤訊息.
485
							#$result["function"],當前執行的函數名稱.
486
							#$result["argu"],使用的參數.
487
							#$result["content"],連線資訊.有DEVICE,TYPE,UUID,NAME.
488
							#必填參數
489
							#$conf["fileArgu"],字串,__FILE__的內容.
490
							$conf["cmd::nmcli_reconnect"]["fileArgu"]=__FILE__;
491
							#$conf["name-uuid"],字串,"代表用來識別連線的名稱或UUID".
492
							$conf["cmd::nmcli_reconnect"]["name-uuid"]=$pV;
493
							#可省略參數:
494
							#無.
495
							#備註:
496
							#僅能在命令列環境下執行.
497
							$nmcli_reconnect=cmd::nmcli_reconnect($conf["cmd::nmcli_reconnect"]);
498
							unset($conf["cmd::nmcli_reconnect"]);
499
 
500
							#如果執行失敗
501
							if($nmcli_reconnect["status"]==="false"){
502
 
503
								#debug
504
								var_dump($nmcli_reconnect);
505
 
506
								#stop
507
								exit;
508
 
509
								}#if end
510
 
511
							}#if end
512
 
513
						}#if end
514
 
515
					#/*
516
					#debug
517
					echo "connection is truely on".PHP_EOL;
518
					#*/
519
 
520
					#確認連線是活的
521
					continue 2;
522
 
523
					}#if end
524
 
525
				#反之設備名稱爲空,代表沒有建立連線。
526
				else{
527
 
528
					#/*
529
					#debug
530
					echo "connection is off".PHP_EOL;
531
					#*/
532
 
533
					#建立連線
534
					#函式說明:
535
					#fedora的nmcli指令,重新連線指定的連線。
536
					#回傳結果:
537
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
538
					#$result["error"],錯誤訊息.
539
					#$result["function"],當前執行的函數名稱.
540
					#$result["argu"],使用的參數.
541
					#$result["content"],連線資訊.有DEVICE,TYPE,UUID,NAME.
542
					#必填參數
543
					#$conf["fileArgu"],字串,__FILE__的內容.
544
					$conf["cmd::nmcli_reconnect"]["fileArgu"]=__FILE__;
545
					#$conf["name-uuid"],字串,"代表用來識別連線的名稱或UUID".
546
					$conf["cmd::nmcli_reconnect"]["name-uuid"]=$pV;
547
					#可省略參數:
548
					#無.
549
					#備註:
550
					#僅能在命令列環境下執行.
551
					$nmcli_reconnect=cmd::nmcli_reconnect($conf["cmd::nmcli_reconnect"]);
552
					unset($conf["cmd::nmcli_reconnect"]);
553
 
554
					#/*
555
					#debug
556
					var_dump($nmcli_reconnect);
557
					#*/
558
 
559
					#如果執行失敗
560
					if($nmcli_reconnect["status"]==="false"){
561
 
562
						#debug
563
						var_dump($nmcli_reconnect);
564
 
565
						#stop
566
						exit;
567
 
568
						}#if end
569
 
570
 
571
					#確認連線是活的
572
					continue 2;
573
 
574
					}#else end
575
 
576
				#跳出 foreach
577
				break;
578
 
579
				}#if end
580
 
581
			}#foreach end
582
 
583
		}#while end	
584
 
585
	}#function makeSureNetWorkFine end
586
 
587
?>