Skip to content

Commit b508b81

Browse files
committed
some bug fixes and pylinting. FIXME: pylint & flake8 tests/*
1 parent f43778d commit b508b81

File tree

4 files changed

+250
-95
lines changed

4 files changed

+250
-95
lines changed

libnmap/parser.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class NmapParser(object):
99
@classmethod
10-
def parse(cls, nmap_data=None, type='XML'):
10+
def parse(cls, nmap_data=None, data_type='XML'):
1111
nmap_scan = {'_nmaprun': {}, '_scaninfo': {},
1212
'_hosts': [], '_runstats': {}}
1313
if not nmap_data:
@@ -24,7 +24,7 @@ def parse(cls, nmap_data=None, type='XML'):
2424

2525
root = tree.getroot()
2626
if root.tag == 'nmaprun':
27-
nmap_scan['_nmaprun'] = cls.__format_attributes(root)
27+
nmap_scan['_nmaprun'] = cls.__format_attributes(root)
2828
else:
2929
raise NmapParserException("Unpexpected data structure \
3030
for XML root node")
@@ -41,14 +41,14 @@ def parse(cls, nmap_data=None, type='XML'):
4141
return nmap_scan
4242

4343
@classmethod
44-
def parse_fromstring(cls, nmap_data, type="XML"):
45-
return cls.parse(nmap_data, type)
44+
def parse_fromstring(cls, nmap_data, data_type="XML"):
45+
return cls.parse(nmap_data, data_type)
4646

4747
@classmethod
48-
def parse_fromfile(cls, nmap_report_path, type="XML"):
48+
def parse_fromfile(cls, nmap_report_path, data_type="XML"):
4949
if os.path.exists(nmap_report_path):
5050
fd = open(nmap_report_path, 'r')
51-
r = cls.parse(fd, type)
51+
r = cls.parse(fd, data_type)
5252
fd.close()
5353
else:
5454
raise NmapParserException("Nmap data file could not be found \

libnmap/plugins/mongodb.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55

66

77
class NmapMongoPlugin(NmapDBPlugin):
8-
def __init__(self, **kwargs):
8+
def __init__(self, dbname=None, store=None, **kwargs):
99
NmapDBPlugin.__init__(self)
10+
if dbname is not None:
11+
self.dbname = dbname
12+
if store is not None:
13+
self.store = store
1014
self.dbclient = MongoClient(**kwargs)
1115
self.collection = self.dbclient[self.dbname][self.store]
1216

libnmap/test/host_test.py

Lines changed: 114 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#!/usr/bin/env python
22

33
import unittest
4-
import os, sys
5-
6-
from libnmap import NmapParser, NmapParserException, NmapReport
4+
from libnmap import NmapParser
75

86
host1 = """
9-
<host starttime="1361738377" endtime="1361738377"><status state="up" reason="localhost-response"/>
7+
<host starttime="1361738377" endtime="1361738377">
8+
<status state="up" reason="localhost-response"/>
109
<address addr="127.0.0.1" addrtype="ipv4"/>
1110
<hostnames>
1211
<hostname name="localhost" type="user"/>
@@ -15,17 +14,33 @@
1514
<ports><extraports state="WILLY_WONCKA" count="995">
1615
<extrareasons reason="conn-refused" count="995"/>
1716
</extraports>
18-
<port protocol="tcp" portid="22"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="ssh" method="table" conf="3"/></port>
19-
<port protocol="tcp" portid="25"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="smtp" method="table" conf="3"/></port>
20-
<port protocol="tcp" portid="111"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="rpcbind" method="table" conf="3"/></port>
21-
<port protocol="tcp" portid="631"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="ipp" method="table" conf="3"/></port>
22-
<port protocol="tcp" portid="3306"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="mysql" method="table" conf="3"/></port>
17+
<port protocol="tcp" portid="22">
18+
<state state="open" reason="syn-ack" reason_ttl="0"/>
19+
<service name="ssh" method="table" conf="3"/>
20+
</port>
21+
<port protocol="tcp" portid="25">
22+
<state state="open" reason="syn-ack" reason_ttl="0"/>
23+
<service name="smtp" method="table" conf="3"/>
24+
</port>
25+
<port protocol="tcp" portid="111">
26+
<state state="open" reason="syn-ack" reason_ttl="0"/>
27+
<service name="rpcbind" method="table" conf="3"/>
28+
</port>
29+
<port protocol="tcp" portid="631">
30+
<state state="open" reason="syn-ack" reason_ttl="0"/>
31+
<service name="ipp" method="table" conf="3"/>
32+
</port>
33+
<port protocol="tcp" portid="3306">
34+
<state state="open" reason="syn-ack" reason_ttl="0"/>
35+
<service name="mysql" method="table" conf="3"/>
36+
</port>
2337
</ports>
2438
<times srtt="2100" rttvar="688" to="100000"/>
2539
</host>
2640
"""
2741
host2 = """
28-
<host starttime="1361738318" endtime="13617386177"><status state="up" reason="localhost-respoe"/>
42+
<host starttime="1361738318" endtime="13617386177">
43+
<status state="up" reason="localhost-respoe"/>
2944
<address addr="127.0.0.1" addrtype="ipv4"/>
3045
<hostnames>
3146
<hostname name="localhost" type="user"/>
@@ -35,17 +50,33 @@
3550
<ports><extraports state="closed" count="995">
3651
<extrareasons reason="conn-refused" count="995"/>
3752
</extraports>
38-
<port protocol="tcp" portid="22"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="ssh" method="table" conf="3"/></port>
39-
<port protocol="tcp" portid="25"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="smtp" method="table" conf="3"/></port>
40-
<port protocol="tcp" portid="111"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="rpcbind" method="table" conf="3"/></port>
41-
<port protocol="tcp" portid="631"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="ipp" method="table" conf="3"/></port>
42-
<port protocol="tcp" portid="3306"><state state="open" reason="syn-ack" reason_ttl="1"/><service name="mysql" method="table" conf="3"/></port>
53+
<port protocol="tcp" portid="22">
54+
<state state="open" reason="syn-ack" reason_ttl="0"/>
55+
<service name="ssh" method="table" conf="3"/>
56+
</port>
57+
<port protocol="tcp" portid="25">
58+
<state state="open" reason="syn-ack" reason_ttl="0"/>
59+
<service name="smtp" method="table" conf="3"/>
60+
</port>
61+
<port protocol="tcp" portid="111">
62+
<state state="open" reason="syn-ack" reason_ttl="0"/>
63+
<service name="rpcbind" method="table" conf="3"/>
64+
</port>
65+
<port protocol="tcp" portid="631">
66+
<state state="open" reason="syn-ack" reason_ttl="0"/>
67+
<service name="ipp" method="table" conf="3"/>
68+
</port>
69+
<port protocol="tcp" portid="3306">
70+
<state state="open" reason="syn-ack" reason_ttl="1"/>
71+
<service name="mysql" method="table" conf="3"/>
72+
</port>
4373
</ports>
4474
<times srtt="2100" rttvar="688" to="100000"/>
4575
</host>
4676
"""
47-
host3 = """
48-
<host starttime="13617" endtime="13617"><status state="down" reason="localhost-response"/>
77+
78+
host3 = """<host starttime="13617" endtime="13617">
79+
<status state="down" reason="localhost-response"/>
4980
<address addr="127.0.0.1" addrtype="ipv4"/>
5081
<hostnames>
5182
<hostname name="localhost" type="user"/>
@@ -54,17 +85,33 @@
5485
<ports><extraports state="closed" count="995">
5586
<extrareasons reason="conn-refused" count="995"/>
5687
</extraports>
57-
<port protocol="tcp" portid="22"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="ssh" method="table" conf="3"/></port>
58-
<port protocol="tcp" portid="111"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="rpcbind" method="table" conf="3"/></port>
59-
<port protocol="tcp" portid="631"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="ipp" method="table" conf="3"/></port>
60-
<port protocol="tcp" portid="3306"><state state="closed" reason="syn-ack" reason_ttl="0"/><service name="mysql" method="table" conf="3"/></port>
61-
<port protocol="tcp" portid="3307"><state state="closed" reason="syn-ack" reason_ttl="0"/><service name="mysql" method="table" conf="3"/></port>
88+
<port protocol="tcp" portid="22">
89+
<state state="open" reason="syn-ack" reason_ttl="0"/>
90+
<service name="ssh" method="table" conf="3"/>
91+
</port>
92+
<port protocol="tcp" portid="111">
93+
<state state="open" reason="syn-ack" reason_ttl="0"/>
94+
<service name="rpcbind" method="table" conf="3"/>
95+
</port>
96+
<port protocol="tcp" portid="631">
97+
<state state="open" reason="syn-ack" reason_ttl="0"/>
98+
<service name="ipp" method="table" conf="3"/>
99+
</port>
100+
<port protocol="tcp" portid="3306">
101+
<state state="closed" reason="syn-ack" reason_ttl="0"/>
102+
<service name="mysql" method="table" conf="3"/>
103+
</port>
104+
<port protocol="tcp" portid="3307">
105+
<state state="closed" reason="syn-ack" reason_ttl="0"/>
106+
<service name="mysql" method="table" conf="3"/>
107+
</port>
62108
</ports>
63109
<times srtt="2100" rttvar="688" to="100000"/>
64110
</host>
65111
"""
66112
host4 = """
67-
<host starttime="77" endtime="13"><status state="up" reason="locaonse"/>
113+
<host starttime="77" endtime="13">
114+
<status state="up" reason="locaonse"/>
68115
<address addr="127.0.0.1" addrtype="ipv4"/>
69116
<hostnames>
70117
<hostname name="localhost" type="user"/>
@@ -73,22 +120,33 @@
73120
<ports><extraports state="azeazeaze" count="995">
74121
<extrareasons reason="conn-refused" count="995"/>
75122
</extraports>
76-
<port protocol="tcp" portid="22"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="ssh" method="table" conf="3"/></port>
77-
<port protocol="tcp" portid="25"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="smtp" method="table" conf="3"/></port>
78-
<port protocol="tcp" portid="111"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="rpcbind" method="table" conf="3"/></port>
79-
<port protocol="tcp" portid="631"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="ipp" method="table" conf="3"/></port>
80-
<port protocol="tcp" portid="3306"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="mysql" method="table" conf="3"/></port>
123+
<port protocol="tcp" portid="22">
124+
<state state="open" reason="syn-ack" reason_ttl="0"/>
125+
<service name="ssh" method="table" conf="3"/>
126+
</port>
127+
<port protocol="tcp" portid="25">
128+
<state state="open" reason="syn-ack" reason_ttl="0"/>
129+
<service name="smtp" method="table" conf="3"/>
130+
</port>
131+
<port protocol="tcp" portid="111">
132+
<state state="open" reason="syn-ack" reason_ttl="0"/>
133+
<service name="rpcbind" method="table" conf="3"/>
134+
</port>
135+
<port protocol="tcp" portid="631">
136+
<state state="open" reason="syn-ack" reason_ttl="0"/>
137+
<service name="ipp" method="table" conf="3"/>
138+
</port>
139+
<port protocol="tcp" portid="3306">
140+
<state state="open" reason="syn-ack" reason_ttl="0"/>
141+
<service name="mysql" method="table" conf="3"/>
142+
</port>
81143
</ports>
82144
<times srtt="200" rttvar="68" to="100"/>
83145
</host>
84146
"""
85147

86-
class TestNmapHost(unittest.TestCase):
87-
def setUp(self):
88-
fdir = os.path.dirname(os.path.realpath(__file__))
89-
# self.flist_full = [{'file': "%s/%s" % (fdir, 'files/2_hosts.xml'), 'hosts': 2}, {'file': "%s/%s" % (fdir,'files/1_hosts.xml'), 'hosts': 1},]
90-
# self.flist = self.flist_full
91148

149+
class TestNmapHost(unittest.TestCase):
92150
def test_eq_host(self):
93151
h1 = NmapParser.parse_host(host1)
94152
h2 = NmapParser.parse_host(host2)
@@ -113,20 +171,35 @@ def test_diff_host(self):
113171
self.assertEqual(c1.changed(), set(['hostnames']))
114172
self.assertEqual(c1.added(), set([]))
115173
self.assertEqual(c1.removed(), set([]))
116-
self.assertEqual(c1.unchanged(), set(['status', 'NmapService.343309847', 'NmapService.343309848', 'NmapService.343309921', 'NmapService.343309433', 'address', 'NmapService.343306980']))
174+
self.assertEqual(c1.unchanged(), set(['status',
175+
'NmapService.343309847',
176+
'NmapService.343309848',
177+
'NmapService.343309921',
178+
'NmapService.343309433',
179+
'address',
180+
'NmapService.343306980']))
117181

118-
self.assertEqual(c2.changed(), set(['status', 'NmapService.343306980']))
182+
self.assertEqual(c2.changed(), set(['status',
183+
'NmapService.343306980']))
119184
self.assertEqual(c2.added(), set(['NmapService.343309847']))
120185
self.assertEqual(c2.removed(), set(['NmapService.343306981']))
121-
self.assertEqual(c2.unchanged(), set(['hostnames', 'NmapService.343309848', 'NmapService.343309921', 'NmapService.343309433', 'address']))
186+
self.assertEqual(c2.unchanged(), set(['hostnames',
187+
'NmapService.343309848',
188+
'NmapService.343309921',
189+
'NmapService.343309433',
190+
'address']))
122191

123-
self.assertEqual(c3.changed(), set(['status', 'hostnames', 'NmapService.343306980']))
192+
self.assertEqual(c3.changed(), set(['status', 'hostnames',
193+
'NmapService.343306980']))
124194
self.assertEqual(c3.added(), set(['NmapService.343309847']))
125195
self.assertEqual(c3.removed(), set(['NmapService.343306981']))
126-
self.assertEqual(c3.unchanged(), set(['NmapService.343309848', 'NmapService.343309921', 'NmapService.343309433', 'address']))
196+
self.assertEqual(c3.unchanged(), set(['NmapService.343309848',
197+
'NmapService.343309921',
198+
'NmapService.343309433',
199+
'address']))
200+
127201

128202
if __name__ == '__main__':
129-
# test_suite = [ 'test_diff_host_list', 'test_diff_host' ]
130-
test_suite = [ 'test_eq_host', 'test_diff_host' ]
203+
test_suite = ['test_eq_host', 'test_diff_host']
131204
suite = unittest.TestSuite(map(TestNmapHost, test_suite))
132-
test_result = unittest.TextTestRunner(verbosity=2).run(suite) ## for more verbosity uncomment this line and comment next line
205+
test_result = unittest.TextTestRunner(verbosity=2).run(suite)

0 commit comments

Comments
 (0)