假设我有一个函数ssh_connect
以服务器的SSH设置为参数,现在我有多个服务器,我想随机选择其中一台服务器并将其作为参数输入到函数中。此外,如果使用一组参数时$ result为空,必须使用另一组
$result=ssh_connect($ip, $user, $pass, $port,$command)
假设我有以下详细信息
//Server 1
$ip="123.456.78.901";
$user="root"
$pass="mypassishere"
$port = "22"
//Server 2
$ip="225.456.98.901"
$user="root"
$pass="mypassishere"
$port = "22"
我可以使用选择结构,但该解决方案存在严重缺陷,因为一台服务器将始终优先$result
于其他服务器。这就像如果服务器1的详细信息为空时,请移至服务器2,这实际上不是我想要的。
我正在考虑数组,但是我不确定如何完成,因为我需要随机选择一组详细信息,如果一组失败,请尝试另一组,直到没有服务器或$ result有值为止。
@gwillie的回答,下面是一个示例:
<?php
$a = array(
array('host'=>'host1','user'=>'user1','pwd'=>'pwd1'),
array('host'=>'host2','user'=>'user2','pwd'=>'pwd2'),
array('host'=>'host3','user'=>'user3','pwd'=>'pwd3'),
array('host'=>'host4','user'=>'user4','pwd'=>'pwd4'),
array('host'=>'host5','user'=>'user5','pwd'=>'pwd5'),
);
$connect = false;
while (!$connect) {
$srvIndex = array_rand($a);
$host = $a[$srvIndex];
$connect = my_connect_function($host); //Return true on success, false on error
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句