我是PHP的新手。我正在尝试运行一个PHP脚本(从我的Windows框中),该脚本简单地将SSH放入列表中使用的一堆linux服务器(Net / SSH2.php)中,在每台服务器上运行一个bash命令,并将结果转储到一个文件。在我的公司开始使用rsa密钥之前,它运行良好。我现在有3个文件,.pkcs8(加密的私钥)文件,.rsa(BEGIN RSA私钥)文件和.ppk(putty,在同一文件中包括公用和专用行)。通过仅提供我的userID,我就可以在腻子中使用.ppk并进行身份验证,因此,我相信这一切都是可能的。我只需要使其在我的PHP脚本中运行即可。
# multiple server file query script
# define username and password
$username = "1234567";
$password = "xxxxxxx";
# create variables and array for reading servers from txt document
# then exploding the contents of the txt file into a variable
$text_file_contents = file_get_contents ("serverlist.txt");
$server_array = explode("\r\n", $text_file_contents);
# using the SSH2 tie-in for PHP
include 'Net/SSH2.php';
foreach($server_array as $server) {
$ssh = new Net_SSH2($server);
if (!$ssh->login($username, $password)) {
echo "\r\n";
echo "------------------------------------";
echo "\r\n";
} else {
$cmd = $ssh->exec('BASH_COMMAND_HERE');
echo "$server";
echo "\r\n";
echo "\r\n";
echo "$cmd";
echo "\r\n";
echo "------------------------------------";
echo "\r\n";
}
$ssh->disconnect();
}
?>
做一些研究,我认为我需要通过phpseclib调用这些键,但是说明不清楚,我也不清楚我需要调用哪些文件以及如何在代码中使用它们。由于我现在需要通过其他方式进行身份验证,因此可能需要从上面的代码中删除这些内容。我也很努力地被公司锁定,因此如果没有额外的库,我可以做的越多越好。我确实设法在笔记本电脑上获得了RSA.php库,但是我想可能需要其他库吗?
谢谢您能提供的任何帮助,否则,我会继续封锁它并锁定我的帐户;)
答案 0 :(得分:1)
尝试一下:
# multiple server file query script
include 'Crypt/RSA.php';
# define username and password
$username = "1234567";
$key = new Crypt_RSA;
//$key->setPassword('whatever');
$key->loadKey(file_get_contents('/path/to/key'));
# create variables and array for reading servers from txt document
# then exploding the contents of the txt file into a variable
$text_file_contents = file_get_contents ("serverlist.txt");
$server_array = explode("\r\n", $text_file_contents);
# using the SSH2 tie-in for PHP
include 'Net/SSH2.php';
foreach($server_array as $server) {
$ssh = new Net_SSH2($server);
if (!$ssh->login($username, $key)) {
echo "\r\n";
echo "------------------------------------";
echo "\r\n";
} else {
$cmd = $ssh->exec('BASH_COMMAND_HERE');
echo "$server";
echo "\r\n";
echo "\r\n";
echo "$cmd";
echo "\r\n";
echo "------------------------------------";
echo "\r\n";
}
$ssh->disconnect();
}
?>
我添加了include 'Crypt/RSA.php';
,并用以下内容替换了您的$password = 'xxxx';
行:
$key = new Crypt_RSA;
//$key->setPassword('whatever');
$key->loadKey(file_get_contents('/path/to/key'));
然后我将您的$ssh->login()
行替换为$ssh->login($username, $key)
。