Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick n' dirty utility to compare the state of packages on two ubuntu/debian machines.
#!/usr/bin/env ruby
#
# This utility compares the state of `dpkg -l` on two given boxes
#
puts "Usage: #{$PROGRAM_NAME} hostname other_hostname" and exit unless ARGV.length == 2
PACKAGES = {}
def dpkg(hostname)
raw = `ssh #{hostname} 'dpkg -l | egrep "^[a-z]{2} " | sed -r "s/ +/ /g" | cut -f 1,2,3,4 -d " "'`
Hash.new.tap do |packages|
raw.split("\n").each do |line|
status, name, version, arch = *line.split(' ')
name.gsub!(/:.*/, '')
PACKAGES[name] ||= []
PACKAGES[name] << [hostname, {
status: status,
version: version,
arch: arch
}]
end
end
end
ARGV.each do |hostname|
dpkg hostname
end
exclusives = {}
shared = {}
PACKAGES.each do |package, record|
if record.length == 1
hostname = record.first.first
exclusives[hostname] ||= []
exclusives[hostname] << package
else
first_host_package_info = record.first.last
second_host_package_info = record.last.last
if first_host_package_info != second_host_package_info
shared[package] = record
end
end
end
exclusives.each do |hostname, packages|
packages.each do |package|
puts "ONLY #{hostname} has package #{package}"
end
end
shared.each do |package, installation_information|
first_host_info = installation_information.first
second_host_info = installation_information.last
first_host = first_host_info.first
second_host = second_host_info.first
first_host_package_info = first_host_info.last
second_host_package_info = second_host_info.last
differences = {}
first_host_package_info.collect do |k, v|
if v != second_host_package_info[k]
differences[k] = {first_host => v, second_host => second_host_package_info[k] }
end
end
puts "BOTH #{package} #{differences}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment