PHP 使用 ppk、rsa、pkcs8 进行身份验证

PHP authentication with ppk, rsa, pkcs8

我是 PHP 的新手。我正在尝试 运行 一个 PHP 脚本(来自我的 windows 框),它只是通过 SSH 连接到一堆 linux 服务器(Net/SSH2.php) 在列表中使用,运行在每台服务器上执行一个 bash 命令,并将结果转储到一个文件中。在我的公司开始使用 rsa 密钥之前,它工作得很好。我现在有 3 个文件,一个 .pkcs8 (ENCRYPTED PRIVATE KEY) 文件,一个 .rsa (BEGIN RSA PRIVATE KEY) 文件和一个 .ppk(putty,在同一个文件中同时包含 public 和专用线路) .我能够将 .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 库,但我想我可能需要额外的库?

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

试试这个:

# 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($username, $key) 替换了你的 $ssh->login() 行。