1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-07 13:11:28 +00:00

Let get_best_osmatch keep the original XML ordering.

Ties in accuracy are broken by osmatches' relative position in the XML
file, because Nmap writes results in descending accuracy order.
This commit is contained in:
david
2012-05-05 18:02:43 +00:00
parent cac71422e8
commit ec027e9085

View File

@@ -199,20 +199,15 @@ class HostInfo(object):
return self._osmatches return self._osmatches
def get_best_osmatch(self): def get_best_osmatch(self):
"""Return the OS match with the highest accuracy. If there is a tie, one """Return the OS match with the highest accuracy."""
of the best matches will be returned."""
if not self._osmatches: if not self._osmatches:
return None return None
def osmatch_key(osmatch): def osmatch_key(osmatch):
# Sort first by accuracy, then by name so it's deterministic.
try: try:
accuracy = float(osmatch.get("accuracy", "")) return -float(osmatch["accuracy"])
except ValueError: except ValueError:
accuracy = 0 return 0
return (accuracy, osmatch.get("name")) return sorted(self._osmatches, key = osmatch_key)[0]
osmatches = self.osmatches[:]
osmatches.sort(cmp = lambda a, b: cmp(osmatch_key(a), osmatch_key(b)))
return osmatches[-1]
# ports_used is a list like # ports_used is a list like