运行 多个 PHP SSH2 脚本
Running Multiple PHP SSH2 Script
我的商店有大约 10 台 Mac。
目前,我可以使用此代码
通过 PHP SSH2 功能远程 shutdown/restart 他们
<?php
include('Net/SSH2.php');
$server = "hostname";
$username = "user";
$password = "pwd";
$command = "sudo shutdown -r now";
$ssh = new Net_SSH2($server);
if (!$ssh->login($username, $password)) {
exit('Login Failed');
}
echo $ssh->exec($command);
echo "Sucessfully Restarted blah blah blah";
?>
但是为了 shutdown/restart 10 个终端,我必须 运行 10 个不同的脚本来实现。有什么方法可以连接到多个服务器并 运行 相同的命令吗?
您可以将主机名和凭据存储在 multi-dimensional array. This will allow you to iterate through each item using foreach 中并在每个主机上执行所需的命令。以下是您需要执行的操作示例:
<?php
include('Net/SSH2.php');
$hosts = array(
array(
'hostname' => 'hostname1',
'username' => 'user1',
'password' => 'pwd1'
),
array(
'hostname' => 'hostname2',
'username' => 'user2',
'password' => 'pwd2'
)
);
$command = "sudo shutdown -r now";
foreach ($hosts as $host) {
$ssh = new Net_SSH2($host['hostname']);
if (!$ssh->login($host['username'], $host['password'])) {
echo "Login Failed for host '{$host['hostname']}'\n";
continue;
}
echo $ssh->exec($command);
echo "Sucessfully Restarted {$host['hostname']}\n";
}
希望对您有所帮助。
关于安全:建议您使用 SSH 密钥而不是用户名和密码。另外,请确保将 mac 保持在未开放的网络上,例如,对客户,您应该使用专用网络。
经过反复试验并尝试理解代码。我对您的代码稍加修改就解决了这个问题。在每个数组中又添加了一个 "name"。然后,删除 echo,并用一个变量替换它。所以每当我想获得状态时,我都可以调用值。
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include('Net/SSH2.php');
$hosts = array(
array(
'hostname' => 'hostname1',
'username' => 'user',
'password' => 'pwd',
'name' => '1sthost'
),
array(
'hostname' => 'hostname2',
'username' => 'user',
'password' => 'pwd',
'name' => '2ndhost'
)
);
$command = "shutdown -r now";
foreach ($hosts as $host) {
$ssh = new Net_SSH2($host['hostname']);
if (!$ssh->login($host['username'], $host['password'])) {
$$host['name'] = "{$host['hostname']}\n failed";
continue;
}
echo $ssh->exec($command);
$$host['name'] = "{$host['hostname']}\n success";
}
?>
<?php echo sthost; ?>
<?php echo ndhost; ?>
我的商店有大约 10 台 Mac。 目前,我可以使用此代码
通过 PHP SSH2 功能远程 shutdown/restart 他们<?php
include('Net/SSH2.php');
$server = "hostname";
$username = "user";
$password = "pwd";
$command = "sudo shutdown -r now";
$ssh = new Net_SSH2($server);
if (!$ssh->login($username, $password)) {
exit('Login Failed');
}
echo $ssh->exec($command);
echo "Sucessfully Restarted blah blah blah";
?>
但是为了 shutdown/restart 10 个终端,我必须 运行 10 个不同的脚本来实现。有什么方法可以连接到多个服务器并 运行 相同的命令吗?
您可以将主机名和凭据存储在 multi-dimensional array. This will allow you to iterate through each item using foreach 中并在每个主机上执行所需的命令。以下是您需要执行的操作示例:
<?php
include('Net/SSH2.php');
$hosts = array(
array(
'hostname' => 'hostname1',
'username' => 'user1',
'password' => 'pwd1'
),
array(
'hostname' => 'hostname2',
'username' => 'user2',
'password' => 'pwd2'
)
);
$command = "sudo shutdown -r now";
foreach ($hosts as $host) {
$ssh = new Net_SSH2($host['hostname']);
if (!$ssh->login($host['username'], $host['password'])) {
echo "Login Failed for host '{$host['hostname']}'\n";
continue;
}
echo $ssh->exec($command);
echo "Sucessfully Restarted {$host['hostname']}\n";
}
希望对您有所帮助。
关于安全:建议您使用 SSH 密钥而不是用户名和密码。另外,请确保将 mac 保持在未开放的网络上,例如,对客户,您应该使用专用网络。
经过反复试验并尝试理解代码。我对您的代码稍加修改就解决了这个问题。在每个数组中又添加了一个 "name"。然后,删除 echo,并用一个变量替换它。所以每当我想获得状态时,我都可以调用值。
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include('Net/SSH2.php');
$hosts = array(
array(
'hostname' => 'hostname1',
'username' => 'user',
'password' => 'pwd',
'name' => '1sthost'
),
array(
'hostname' => 'hostname2',
'username' => 'user',
'password' => 'pwd',
'name' => '2ndhost'
)
);
$command = "shutdown -r now";
foreach ($hosts as $host) {
$ssh = new Net_SSH2($host['hostname']);
if (!$ssh->login($host['username'], $host['password'])) {
$$host['name'] = "{$host['hostname']}\n failed";
continue;
}
echo $ssh->exec($command);
$$host['name'] = "{$host['hostname']}\n success";
}
?>
<?php echo sthost; ?>
<?php echo ndhost; ?>