-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample.php
66 lines (51 loc) · 1.76 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* Find installed package list of each server
*/
$servers = array('user1@host1','user2@host2','user3@host3');
$transport = '/usr/bin/ssh';
$command = 'rpm -qa --qf "\"%{NAME}\":\"%{VERSION}\"," | rev | cut -c 2- | rev';
$installed_packages = array();
foreach($servers as $server){
# Example: /usr/bin/ssh host1 'rpm -qa --qf "\"%{NAME}\":\"%{VERSION}\"," | rev | cut -c 2- | rev'
$exec_command = $transport . " " . $server . " '" . $command . "'";
$out = array();
exec($exec_command, $out, $status);
var_dump($out);
if($status < 1){
$installed_packages[$server] = '{' . $out[0] . '}'; // json package list
}
}
/**
* Compare installed package list with VersionComparison class
*/
require_once 'VersionComparison.php';
$server_details = VersionComparison::CompareVersions($installed_packages);
/**
* Format the output
*/
echo "Details of Package Differences on Servers\n";
echo "Server List: " . implode(', ', $servers) . "\n";
// Base server unspecified find from function
echo "Base Server: " . VersionComparison::GetBaseGroup($installed_packages) . "\n";
foreach($server_details as $key => $value){
echo "\nServer: $key\n";
echo "Missing Packages:\n";
echo "- " . implode("\n- ", $value['missing']) . "\n";
echo "Different Package Versions:\n";
echo "- " . implode("\n- ", array_map_assoc(function ($k, $v){ return "$k => $v"; },$value['different'])) . "\n";
echo "Extra Packages Installed:\n";
echo "- " . implode("\n- ", $value['extra']) . "\n";
}
/**
* Imploding an associative array in PHP
* http://stackoverflow.com/questions/6556985/imploding-an-associative-array-in-php
*/
function array_map_assoc($callback, $array){
$result = array();
foreach($array as $key => $value){
$result[$key] = $callback($key, $value);
}
return $result;
}
?>