【问题标题】:PHP authentication with ppk, rsa, pkcs8使用 ppk、rsa、pkcs8 进行 PHP 身份验证
【发布时间】:2020-07-20 06:51:56
【问题描述】:

我对 PHP 很陌生。我正在尝试运行一个 PHP 脚本(从我的 Windows 框中),该脚本只是在列表中使用 SSH 进入一堆 linux 服务器(Net/SSH2.php),在每台服务器上运行一个 bash 命令,并将结果转储到文件。在我的公司开始使用 rsa 密钥之前,它运行良好。我现在有 3 个文件,一个 .pkcs8(加密私钥)文件、一个 .rsa(BEGIN RSA 私钥)文件和一个 .ppk(腻子,在同一个文件中包含公共和私有行)。我能够将 .ppk 与 putty 一起使用并仅通过提供我的用户 ID 进行身份验证,因此我确信这一切都是可能的。我只需要让它在我的 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 库,但我认为我可能需要其他库?

感谢您提供的任何帮助,如果没有,我会继续努力并锁定我的帐户;)

【问题讨论】:

    标签: php ssh rsa phpseclib pkcs#8


    【解决方案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)

    【讨论】:

    • 你是一个绅士和一个学者。它像黄油一样光滑,我非常感谢你的帮助。说真的……谢谢。
    猜你喜欢
    • 2018-05-05
    • 2020-10-11
    • 2017-08-27
    • 2017-01-22
    • 2011-04-25
    • 2018-06-30
    • 2011-11-12
    • 2011-07-23
    • 1970-01-01
    相关资源
    最近更新 更多