11#!/usr/bin/env python
2- from libnmap import NmapDiff , NmapDiffException
2+ from libnmap import NmapDiff
3+
34
45class NmapHost (object ):
5- def __init__ (self , starttime = '' , endtime = '' , address = None , status = None ,
6+ def __init__ (self , starttime = '' , endtime = '' , address = None , status = None ,
67 hostnames = None , services = None ):
78 self ._starttime = starttime
89 self ._endtime = endtime
@@ -16,17 +17,19 @@ def __eq__(self, other):
1617 self .address == other .address and self .changed (other ) == 0 )
1718
1819 def __ne__ (self , other ):
19- return ((self ._hostnames != other ._hostnames or
20+ return ((self ._hostnames != other ._hostnames or
2021 self .address != other .address ) and self .changed (other ))
2122
2223 def __repr__ (self ):
23- return "{0}: [{1} ({2}) - {3}]" .format (self .__class__ .__name__ ,
24+ return "{0}: [{1} ({2}) - {3}]" .format (self .__class__ .__name__ ,
2425 self .address ,
2526 " " .join (self ._hostnames ),
2627 self .status )
28+
2729 def __hash__ (self ):
2830 return (hash (self .status ) ^ hash (self .address ) ^
29- hash (frozenset (self ._services )) ^ hash (frozenset (" " .join (self ._hostnames ))))
31+ hash (frozenset (self ._services )) ^
32+ hash (frozenset (" " .join (self ._hostnames ))))
3033
3134 def changed (self , other ):
3235 return len (self .diff (other ).changed ())
@@ -67,7 +70,6 @@ def starttime(self):
6770 def endtime (self ):
6871 return self ._endtime
6972
70-
7173 def add_hostname (self , hostname ):
7274 self ._hostnames .append (hostname )
7375
@@ -85,15 +87,16 @@ def get_ports(self):
8587 return [(p .port , p .protocol ) for p in self ._services ]
8688
8789 def get_open_ports (self ):
88- return [(p .port , p .protocol ) for p in self ._services if p .state == 'open' ]
90+ return ([(p .port , p .protocol )
91+ for p in self ._services if p .state == 'open' ])
8992
9093 def get_service (self , portno , protocol = 'tcp' ):
9194 plist = [p for p in self ._services if
9295 p .port == portno and p .protocol == protocol ]
9396 return plist .pop () if len (plist ) else None
9497
9598 def get_service_byid (self , id ):
96- service = [ s for s in self .service if s .id () == id ]
99+ service = [s for s in self .service if s .id () == id ]
97100 if len (service ) > 1 :
98101 raise Exception ("Duplicate services found in NmapHost object" )
99102
@@ -104,19 +107,21 @@ def id(self):
104107 return self .address
105108
106109 def get_dict (self ):
107- d = dict ([("%s.%s" % (s .__class__ .__name__ , str (s .id )), hash (s )) for s in self .services ])
108- d .update ({ 'address' : self .address , 'status' : self .status ,
109- 'hostnames' : " " .join (self ._hostnames )})
110+ d = dict ([("%s.%s" % (s .__class__ .__name__ ,
111+ str (s .id )), hash (s )) for s in self .services ])
112+ d .update ({'address' : self .address , 'status' : self .status ,
113+ 'hostnames' : " " .join (self ._hostnames )})
110114 return d
111115
112116 def diff (self , other ):
113117 return NmapDiff (self , other )
114118
119+
115120class NmapService (object ):
116121 def __init__ (self , portid , protocol = 'tcp' , state = None , service = None ):
117122 try :
118123 self ._portid = int (portid or - 1 )
119- except ValueError , TypeError :
124+ except ( ValueError , TypeError ) :
120125 raise
121126 if self ._portid < 0 or self ._portid > 65535 :
122127 raise ValueError
@@ -126,18 +131,21 @@ def __init__(self, portid, protocol='tcp', state=None, service=None):
126131 self ._service = service if service is not None else {}
127132
128133 def __eq__ (self , other ):
129- return (self .id == other .id and self .changed (other ) == 0 )
134+ return (self .id == other .id and self .changed (other ) == 0 )
130135
131136 def __ne__ (self , other ):
132- return (self .id != other .id or self .changed (other ))
137+ return (self .id != other .id or self .changed (other ))
133138
134139 def __repr__ (self ):
135- return "{0}: [{1} - {2}/{3} {4} ({5})]" .format (self .__class__ .__name__ , self .state ,
136- str (self .port ), self .protocol ,
137- self .service , self .banner )
140+ return "{0}: [{1} {2}/{3} {4} ({5})]" .format (self .__class__ .__name__ ,
141+ self .state ,
142+ str (self .port ),
143+ self .protocol ,
144+ self .service ,
145+ self .banner )
138146
139147 def __hash__ (self ):
140- return (hash (self .port ) ^ hash (self .protocol ) ^ hash (self .state ) ^
148+ return (hash (self .port ) ^ hash (self .protocol ) ^ hash (self .state ) ^
141149 hash (self .service ) ^ hash (self .banner ))
142150
143151 def changed (self , other ):
@@ -159,7 +167,7 @@ def protocol(self):
159167 def state (self ):
160168 return self ._state ['state' ] if 'state' in self ._state else None
161169
162- def add_state (self , state = {}):
170+ def add_state (self , state = {}):
163171 self ._state = state
164172
165173 @property
@@ -170,19 +178,24 @@ def add_service(self, service={}):
170178 self ._service = service
171179
172180 def open (self ):
173- return True if self ._state ['state' ] and self ._state ['state' ] == 'open' else False
181+ return (True
182+ if self ._state ['state' ] and self ._state ['state' ] == 'open'
183+ else False )
174184
175185 @property
176186 def banner (self ):
177- notrelevant = ['name' , 'method' , 'conf' ]
187+ notrelevant = ['name' , 'method' , 'conf' ]
178188 b = ''
179189 if self ._service and self ._service ['method' ] == "probed" :
180- b = " " .join ([ k + ": " + self ._service [k ] for k in self ._service .keys () if k not in notrelevant ])
190+ b = " " .join ([k + ": " + self ._service [k ]
191+ for k in self ._service .keys ()
192+ if k not in notrelevant ])
181193 return b
182194
183195 def get_dict (self ):
184- return { 'id' : self .id , 'port' : str (self .port ), 'protocol' : self .protocol ,
185- 'banner' : self .banner , 'service' : self .service , 'state' : self .state }
196+ return ({'id' : self .id , 'port' : str (self .port ),
197+ 'protocol' : self .protocol , 'banner' : self .banner ,
198+ 'service' : self .service , 'state' : self .state })
186199
187- def diff (self , other ):
200+ def diff (self , other ):
188201 return NmapDiff (self , other )
0 commit comments