@@ -88,7 +88,7 @@ def __init__(self, masscan_search_path=(
8888
8989 """
9090 self ._masscan_path = '' # masscan path
91- self ._scan_result = {"command_line" : {}, "scan" : [] }
91+ self ._scan_result = {"command_line" : {}, "scan" : {} }
9292 self ._masscan_version_number = 0 # masscan version number
9393 self ._masscan_subversion_number = 0 # masscan subversion number
9494 self ._masscan_revised_number = 0 # masscan revised number
@@ -163,9 +163,8 @@ def __getitem__(self, host):
163163 else :
164164 assert type (host ) is str , 'Wrong type for [host], should be a string [was {0}]' .format (type (host ))
165165
166- for item in self ._scan_result ['scan' ]:
167- if item ["ip" ] == host :
168- return item
166+ if host in self ._scan_result ['scan' ]:
167+ return self ._scan_result ['scan' ][host ]
169168 return None
170169
171170 @property
@@ -193,9 +192,7 @@ def all_hosts(self):
193192 """Return a sorted list of all hosts."""
194193 host_list = []
195194 if self ._scan_result ['scan' ]:
196- for item in self ._scan_result ['scan' ]:
197- if "ip" in item and item ["ip" ] not in host_list :
198- host_list .append (item ["ip" ])
195+ host_list = self ._scan_result ['scan' ].keys ()
199196 return host_list
200197
201198 @property
@@ -215,7 +212,7 @@ def scan_result(self):
215212
216213 may raise AssertionError exception if called before scanning
217214 """
218- return self ._scan_result
215+ return json . dumps ( self ._scan_result )
219216
220217 def scan (self , hosts = '127.0.0.1' , ports = PORTS , arguments = '' , sudo = False ):
221218 """
@@ -306,18 +303,21 @@ def scan(self, hosts='127.0.0.1', ports=PORTS, arguments='', sudo=False):
306303 # raise PortScannerError(masscan_err)
307304 masscan_err_keep_trace .append (masscan_err )
308305 try :
309- self ._scan_result ["scan" ] = json .loads (self ._masscan_last_output )
306+ scan_result = json .loads (self ._masscan_last_output )
307+ for item in scan_result :
308+ if item ["ip" ] not in self ._scan_result ["scan" ]:
309+ self ._scan_result ["scan" ][item ["ip" ]] = []
310+ self ._scan_result ["scan" ][item ["ip" ]].extend (item ["ports" ])
311+
310312 except ValueError as ex :
311313 pass
312314
313315 return self ._scan_result
314316
315317 def has_host (self , host ):
316318 """If host has result it returns True, False otherwise."""
317- if self ._scan_result ['scan' ]:
318- for item in self ._scan_result ['scan' ]:
319- if "ip" in item and item ["ip" ] == host :
320- return True
319+ if self ._scan_result ['scan' ] and host in self ._scan_result ['scan' ]:
320+ return True
321321 return False
322322
323323
0 commit comments