forked from savon-noir/python-libnmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff_sample2.py
78 lines (61 loc) · 2.25 KB
/
diff_sample2.py
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
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from libnmap.parser import NmapParser
def nested_obj(objname):
rval = None
splitted = objname.split("::")
if len(splitted) == 2:
rval = splitted
return rval
def print_diff_added(obj1, obj2, added):
for akey in added:
nested = nested_obj(akey)
if nested is not None:
if nested[0] == "NmapHost":
subobj1 = obj1.get_host_byid(nested[1])
elif nested[0] == "NmapService":
subobj1 = obj1.get_service_byid(nested[1])
print("+ {0}".format(subobj1))
else:
print("+ {0} {1}: {2}".format(obj1, akey, getattr(obj1, akey)))
def print_diff_removed(obj1, obj2, removed):
for rkey in removed:
nested = nested_obj(rkey)
if nested is not None:
if nested[0] == "NmapHost":
subobj2 = obj2.get_host_byid(nested[1])
elif nested[0] == "NmapService":
subobj2 = obj2.get_service_byid(nested[1])
print("- {0}".format(subobj2))
else:
print("- {0} {1}: {2}".format(obj2, rkey, getattr(obj2, rkey)))
def print_diff_changed(obj1, obj2, changes):
for mkey in changes:
nested = nested_obj(mkey)
if nested is not None:
if nested[0] == "NmapHost":
subobj1 = obj1.get_host_byid(nested[1])
subobj2 = obj2.get_host_byid(nested[1])
elif nested[0] == "NmapService":
subobj1 = obj1.get_service_byid(nested[1])
subobj2 = obj2.get_service_byid(nested[1])
print_diff(subobj1, subobj2)
else:
print(
"~ {0} {1}: {2} => {3}".format(
obj1, mkey, getattr(obj2, mkey), getattr(obj1, mkey)
)
)
def print_diff(obj1, obj2):
ndiff = obj1.diff(obj2)
print_diff_changed(obj1, obj2, ndiff.changed())
print_diff_added(obj1, obj2, ndiff.added())
print_diff_removed(obj1, obj2, ndiff.removed())
def main():
newrep = NmapParser.parse_fromfile(
"libnmap/test/files/2_hosts_achange.xml"
)
oldrep = NmapParser.parse_fromfile("libnmap/test/files/1_hosts.xml")
print_diff(newrep, oldrep)
if __name__ == "__main__":
main()