感谢支持
我们一直在努力

Linux比较两台机器上的软件包是否一致

代码:


#!/usr/bin/awk -f
# dpkg-diff.awk – compare and print differencies in the installed packages of two dpkg status files
# by xmb – localhack
# xmb@skilled.com
# 19.11.2003, 23.01.2005(do_ver,status->4)
# status in the array:
# 1 = installed in the reference file, but not in the other one
# 2 = both matching
# 3 = newly intsalled on the other status file
# 4 = version change
# decided to use additionally a plain index for faster processing of the END block
BEGIN {
if (! status) status = “/var/lib/dpkg/status”
if (! do_ver && do_ver != “0”) do_ver = 1 # version comparing
if (ARGC <= 1 && ! stdin) {
print “Usage: dpkg-diff.awk [-v status=reference-status-file] <-v stdin=1|other status file>”
print “By default, /var/lib/dpkg/status is used as reference”
print “use -v stdin=1 to pipe instead of specify a file (like /var/backups .gz ones)”
print “you may also specify only one non reference status file”
exit 1
}
if (stdin)
ARGC = 1
else
ARGC = 2
while (getline < status) {
if ($1 == “Package:”) {
tmp = $2
getline < status
if ($1 != “Status:”)
getline < status
if (! /install ok/)
continue
if (do_ver == 1)
ver = get_ver(status)
Package[tmp] = 1
#if (ver) { Package[tmp, v] = ver; ver = “” }
if (do_ver == 1) Package[tmp, v] = ver
idx = idx tmp ” “
}
} close(status)
}
$1 == “Package:” {
tmp = $2
getline
if ($1 != “Status:”)
getline
if (/install ok/) {
if (Package[tmp]) {
if (do_ver == 1) {
ver = get_ver()
#if (ver == -1) # ..
if (Package[tmp, v] == ver)
Package[tmp] = 2
else {
Package[tmp, “nv”] = ver
Package[tmp] = 4
}
} else
Package[tmp] = 2
} else {
Package[tmp] = 3
idx = idx tmp ” “
}
}
if (! valid_file)
valid_file = 1
}
END {
if (! valid_file) {
print “Invalid file specified”
exit 1
}
# ‘iin’ instead of ‘in’ cause ‘in’ is an awk keyword
split(idx, Idx)
while (Idx[++i]) {
if (Package[Idx[i]] == 1)
Res[1, ++iin] = Idx[i]
else if (Package[Idx[i]] == 3)
Res[3, ++out] = Idx[i]
else if (Package[Idx[i]] == 4)
Res[4, ++V] = Idx[i]
} iin = out = V = 0
while (Res[1, ++iin])
printf “+ %s\n”, Res[1, iin]
while (Res[3, ++out])
printf “- %s\n”, Res[3, out]
while (Res[4, ++V])
printf “~ %-20s\t%-10s -> %s\n”, Res[4, V], Package[Res[4, V], v], Package[Res[4, V], “nv”]
#if (! Res[1, 1] && ! Res[3, 1])
if (iin == out == V == 1)
print “No differencies found.”
}
function get_ver(stream ,stop) { # eliminate duplicated getline code.. but
while (! stop) {             # no but, function done
if (stream) getline < stream
else getline
if ($1 == “Version:”)
return $2
else if (! $1)
return -1
}
}


运行:
./dpkg-diff.awk –v status=/var/lib/dpkg/status /path/to/other_dpkg_status

赞(0) 打赏
转载请注明出处:服务器评测 » Linux比较两台机器上的软件包是否一致
分享到: 更多 (0)

听说打赏我的人,都进福布斯排行榜啦!

支付宝扫一扫打赏

微信扫一扫打赏