PHP authentication with ppk, rsa, pkcs8

Viewed 424

I'm very new to PHP. I'm trying to run a PHP script (from my windows box) that simply SSH's into a bunch of linux servers (Net/SSH2.php) using in a list, runs one bash command on each server, and dumps the findings into a file. It worked perfectly before my company started using rsa keys. I now have 3 files, a .pkcs8 (ENCRYPTED PRIVATE KEY) file, a .rsa (BEGIN RSA PRIVATE KEY) file, and a .ppk (putty, which includes both public and private lines in the same file). I was able to use the .ppk with putty and authenticate by only providing my userID, so I'm sure its all possible. I just need to get it working in my PHP scripts.

# 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();
    }
  ?>

Doing some research I'm thinking I need to somehow call these keys via phpseclib, however the instructions were unclear, and I wasn't clear on what files I needed to call and how to use them in my code, as well as what I may need to remove from the code above now that I'd be authenticating a different way. I'm also locked down pretty hard by the company, so the more I can do without extra libraries the better. I did manage to get the RSA.php library on my laptop, but am thinking I may need additional libraries?

Thank you for any help you can give, and if not I'll keep plugging away at it and locking out my account ;)

1 Answers

Try this:

# 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();
    }
  ?>

I added include 'Crypt/RSA.php'; and replaced your $password = 'xxxx'; line with this:

    $key = new Crypt_RSA;
    //$key->setPassword('whatever');
    $key->loadKey(file_get_contents('/path/to/key'));

I then replaced your $ssh->login() line with $ssh->login($username, $key).

Related