<s> """<STR_LIT>""" <EOL> from netaddr . core import AddrFormatError <EOL> from netaddr . ip import IPAddress , IPNetwork <EOL> from netaddr . compat import _iter_range , _is_str , _iter_next <EOL> def _nmap_octet_target_values ( spec ) : <EOL> values = set ( ) <EOL> for element in spec . split ( '<STR_LIT:U+002C>' ) : <EOL> if '<STR_LIT:->' in element : <EOL> left , right = element . split ( '<STR_LIT:->' , <NUM_LIT:1> ) <EOL> if not left : <EOL> left = <NUM_LIT:0> <EOL> if not right : <EOL> right = <NUM_LIT:255> <EOL> low = int ( left ) <EOL> high = int ( right ) <EOL> if not ( ( <NUM_LIT:0> <= low <= <NUM_LIT:255> ) and ( <NUM_LIT:0> <= high <= <NUM_LIT:255> ) ) : <EOL> raise ValueError ( '<STR_LIT>' % spec ) <EOL> if low > high : <EOL> raise ValueError ( '<STR_LIT>' % element ) <EOL> for octet in _iter_range ( low , high + <NUM_LIT:1> ) : <EOL> values . add ( octet ) <EOL> else : <EOL> octet = int ( element ) <EOL> if not ( <NUM_LIT:0> <= octet <= <NUM_LIT:255> ) : <EOL> raise ValueError ( '<STR_LIT>' % spec ) <EOL> values . add ( octet ) <EOL> return sorted ( values ) <EOL> def _generate_nmap_octet_ranges ( nmap_target_spec ) : <EOL> if not _is_str ( nmap_target_spec ) : <EOL> raise TypeError ( '<STR_LIT>' % type ( nmap_target_spec ) ) <EOL> if not nmap_target_spec : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> tokens = nmap_target_spec . split ( '<STR_LIT:.>' ) <EOL> if len ( tokens ) != <NUM_LIT:4> : <EOL> raise AddrFormatError ( '<STR_LIT>' % nmap_target_spec ) <EOL> return ( _nmap_octet_target_values ( tokens [ <NUM_LIT:0> ] ) , <EOL> _nmap_octet_target_values ( tokens [ <NUM_LIT:1> ] ) , <EOL> _nmap_octet_target_values ( tokens [ <NUM_LIT:2> ] ) , <EOL> _nmap_octet_target_values ( tokens [ <NUM_LIT:3> ] ) ) <EOL> def _parse_nmap_target_spec ( target_spec ) : <EOL> if '<STR_LIT:/>' in target_spec : <EOL> _ , prefix = target_spec . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> if not ( <NUM_LIT:0> < int ( prefix ) < <NUM_LIT> ) : <EOL> raise AddrFormatError ( '<STR_LIT>' % prefix ) <EOL> net = IPNetwork ( target_spec ) <EOL> if net . version != <NUM_LIT:4> : <EOL> raise AddrFormatError ( '<STR_LIT>' ) <EOL> for ip in net : <EOL> yield ip <EOL> elif '<STR_LIT::>' in target_spec : <EOL> yield IPAddress ( target_spec ) <EOL> else : <EOL> octet_ranges = _generate_nmap_octet_ranges ( target_spec ) <EOL> for w in octet_ranges [ <NUM_LIT:0> ] : <EOL> for x in octet_ranges [ <NUM_LIT:1> ] : <EOL> for y in octet_ranges [ <NUM_LIT:2> ] : <EOL> for z in octet_ranges [ <NUM_LIT:3> ] : <EOL> yield IPAddress ( "<STR_LIT>" % ( w , x , y , z ) , <NUM_LIT:4> ) <EOL> def valid_nmap_range ( target_spec ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> _iter_next ( _parse_nmap_target_spec ( target_spec ) ) <EOL> return True <EOL> except ( TypeError , ValueError , AddrFormatError ) : <EOL> pass <EOL> return False <EOL> def iter_nmap_range ( * nmap_target_spec ) : <EOL> """<STR_LIT>""" <EOL> for target_spec in nmap_target_spec : <EOL> for addr in _parse_nmap_target_spec ( target_spec ) : <EOL> yield addr </s>
