243 lines
7.1 KiB
PHP
243 lines
7.1 KiB
PHP
<?php
|
|
/**
|
|
* Plesk Subscription Retriever
|
|
*
|
|
* Connects to multiple Plesk servers via SSH and retrieves subscription information.
|
|
* Outputs results to a local JSON file.
|
|
*/
|
|
|
|
// Server configuration
|
|
$servers = [
|
|
'Blue' => [
|
|
'host' => 'blue.example.com',
|
|
'port' => 22,
|
|
'username' => 'root',
|
|
// Use password authentication
|
|
'password' => 'your_password_here',
|
|
// OR use private key authentication (uncomment and configure)
|
|
// 'pubkey_file' => '/path/to/id_rsa.pub',
|
|
// 'privkey_file' => '/path/to/id_rsa',
|
|
],
|
|
'Red' => [
|
|
'host' => 'red.example.com',
|
|
'port' => 22,
|
|
'username' => 'root',
|
|
'password' => 'your_password_here',
|
|
],
|
|
'Purple' => [
|
|
'host' => 'purple.example.com',
|
|
'port' => 22,
|
|
'username' => 'root',
|
|
'password' => 'your_password_here',
|
|
],
|
|
'Orange' => [
|
|
'host' => 'orange.example.com',
|
|
'port' => 22,
|
|
'username' => 'root',
|
|
'password' => 'your_password_here',
|
|
],
|
|
];
|
|
|
|
// Output file
|
|
$outputFile = __DIR__ . '/subscriptions.json';
|
|
|
|
/**
|
|
* Connect to a Plesk server via SSH and retrieve subscriptions
|
|
*/
|
|
function getSubscriptionsFromServer(string $name, array $config): array
|
|
{
|
|
$result = [
|
|
'server_name' => $name,
|
|
'host' => $config['host'],
|
|
'status' => 'unknown',
|
|
'error' => null,
|
|
'subscriptions' => [],
|
|
];
|
|
|
|
// Check if SSH2 extension is available
|
|
if (!extension_loaded('ssh2')) {
|
|
$result['status'] = 'error';
|
|
$result['error'] = 'SSH2 PHP extension not installed';
|
|
return $result;
|
|
}
|
|
|
|
try {
|
|
// Establish SSH connection
|
|
$connection = @ssh2_connect($config['host'], $config['port']);
|
|
|
|
if (!$connection) {
|
|
$result['status'] = 'error';
|
|
$result['error'] = 'Failed to connect to server';
|
|
return $result;
|
|
}
|
|
|
|
// Authenticate
|
|
if (isset($config['pubkey_file']) && isset($config['privkey_file'])) {
|
|
$authenticated = @ssh2_auth_pubkey_file(
|
|
$connection,
|
|
$config['username'],
|
|
$config['pubkey_file'],
|
|
$config['privkey_file']
|
|
);
|
|
} else {
|
|
$authenticated = @ssh2_auth_password($connection, $config['username'], $config['password']);
|
|
}
|
|
|
|
if (!$authenticated) {
|
|
$result['status'] = 'error';
|
|
$result['error'] = 'Authentication failed';
|
|
return $result;
|
|
}
|
|
|
|
$result['status'] = 'connected';
|
|
|
|
// Execute Plesk CLI command to list all subscriptions
|
|
$stream = @ssh2_exec($connection, '/usr/local/psa/bin/domain --list');
|
|
|
|
if (!$stream) {
|
|
$result['status'] = 'error';
|
|
$result['error'] = 'Failed to execute Plesk command';
|
|
return $result;
|
|
}
|
|
|
|
stream_set_blocking($stream, true);
|
|
$output = stream_get_contents($stream);
|
|
fclose($stream);
|
|
|
|
if (empty($output)) {
|
|
$result['status'] = 'error';
|
|
$result['error'] = 'No output from Plesk command';
|
|
return $result;
|
|
}
|
|
|
|
$result['status'] = 'success';
|
|
|
|
// Parse the domain list output
|
|
$domains = explode("\n", trim($output));
|
|
|
|
foreach ($domains as $domainLine) {
|
|
$domainLine = trim($domainLine);
|
|
if (empty($domainLine)) {
|
|
continue;
|
|
}
|
|
|
|
$subscription = parseDomainLine($domainLine, $connection, $config['username']);
|
|
if ($subscription) {
|
|
$result['subscriptions'][] = $subscription;
|
|
}
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
$result['status'] = 'error';
|
|
$result['error'] = $e->getMessage();
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Parse a single domain line and retrieve additional details
|
|
*/
|
|
function parseDomainLine(string $line, $connection, string $username): ?array
|
|
{
|
|
// Expected format: domain-name.tld (or with additional info)
|
|
$domainName = trim($line);
|
|
|
|
if (empty($domainName)) {
|
|
return null;
|
|
}
|
|
|
|
$subscription = [
|
|
'primary_domain' => $domainName,
|
|
'additional_domains' => [],
|
|
'aliases' => [],
|
|
];
|
|
|
|
// Get additional domains for this subscription
|
|
$additionalCmd = sprintf(
|
|
'/usr/local/psa/bin/domain --info %s --format=json',
|
|
escapeshellarg($domainName)
|
|
);
|
|
|
|
$stream = @ssh2_exec($connection, $additionalCmd);
|
|
if ($stream) {
|
|
stream_set_blocking($stream, true);
|
|
$infoOutput = stream_get_contents($stream);
|
|
fclose($stream);
|
|
|
|
$infoData = json_decode($infoOutput, true);
|
|
if ($infoData) {
|
|
// Extract additional domains if available in the JSON output
|
|
if (isset($infoData['additionalDomains']) && is_array($infoData['additionalDomains'])) {
|
|
$subscription['additional_domains'] = $infoData['additionalDomains'];
|
|
}
|
|
if (isset($infoData['aliases']) && is_array($infoData['aliases'])) {
|
|
$subscription['aliases'] = $infoData['aliases'];
|
|
}
|
|
}
|
|
}
|
|
|
|
// Alternative: Get domain aliases using Plesk CLI
|
|
$aliasCmd = sprintf(
|
|
'/usr/local/psa/bin/domain_alias --list %s',
|
|
escapeshellarg($domainName)
|
|
);
|
|
|
|
$stream = @ssh2_exec($connection, $aliasCmd);
|
|
if ($stream) {
|
|
stream_set_blocking($stream, true);
|
|
$aliasOutput = stream_get_contents($stream);
|
|
fclose($stream);
|
|
|
|
$aliases = explode("\n", trim($aliasOutput));
|
|
$subscription['aliases'] = array_filter(array_map('trim', $aliases));
|
|
}
|
|
|
|
return $subscription;
|
|
}
|
|
|
|
/**
|
|
* Main execution
|
|
*/
|
|
echo "Plesk Subscription Retriever\n";
|
|
echo "============================\n\n";
|
|
|
|
$allResults = [
|
|
'generated_at' => date('c'),
|
|
'servers' => [],
|
|
'summary' => [
|
|
'total_servers' => count($servers),
|
|
'successful' => 0,
|
|
'failed' => 0,
|
|
'total_subscriptions' => 0,
|
|
],
|
|
];
|
|
|
|
foreach ($servers as $serverName => $config) {
|
|
echo "Connecting to {$serverName} ({$config['host']})... ";
|
|
|
|
$serverData = getSubscriptionsFromServer($serverName, $config);
|
|
$allResults['servers'][] = $serverData;
|
|
|
|
if ($serverData['status'] === 'success') {
|
|
$count = count($serverData['subscriptions']);
|
|
echo "OK ({$count} subscriptions)\n";
|
|
$allResults['summary']['successful']++;
|
|
$allResults['summary']['total_subscriptions'] += $count;
|
|
} else {
|
|
echo "FAILED: {$serverData['error']}\n";
|
|
$allResults['summary']['failed']++;
|
|
}
|
|
}
|
|
|
|
// Save to JSON file
|
|
$jsonOutput = json_encode($allResults, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
|
file_put_contents($outputFile, $jsonOutput);
|
|
|
|
echo "\n";
|
|
echo "Summary:\n";
|
|
echo " Successful: {$allResults['summary']['successful']}\n";
|
|
echo " Failed: {$allResults['summary']['failed']}\n";
|
|
echo " Total Subscriptions: {$allResults['summary']['total_subscriptions']}\n";
|
|
echo "\nResults saved to: {$outputFile}\n";
|