Subversion Repositories qbpwcf-lib(archive)

Rev

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