<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import os <EOL> import re <EOL> import socket <EOL> import logging <EOL> from string import ascii_letters , digits <EOL> import salt . ext . six as six <EOL> from salt . ext . six . moves import range <EOL> try : <EOL> import wmi <EOL> import salt . utils . winapi <EOL> except ImportError : <EOL> pass <EOL> import salt . utils <EOL> from salt . _compat import subprocess , ipaddress <EOL> log = logging . getLogger ( __name__ ) <EOL> def sanitize_host ( host ) : <EOL> '''<STR_LIT>''' <EOL> return '<STR_LIT>' . join ( [ <EOL> c for c in host [ <NUM_LIT:0> : <NUM_LIT:255> ] if c in ( ascii_letters + digits + '<STR_LIT>' ) <EOL> ] ) <EOL> def isportopen ( host , port ) : <EOL> '''<STR_LIT>''' <EOL> if not <NUM_LIT:1> <= int ( port ) <= <NUM_LIT> : <EOL> return False <EOL> sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> out = sock . connect_ex ( ( sanitize_host ( host ) , int ( port ) ) ) <EOL> return out <EOL> def host_to_ip ( host ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> family , socktype , proto , canonname , sockaddr = socket . getaddrinfo ( <EOL> host , <NUM_LIT:0> , socket . AF_UNSPEC , socket . SOCK_STREAM ) [ <NUM_LIT:0> ] <EOL> if family == socket . AF_INET : <EOL> ip , port = sockaddr <EOL> elif family == socket . AF_INET6 : <EOL> ip , port , flow_info , scope_id = sockaddr <EOL> except Exception : <EOL> ip = None <EOL> return ip <EOL> def _filter_localhost_names ( name_list ) : <EOL> '''<STR_LIT>''' <EOL> h = [ ] <EOL> re_filters = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> r'<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> for name in name_list : <EOL> filtered = False <EOL> for f in re_filters : <EOL> if re . match ( f , name ) : <EOL> filtered = True <EOL> break <EOL> if not filtered : <EOL> h . append ( name ) <EOL> return h <EOL> def _sort_hostnames ( hostname_list ) : <EOL> '''<STR_LIT>''' <EOL> punish = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:localhost>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:127.0.0.1>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def _key_hostname ( e ) : <EOL> if '<STR_LIT:U+0020>' in e : <EOL> first = <NUM_LIT:1> <EOL> else : <EOL> first = - <NUM_LIT:1> <EOL> if e in punish : <EOL> second = punish . index ( e ) <EOL> else : <EOL> second = - <NUM_LIT:1> <EOL> third = e . count ( '<STR_LIT::>' ) <EOL> e_is_ipv4 = e . count ( '<STR_LIT:.>' ) == <NUM_LIT:3> and not any ( c . isalpha ( ) for c in e ) <EOL> if e_is_ipv4 : <EOL> if e . startswith ( '<STR_LIT>' ) : <EOL> fourth = <NUM_LIT:1> <EOL> else : <EOL> fourth = <NUM_LIT:0> <EOL> else : <EOL> fourth = - <NUM_LIT:1> <EOL> fifth = - ( e . count ( '<STR_LIT:.>' ) ) <EOL> sixth = - ( len ( e ) ) <EOL> return ( first , second , third , fourth , fifth , sixth ) <EOL> return sorted ( hostname_list , key = _key_hostname ) <EOL> def get_hostnames ( ) : <EOL> '''<STR_LIT>''' <EOL> h = [ ] <EOL> h . append ( socket . gethostname ( ) ) <EOL> h . append ( socket . getfqdn ( ) ) <EOL> try : <EOL> addrinfo = socket . getaddrinfo ( <EOL> socket . gethostname ( ) , <NUM_LIT:0> , socket . AF_UNSPEC , socket . SOCK_STREAM , <EOL> socket . SOL_TCP , socket . AI_CANONNAME <EOL> ) <EOL> for info in addrinfo : <EOL> if len ( info ) >= <NUM_LIT:4> : <EOL> h . append ( info [ <NUM_LIT:3> ] ) <EOL> except socket . gaierror : <EOL> pass <EOL> try : <EOL> name = '<STR_LIT>' <EOL> with salt . utils . fopen ( '<STR_LIT>' ) as hfl : <EOL> name = hfl . read ( ) <EOL> h . append ( name ) <EOL> except ( IOError , OSError ) : <EOL> pass <EOL> if salt . utils . is_sunos ( ) : <EOL> try : <EOL> name = '<STR_LIT>' <EOL> with salt . utils . fopen ( '<STR_LIT>' ) as hfl : <EOL> name = hfl . read ( ) <EOL> h . append ( name ) <EOL> except ( IOError , OSError ) : <EOL> pass <EOL> try : <EOL> with salt . utils . fopen ( '<STR_LIT>' ) as hfl : <EOL> for line in hfl : <EOL> names = line . split ( ) <EOL> try : <EOL> ip = names . pop ( <NUM_LIT:0> ) <EOL> except IndexError : <EOL> continue <EOL> if ip . startswith ( '<STR_LIT>' ) or ip == '<STR_LIT>' : <EOL> for name in names : <EOL> h . append ( name ) <EOL> except ( IOError , OSError ) : <EOL> pass <EOL> if salt . utils . is_windows ( ) : <EOL> try : <EOL> windir = os . getenv ( '<STR_LIT>' ) <EOL> with salt . utils . fopen ( windir + r'<STR_LIT>' ) as hfl : <EOL> for line in hfl : <EOL> if line [ <NUM_LIT:0> ] == '<STR_LIT:#>' or len ( line ) <= <NUM_LIT:1> : <EOL> continue <EOL> try : <EOL> entry = line . split ( ) <EOL> if entry [ <NUM_LIT:0> ] . startswith ( '<STR_LIT>' ) : <EOL> for name in entry [ <NUM_LIT:1> : ] : <EOL> h . append ( name ) <EOL> except IndexError : <EOL> pass <EOL> except ( IOError , OSError ) : <EOL> pass <EOL> hosts = [ ] <EOL> for name in h : <EOL> name = name . strip ( ) <EOL> if len ( name ) > <NUM_LIT:0> : <EOL> hosts . append ( name ) <EOL> hosts = list ( set ( hosts ) ) <EOL> return hosts <EOL> def generate_minion_id ( ) : <EOL> '''<STR_LIT>''' <EOL> possible_ids = get_hostnames ( ) <EOL> for addr in salt . utils . network . ip_addrs ( ) : <EOL> addr = ipaddress . ip_address ( addr ) <EOL> if addr . is_loopback : <EOL> continue <EOL> possible_ids . append ( str ( addr ) ) <EOL> possible_ids = _filter_localhost_names ( possible_ids ) <EOL> if len ( possible_ids ) == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> hosts = _sort_hostnames ( possible_ids ) <EOL> return hosts [ <NUM_LIT:0> ] <EOL> def get_socket ( addr , type = socket . SOCK_STREAM , proto = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> version = ipaddress . ip_address ( addr ) . version <EOL> if version == <NUM_LIT:4> : <EOL> family = socket . AF_INET <EOL> elif version == <NUM_LIT:6> : <EOL> family = socket . AF_INET6 <EOL> return socket . socket ( family , type , proto ) <EOL> def get_fqhostname ( ) : <EOL> '''<STR_LIT>''' <EOL> l = [ ] <EOL> l . append ( socket . getfqdn ( ) ) <EOL> try : <EOL> addrinfo = socket . getaddrinfo ( <EOL> socket . gethostname ( ) , <NUM_LIT:0> , socket . AF_UNSPEC , socket . SOCK_STREAM , <EOL> socket . SOL_TCP , socket . AI_CANONNAME <EOL> ) <EOL> for info in addrinfo : <EOL> if len ( info ) >= <NUM_LIT:4> : <EOL> l . append ( info [ <NUM_LIT:3> ] ) <EOL> except socket . gaierror : <EOL> pass <EOL> l = _sort_hostnames ( l ) <EOL> if len ( l ) > <NUM_LIT:0> : <EOL> return l [ <NUM_LIT:0> ] <EOL> return None <EOL> def ip_to_host ( ip ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> hostname , aliaslist , ipaddrlist = socket . gethostbyaddr ( ip ) <EOL> except Exception : <EOL> hostname = None <EOL> return hostname <EOL> def _validate_ip ( af , ip ) : <EOL> '''<STR_LIT>''' <EOL> if af == socket . AF_INET6 and not socket . has_ipv6 : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> try : <EOL> socket . inet_pton ( af , ip ) <EOL> except AttributeError : <EOL> if af == socket . AF_INET6 : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> try : <EOL> socket . inet_aton ( ip ) <EOL> except socket . error : <EOL> return False <EOL> except ( socket . error , TypeError ) : <EOL> return False <EOL> return True <EOL> def is_ip ( ip ) : <EOL> '''<STR_LIT>''' <EOL> for af in ( socket . AF_INET , socket . AF_INET6 ) : <EOL> if _validate_ip ( af , ip ) : <EOL> return True <EOL> return False <EOL> def is_ipv4 ( ip ) : <EOL> '''<STR_LIT>''' <EOL> return _validate_ip ( socket . AF_INET , ip ) <EOL> def is_ipv6 ( ip ) : <EOL> '''<STR_LIT>''' <EOL> return _validate_ip ( socket . AF_INET6 , ip ) <EOL> def cidr_to_ipv4_netmask ( cidr_bits ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> cidr_bits = int ( cidr_bits ) <EOL> if not <NUM_LIT:1> <= cidr_bits <= <NUM_LIT:32> : <EOL> return '<STR_LIT>' <EOL> except ValueError : <EOL> return '<STR_LIT>' <EOL> netmask = '<STR_LIT>' <EOL> for idx in range ( <NUM_LIT:4> ) : <EOL> if idx : <EOL> netmask += '<STR_LIT:.>' <EOL> if cidr_bits >= <NUM_LIT:8> : <EOL> netmask += '<STR_LIT>' <EOL> cidr_bits -= <NUM_LIT:8> <EOL> else : <EOL> netmask += '<STR_LIT>' . format ( <NUM_LIT> - ( <NUM_LIT:2> ** ( <NUM_LIT:8> - cidr_bits ) ) ) <EOL> cidr_bits = <NUM_LIT:0> <EOL> return netmask <EOL> def _number_of_set_bits_to_ipv4_netmask ( set_bits ) : <EOL> '''<STR_LIT>''' <EOL> return cidr_to_ipv4_netmask ( _number_of_set_bits ( set_bits ) ) <EOL> def _number_of_set_bits ( x ) : <EOL> '''<STR_LIT>''' <EOL> x -= ( x >> <NUM_LIT:1> ) & <NUM_LIT> <EOL> x = ( ( x >> <NUM_LIT:2> ) & <NUM_LIT> ) + ( x & <NUM_LIT> ) <EOL> x = ( ( x >> <NUM_LIT:4> ) + x ) & <NUM_LIT> <EOL> x += x >> <NUM_LIT:8> <EOL> x += x >> <NUM_LIT:16> <EOL> return x & <NUM_LIT> <EOL> def _interfaces_ip ( out ) : <EOL> '''<STR_LIT>''' <EOL> ret = dict ( ) <EOL> def parse_network ( value , cols ) : <EOL> '''<STR_LIT>''' <EOL> brd = None <EOL> scope = None <EOL> if '<STR_LIT:/>' in value : <EOL> ip , cidr = value . split ( '<STR_LIT:/>' ) <EOL> else : <EOL> ip = value <EOL> cidr = <NUM_LIT:32> <EOL> if type_ == '<STR_LIT>' : <EOL> mask = cidr_to_ipv4_netmask ( int ( cidr ) ) <EOL> if '<STR_LIT>' in cols : <EOL> brd = cols [ cols . index ( '<STR_LIT>' ) + <NUM_LIT:1> ] <EOL> elif type_ == '<STR_LIT>' : <EOL> mask = cidr <EOL> if '<STR_LIT>' in cols : <EOL> scope = cols [ cols . index ( '<STR_LIT>' ) + <NUM_LIT:1> ] <EOL> return ( ip , mask , brd , scope ) <EOL> groups = re . compile ( '<STR_LIT>' ) . split ( out ) <EOL> for group in groups : <EOL> iface = None <EOL> data = dict ( ) <EOL> for line in group . splitlines ( ) : <EOL> if '<STR_LIT:U+0020>' not in line : <EOL> continue <EOL> match = re . match ( r'<STR_LIT>' , line ) <EOL> if match : <EOL> iface , parent , attrs = match . groups ( ) <EOL> if '<STR_LIT>' in attrs . split ( '<STR_LIT:U+002C>' ) : <EOL> data [ '<STR_LIT>' ] = True <EOL> else : <EOL> data [ '<STR_LIT>' ] = False <EOL> if parent : <EOL> data [ '<STR_LIT>' ] = parent <EOL> continue <EOL> cols = line . split ( ) <EOL> if len ( cols ) >= <NUM_LIT:2> : <EOL> type_ , value = tuple ( cols [ <NUM_LIT:0> : <NUM_LIT:2> ] ) <EOL> iflabel = cols [ - <NUM_LIT:1> : ] [ <NUM_LIT:0> ] <EOL> if type_ in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if '<STR_LIT>' not in cols : <EOL> ipaddr , netmask , broadcast , scope = parse_network ( value , cols ) <EOL> if type_ == '<STR_LIT>' : <EOL> if '<STR_LIT>' not in data : <EOL> data [ '<STR_LIT>' ] = list ( ) <EOL> addr_obj = dict ( ) <EOL> addr_obj [ '<STR_LIT:address>' ] = ipaddr <EOL> addr_obj [ '<STR_LIT>' ] = netmask <EOL> addr_obj [ '<STR_LIT>' ] = broadcast <EOL> addr_obj [ '<STR_LIT:label>' ] = iflabel <EOL> data [ '<STR_LIT>' ] . append ( addr_obj ) <EOL> elif type_ == '<STR_LIT>' : <EOL> if '<STR_LIT>' not in data : <EOL> data [ '<STR_LIT>' ] = list ( ) <EOL> addr_obj = dict ( ) <EOL> addr_obj [ '<STR_LIT:address>' ] = ipaddr <EOL> addr_obj [ '<STR_LIT>' ] = netmask <EOL> addr_obj [ '<STR_LIT>' ] = scope <EOL> data [ '<STR_LIT>' ] . append ( addr_obj ) <EOL> else : <EOL> if '<STR_LIT>' not in data : <EOL> data [ '<STR_LIT>' ] = list ( ) <EOL> ip_ , mask , brd , scp = parse_network ( value , cols ) <EOL> data [ '<STR_LIT>' ] . append ( { <EOL> '<STR_LIT:type>' : type_ , <EOL> '<STR_LIT:address>' : ip_ , <EOL> '<STR_LIT>' : mask , <EOL> '<STR_LIT>' : brd , <EOL> '<STR_LIT:label>' : iflabel , <EOL> } ) <EOL> del ip_ , mask , brd , scp <EOL> elif type_ . startswith ( '<STR_LIT>' ) : <EOL> data [ '<STR_LIT>' ] = value <EOL> if iface : <EOL> ret [ iface ] = data <EOL> del iface , data <EOL> return ret <EOL> def _interfaces_ifconfig ( out ) : <EOL> '''<STR_LIT>''' <EOL> ret = dict ( ) <EOL> piface = re . compile ( r'<STR_LIT>' ) <EOL> pmac = re . compile ( '<STR_LIT>' ) <EOL> if salt . utils . is_sunos ( ) : <EOL> pip = re . compile ( r'<STR_LIT>' ) <EOL> pip6 = re . compile ( '<STR_LIT>' ) <EOL> pmask6 = re . compile ( r'<STR_LIT>' ) <EOL> else : <EOL> pip = re . compile ( r'<STR_LIT>' ) <EOL> pip6 = re . compile ( '<STR_LIT>' ) <EOL> pmask6 = re . compile ( r'<STR_LIT>' ) <EOL> pmask = re . compile ( r'<STR_LIT>' ) <EOL> pupdown = re . compile ( '<STR_LIT>' ) <EOL> pbcast = re . compile ( r'<STR_LIT>' ) <EOL> groups = re . compile ( '<STR_LIT>' ) . split ( out ) <EOL> for group in groups : <EOL> data = dict ( ) <EOL> iface = '<STR_LIT>' <EOL> updown = False <EOL> for line in group . splitlines ( ) : <EOL> miface = piface . match ( line ) <EOL> mmac = pmac . match ( line ) <EOL> mip = pip . match ( line ) <EOL> mip6 = pip6 . match ( line ) <EOL> mupdown = pupdown . search ( line ) <EOL> if miface : <EOL> iface = miface . group ( <NUM_LIT:1> ) <EOL> if mmac : <EOL> data [ '<STR_LIT>' ] = mmac . group ( <NUM_LIT:1> ) <EOL> if salt . utils . is_sunos ( ) : <EOL> expand_mac = [ ] <EOL> for chunk in data [ '<STR_LIT>' ] . split ( '<STR_LIT::>' ) : <EOL> expand_mac . append ( '<STR_LIT>' . format ( chunk ) if len ( chunk ) < <NUM_LIT:2> else '<STR_LIT>' . format ( chunk ) ) <EOL> data [ '<STR_LIT>' ] = '<STR_LIT::>' . join ( expand_mac ) <EOL> if mip : <EOL> if '<STR_LIT>' not in data : <EOL> data [ '<STR_LIT>' ] = list ( ) <EOL> addr_obj = dict ( ) <EOL> addr_obj [ '<STR_LIT:address>' ] = mip . group ( <NUM_LIT:1> ) <EOL> mmask = pmask . match ( line ) <EOL> if mmask : <EOL> if mmask . group ( <NUM_LIT:1> ) : <EOL> mmask = _number_of_set_bits_to_ipv4_netmask ( <EOL> int ( mmask . group ( <NUM_LIT:1> ) , <NUM_LIT:16> ) ) <EOL> else : <EOL> mmask = mmask . group ( <NUM_LIT:2> ) <EOL> addr_obj [ '<STR_LIT>' ] = mmask <EOL> mbcast = pbcast . match ( line ) <EOL> if mbcast : <EOL> addr_obj [ '<STR_LIT>' ] = mbcast . group ( <NUM_LIT:1> ) <EOL> data [ '<STR_LIT>' ] . append ( addr_obj ) <EOL> if mupdown : <EOL> updown = True <EOL> if mip6 : <EOL> if '<STR_LIT>' not in data : <EOL> data [ '<STR_LIT>' ] = list ( ) <EOL> addr_obj = dict ( ) <EOL> addr_obj [ '<STR_LIT:address>' ] = mip6 . group ( <NUM_LIT:1> ) or mip6 . group ( <NUM_LIT:2> ) <EOL> mmask6 = pmask6 . match ( line ) <EOL> if mmask6 : <EOL> addr_obj [ '<STR_LIT>' ] = mmask6 . group ( <NUM_LIT:1> ) or mmask6 . group ( <NUM_LIT:2> ) <EOL> if not salt . utils . is_sunos ( ) : <EOL> ipv6scope = mmask6 . group ( <NUM_LIT:3> ) or mmask6 . group ( <NUM_LIT:4> ) <EOL> addr_obj [ '<STR_LIT>' ] = ipv6scope . lower ( ) if ipv6scope is not None else ipv6scope <EOL> if addr_obj [ '<STR_LIT:address>' ] != '<STR_LIT>' and addr_obj [ '<STR_LIT>' ] != <NUM_LIT:0> : <EOL> data [ '<STR_LIT>' ] . append ( addr_obj ) <EOL> data [ '<STR_LIT>' ] = updown <EOL> if iface in ret : <EOL> ret [ iface ] = dict ( list ( data . items ( ) ) + list ( ret [ iface ] . items ( ) ) ) <EOL> if '<STR_LIT>' in data : <EOL> ret [ iface ] [ '<STR_LIT>' ] . extend ( x for x in data [ '<STR_LIT>' ] if x not in ret [ iface ] [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in data : <EOL> ret [ iface ] [ '<STR_LIT>' ] . extend ( x for x in data [ '<STR_LIT>' ] if x not in ret [ iface ] [ '<STR_LIT>' ] ) <EOL> else : <EOL> ret [ iface ] = data <EOL> del data <EOL> return ret <EOL> def linux_interfaces ( ) : <EOL> '''<STR_LIT>''' <EOL> ifaces = dict ( ) <EOL> ip_path = salt . utils . which ( '<STR_LIT>' ) <EOL> ifconfig_path = None if ip_path else salt . utils . which ( '<STR_LIT>' ) <EOL> if ip_path : <EOL> cmd1 = subprocess . Popen ( <EOL> '<STR_LIT>' . format ( ip_path ) , <EOL> shell = True , <EOL> close_fds = True , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . STDOUT ) . communicate ( ) [ <NUM_LIT:0> ] <EOL> cmd2 = subprocess . Popen ( <EOL> '<STR_LIT>' . format ( ip_path ) , <EOL> shell = True , <EOL> close_fds = True , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . STDOUT ) . communicate ( ) [ <NUM_LIT:0> ] <EOL> ifaces = _interfaces_ip ( "<STR_LIT>" . format ( <EOL> salt . utils . to_str ( cmd1 ) , <EOL> salt . utils . to_str ( cmd2 ) ) ) <EOL> elif ifconfig_path : <EOL> cmd = subprocess . Popen ( <EOL> '<STR_LIT>' . format ( ifconfig_path ) , <EOL> shell = True , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . STDOUT ) . communicate ( ) [ <NUM_LIT:0> ] <EOL> ifaces = _interfaces_ifconfig ( salt . utils . to_str ( cmd ) ) <EOL> return ifaces <EOL> def _interfaces_ipconfig ( out ) : <EOL> '''<STR_LIT>''' <EOL> ifaces = dict ( ) <EOL> iface = None <EOL> adapter_iface_regex = re . compile ( r'<STR_LIT>' ) <EOL> for line in out . splitlines ( ) : <EOL> if not line : <EOL> continue <EOL> if line . startswith ( '<STR_LIT>' ) : <EOL> iface = ifaces [ adapter_iface_regex . search ( line ) . group ( <NUM_LIT:1> ) ] <EOL> iface [ '<STR_LIT>' ] = True <EOL> addr = None <EOL> continue <EOL> if iface : <EOL> key , val = line . split ( '<STR_LIT:U+002C>' , <NUM_LIT:1> ) <EOL> key = key . strip ( '<STR_LIT>' ) <EOL> val = val . strip ( ) <EOL> if addr and key == '<STR_LIT>' : <EOL> addr [ '<STR_LIT>' ] = val <EOL> elif key in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if '<STR_LIT>' not in iface : <EOL> iface [ '<STR_LIT>' ] = list ( ) <EOL> addr = { '<STR_LIT:address>' : val . rstrip ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None } <EOL> iface [ '<STR_LIT>' ] . append ( addr ) <EOL> elif '<STR_LIT>' in key : <EOL> if '<STR_LIT>' not in iface : <EOL> iface [ '<STR_LIT>' ] = list ( ) <EOL> addr = { '<STR_LIT:address>' : val . rstrip ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : None } <EOL> iface [ '<STR_LIT>' ] . append ( addr ) <EOL> elif key == '<STR_LIT>' : <EOL> iface [ '<STR_LIT>' ] = val <EOL> elif key == '<STR_LIT>' : <EOL> iface [ '<STR_LIT>' ] = ( val != '<STR_LIT>' ) <EOL> def win_interfaces ( ) : <EOL> '''<STR_LIT>''' <EOL> with salt . utils . winapi . Com ( ) : <EOL> c = wmi . WMI ( ) <EOL> ifaces = { } <EOL> for iface in c . Win32_NetworkAdapterConfiguration ( IPEnabled = <NUM_LIT:1> ) : <EOL> ifaces [ iface . Description ] = dict ( ) <EOL> if iface . MACAddress : <EOL> ifaces [ iface . Description ] [ '<STR_LIT>' ] = iface . MACAddress <EOL> if iface . IPEnabled : <EOL> ifaces [ iface . Description ] [ '<STR_LIT>' ] = True <EOL> for ip in iface . IPAddress : <EOL> if '<STR_LIT:.>' in ip : <EOL> if '<STR_LIT>' not in ifaces [ iface . Description ] : <EOL> ifaces [ iface . Description ] [ '<STR_LIT>' ] = [ ] <EOL> item = { '<STR_LIT:address>' : ip , <EOL> '<STR_LIT:label>' : iface . Description } <EOL> if iface . DefaultIPGateway : <EOL> broadcast = next ( ( i for i in iface . DefaultIPGateway if '<STR_LIT:.>' in i ) , '<STR_LIT>' ) <EOL> if broadcast : <EOL> item [ '<STR_LIT>' ] = broadcast <EOL> if iface . IPSubnet : <EOL> netmask = next ( ( i for i in iface . IPSubnet if '<STR_LIT:.>' in i ) , '<STR_LIT>' ) <EOL> if netmask : <EOL> item [ '<STR_LIT>' ] = netmask <EOL> ifaces [ iface . Description ] [ '<STR_LIT>' ] . append ( item ) <EOL> if '<STR_LIT::>' in ip : <EOL> if '<STR_LIT>' not in ifaces [ iface . Description ] : <EOL> ifaces [ iface . Description ] [ '<STR_LIT>' ] = [ ] <EOL> item = { '<STR_LIT:address>' : ip } <EOL> if iface . DefaultIPGateway : <EOL> broadcast = next ( ( i for i in iface . DefaultIPGateway if '<STR_LIT::>' in i ) , '<STR_LIT>' ) <EOL> if broadcast : <EOL> item [ '<STR_LIT>' ] = broadcast <EOL> if iface . IPSubnet : <EOL> netmask = next ( ( i for i in iface . IPSubnet if '<STR_LIT::>' in i ) , '<STR_LIT>' ) <EOL> if netmask : <EOL> item [ '<STR_LIT>' ] = netmask <EOL> ifaces [ iface . Description ] [ '<STR_LIT>' ] . append ( item ) <EOL> else : <EOL> ifaces [ iface . Description ] [ '<STR_LIT>' ] = False <EOL> return ifaces <EOL> def interfaces ( ) : <EOL> '''<STR_LIT>''' <EOL> if salt . utils . is_windows ( ) : <EOL> return win_interfaces ( ) <EOL> else : <EOL> return linux_interfaces ( ) <EOL> def get_net_start ( ipaddr , netmask ) : <EOL> '''<STR_LIT>''' <EOL> net = ipaddress . ip_network ( '<STR_LIT>' . format ( ipaddr , netmask ) , strict = False ) <EOL> return str ( net . network_address ) <EOL> def get_net_size ( mask ) : <EOL> '''<STR_LIT>''' <EOL> binary_str = '<STR_LIT>' <EOL> for octet in mask . split ( '<STR_LIT:.>' ) : <EOL> binary_str += bin ( int ( octet ) ) [ <NUM_LIT:2> : ] . zfill ( <NUM_LIT:8> ) <EOL> return len ( binary_str . rstrip ( '<STR_LIT:0>' ) ) <EOL> def calc_net ( ipaddr , netmask = None ) : <EOL> '''<STR_LIT>''' <EOL> if netmask is not None : <EOL> ipaddr = '<STR_LIT>' . format ( ipaddr , netmask ) <EOL> return str ( ipaddress . ip_network ( ipaddr , strict = False ) ) <EOL> def _ipv4_to_bits ( ipaddr ) : <EOL> '''<STR_LIT>''' <EOL> return '<STR_LIT>' . join ( [ bin ( int ( x ) ) [ <NUM_LIT:2> : ] . rjust ( <NUM_LIT:8> , '<STR_LIT:0>' ) for x in ipaddr . split ( '<STR_LIT:.>' ) ] ) <EOL> def _get_iface_info ( iface ) : <EOL> '''<STR_LIT>''' <EOL> iface_info = interfaces ( ) <EOL> if iface in iface_info . keys ( ) : <EOL> return iface_info , False <EOL> else : <EOL> error_msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( iface , '<STR_LIT>' . join ( iface_info . keys ( ) ) ) ) <EOL> log . error ( error_msg ) <EOL> return None , error_msg <EOL> def hw_addr ( iface ) : <EOL> '''<STR_LIT>''' <EOL> iface_info , error = _get_iface_info ( iface ) <EOL> if error is False : <EOL> return iface_info . get ( iface , { } ) . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> return error <EOL> def interface ( iface ) : <EOL> '''<STR_LIT>''' <EOL> iface_info , error = _get_iface_info ( iface ) <EOL> if error is False : <EOL> return iface_info . get ( iface , { } ) . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> return error <EOL> def interface_ip ( iface ) : <EOL> '''<STR_LIT>''' <EOL> iface_info , error = _get_iface_info ( iface ) <EOL> if error is False : <EOL> return iface_info . get ( iface , { } ) . get ( '<STR_LIT>' , { } ) [ <NUM_LIT:0> ] . get ( '<STR_LIT:address>' , '<STR_LIT>' ) <EOL> else : <EOL> return error <EOL> def _subnets ( proto = '<STR_LIT>' , interfaces_ = None ) : <EOL> '''<STR_LIT>''' <EOL> if interfaces_ is None : <EOL> ifaces = interfaces ( ) <EOL> elif isinstance ( interfaces_ , list ) : <EOL> ifaces = { } <EOL> for key , value in six . iteritems ( interfaces ( ) ) : <EOL> if key in interfaces_ : <EOL> ifaces [ key ] = value <EOL> else : <EOL> ifaces = { interfaces_ : interfaces ( ) . get ( interfaces_ , { } ) } <EOL> ret = set ( ) <EOL> if proto == '<STR_LIT>' : <EOL> subnet = '<STR_LIT>' <EOL> elif proto == '<STR_LIT>' : <EOL> subnet = '<STR_LIT>' <EOL> else : <EOL> log . error ( '<STR_LIT>' . format ( proto ) ) <EOL> return <EOL> for ip_info in six . itervalues ( ifaces ) : <EOL> addrs = ip_info . get ( proto , [ ] ) <EOL> addrs . extend ( [ addr for addr in ip_info . get ( '<STR_LIT>' , [ ] ) if addr . get ( '<STR_LIT:type>' ) == proto ] ) <EOL> for intf in addrs : <EOL> intf = ipaddress . ip_interface ( '<STR_LIT>' . format ( intf [ '<STR_LIT:address>' ] , intf [ subnet ] ) ) <EOL> if not intf . is_loopback : <EOL> ret . add ( intf . network ) <EOL> return [ str ( net ) for net in sorted ( ret ) ] <EOL> def subnets ( interfaces = None ) : <EOL> '''<STR_LIT>''' <EOL> return _subnets ( '<STR_LIT>' , interfaces_ = interfaces ) <EOL> def subnets6 ( ) : <EOL> '''<STR_LIT>''' <EOL> return _subnets ( '<STR_LIT>' ) <EOL> def in_subnet ( cidr , addr = None ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> cidr = ipaddress . ip_network ( cidr ) <EOL> except ValueError : <EOL> log . error ( '<STR_LIT>' . format ( cidr ) ) <EOL> return False <EOL> if addr is None : <EOL> addr = ip_addrs ( ) <EOL> addr . extend ( ip_addrs6 ( ) ) <EOL> elif isinstance ( addr , six . string_types ) : <EOL> return ipaddress . ip_address ( addr ) in cidr <EOL> for ip_addr in addr : <EOL> if ipaddress . ip_address ( ip_addr ) in cidr : <EOL> return True <EOL> return False <EOL> def ip_in_subnet ( addr , cidr ) : <EOL> '''<STR_LIT>''' <EOL> salt . utils . warn_until ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> return in_subnet ( cidr , addr ) <EOL> def _ip_addrs ( interface = None , include_loopback = False , interface_data = None , proto = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> ret = set ( ) <EOL> ifaces = interface_data if isinstance ( interface_data , dict ) else interfaces ( ) <EOL> if interface is None : <EOL> target_ifaces = ifaces <EOL> else : <EOL> target_ifaces = dict ( [ ( k , v ) for k , v in six . iteritems ( ifaces ) <EOL> if k == interface ] ) <EOL> if not target_ifaces : <EOL> log . error ( '<STR_LIT>' . format ( interface ) ) <EOL> for ip_info in six . itervalues ( target_ifaces ) : <EOL> addrs = ip_info . get ( proto , [ ] ) <EOL> addrs . extend ( [ addr for addr in ip_info . get ( '<STR_LIT>' , [ ] ) if addr . get ( '<STR_LIT:type>' ) == proto ] ) <EOL> for addr in addrs : <EOL> addr = ipaddress . ip_address ( addr . get ( '<STR_LIT:address>' ) ) <EOL> if not addr . is_loopback or include_loopback : <EOL> ret . add ( addr ) <EOL> return [ str ( addr ) for addr in sorted ( ret ) ] <EOL> def ip_addrs ( interface = None , include_loopback = False , interface_data = None ) : <EOL> '''<STR_LIT>''' <EOL> return _ip_addrs ( interface , include_loopback , interface_data , '<STR_LIT>' ) <EOL> def ip_addrs6 ( interface = None , include_loopback = False , interface_data = None ) : <EOL> '''<STR_LIT>''' <EOL> return _ip_addrs ( interface , include_loopback , interface_data , '<STR_LIT>' ) <EOL> def hex2ip ( hex_ip , invert = False ) : <EOL> '''<STR_LIT>''' <EOL> if len ( hex_ip ) == <NUM_LIT:32> : <EOL> ip = [ ] <EOL> for i in range ( <NUM_LIT:0> , <NUM_LIT:32> , <NUM_LIT:8> ) : <EOL> ip_part = hex_ip [ i : i + <NUM_LIT:8> ] <EOL> ip_part = [ ip_part [ x : x + <NUM_LIT:2> ] for x in range ( <NUM_LIT:0> , <NUM_LIT:8> , <NUM_LIT:2> ) ] <EOL> if invert : <EOL> ip . append ( "<STR_LIT>" . format ( ip_part ) ) <EOL> else : <EOL> ip . append ( "<STR_LIT>" . format ( ip_part ) ) <EOL> try : <EOL> return ipaddress . IPv6Address ( "<STR_LIT::>" . join ( ip ) ) . compressed <EOL> except ipaddress . AddressValueError as ex : <EOL> log . error ( '<STR_LIT>' . format ( ex ) ) <EOL> return hex_ip <EOL> try : <EOL> hip = int ( hex_ip , <NUM_LIT:16> ) <EOL> except ValueError : <EOL> return hex_ip <EOL> if invert : <EOL> return '<STR_LIT>' . format ( hip >> <NUM_LIT> & <NUM_LIT:255> , <EOL> hip >> <NUM_LIT:16> & <NUM_LIT:255> , <EOL> hip >> <NUM_LIT:8> & <NUM_LIT:255> , <EOL> hip & <NUM_LIT:255> ) <EOL> return '<STR_LIT>' . format ( hip >> <NUM_LIT> & <NUM_LIT:255> , <EOL> hip >> <NUM_LIT:16> & <NUM_LIT:255> , <EOL> hip >> <NUM_LIT:8> & <NUM_LIT:255> , <EOL> hip & <NUM_LIT:255> ) <EOL> def mac2eui64 ( mac , prefix = None ) : <EOL> '''<STR_LIT>''' <EOL> eui64 = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , mac ) . lower ( ) <EOL> eui64 = eui64 [ <NUM_LIT:0> : <NUM_LIT:6> ] + '<STR_LIT>' + eui64 [ <NUM_LIT:6> : ] <EOL> eui64 = hex ( int ( eui64 [ <NUM_LIT:0> : <NUM_LIT:2> ] , <NUM_LIT:16> ) | <NUM_LIT:2> ) [ <NUM_LIT:2> : ] . zfill ( <NUM_LIT:2> ) + eui64 [ <NUM_LIT:2> : ] <EOL> if prefix is None : <EOL> return '<STR_LIT::>' . join ( re . findall ( r'<STR_LIT>' , eui64 ) ) <EOL> else : <EOL> try : <EOL> net = ipaddress . ip_network ( prefix , strict = False ) <EOL> euil = int ( '<STR_LIT>' . format ( eui64 ) , <NUM_LIT:16> ) <EOL> return '<STR_LIT>' . format ( net [ euil ] , net . prefixlen ) <EOL> except : <EOL> return <EOL> def active_tcp ( ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> for statf in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if os . path . isfile ( statf ) : <EOL> with salt . utils . fopen ( statf , '<STR_LIT:rb>' ) as fp_ : <EOL> for line in fp_ : <EOL> if line . strip ( ) . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> ret . update ( _parse_tcp_line ( line ) ) <EOL> return ret <EOL> def local_port_tcp ( port ) : <EOL> '''<STR_LIT>''' <EOL> ret = _remotes_on ( port , '<STR_LIT>' ) <EOL> return ret <EOL> def remote_port_tcp ( port ) : <EOL> '''<STR_LIT>''' <EOL> ret = _remotes_on ( port , '<STR_LIT>' ) <EOL> return ret <EOL> def _remotes_on ( port , which_end ) : <EOL> '''<STR_LIT>''' <EOL> port = int ( port ) <EOL> ret = set ( ) <EOL> proc_available = False <EOL> for statf in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if os . path . isfile ( statf ) : <EOL> proc_available = True <EOL> with salt . utils . fopen ( statf , '<STR_LIT:rb>' ) as fp_ : <EOL> for line in fp_ : <EOL> if line . strip ( ) . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> iret = _parse_tcp_line ( line ) <EOL> sl = next ( iter ( iret ) ) <EOL> if iret [ sl ] [ which_end ] == port : <EOL> ret . add ( iret [ sl ] [ '<STR_LIT>' ] ) <EOL> if not proc_available : <EOL> if salt . utils . is_sunos ( ) : <EOL> return _sunos_remotes_on ( port , which_end ) <EOL> if salt . utils . is_freebsd ( ) : <EOL> return _freebsd_remotes_on ( port , which_end ) <EOL> if salt . utils . is_netbsd ( ) : <EOL> return _netbsd_remotes_on ( port , which_end ) <EOL> if salt . utils . is_openbsd ( ) : <EOL> return _openbsd_remotes_on ( port , which_end ) <EOL> if salt . utils . is_windows ( ) : <EOL> return _windows_remotes_on ( port , which_end ) <EOL> return _linux_remotes_on ( port , which_end ) <EOL> return ret <EOL> def _parse_tcp_line ( line ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> comps = line . strip ( ) . split ( ) <EOL> sl = comps [ <NUM_LIT:0> ] . rstrip ( '<STR_LIT::>' ) <EOL> ret [ sl ] = { } <EOL> l_addr , l_port = comps [ <NUM_LIT:1> ] . split ( '<STR_LIT::>' ) <EOL> r_addr , r_port = comps [ <NUM_LIT:2> ] . split ( '<STR_LIT::>' ) <EOL> ret [ sl ] [ '<STR_LIT>' ] = hex2ip ( l_addr , True ) <EOL> ret [ sl ] [ '<STR_LIT>' ] = int ( l_port , <NUM_LIT:16> ) <EOL> ret [ sl ] [ '<STR_LIT>' ] = hex2ip ( r_addr , True ) <EOL> ret [ sl ] [ '<STR_LIT>' ] = int ( r_port , <NUM_LIT:16> ) <EOL> return ret <EOL> def _sunos_remotes_on ( port , which_end ) : <EOL> '''<STR_LIT>''' <EOL> remotes = set ( ) <EOL> try : <EOL> data = subprocess . check_output ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> except subprocess . CalledProcessError : <EOL> log . error ( '<STR_LIT>' ) <EOL> raise <EOL> lines = salt . utils . to_str ( data ) . split ( '<STR_LIT:\n>' ) <EOL> for line in lines : <EOL> if '<STR_LIT>' not in line : <EOL> continue <EOL> chunks = line . split ( ) <EOL> local_host , local_port = chunks [ <NUM_LIT:0> ] . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> remote_host , remote_port = chunks [ <NUM_LIT:1> ] . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> if which_end == '<STR_LIT>' and int ( remote_port ) != port : <EOL> continue <EOL> if which_end == '<STR_LIT>' and int ( local_port ) != port : <EOL> continue <EOL> remotes . add ( remote_host ) <EOL> return remotes <EOL> def _freebsd_remotes_on ( port , which_end ) : <EOL> '''<STR_LIT>''' <EOL> port = int ( port ) <EOL> remotes = set ( ) <EOL> try : <EOL> cmd = salt . utils . shlex_split ( '<STR_LIT>' . format ( port ) ) <EOL> data = subprocess . check_output ( cmd ) <EOL> except subprocess . CalledProcessError as ex : <EOL> log . error ( '<STR_LIT>' . format ( ex . returncode ) ) <EOL> raise <EOL> lines = salt . utils . to_str ( data ) . split ( '<STR_LIT:\n>' ) <EOL> for line in lines : <EOL> chunks = line . split ( ) <EOL> if not chunks : <EOL> continue <EOL> if '<STR_LIT>' in chunks [ <NUM_LIT:1> ] : <EOL> continue <EOL> if len ( chunks ) < <NUM_LIT:2> : <EOL> continue <EOL> local = chunks [ <NUM_LIT:5> ] <EOL> remote = chunks [ <NUM_LIT:6> ] <EOL> lhost , lport = local . split ( '<STR_LIT::>' ) <EOL> rhost , rport = remote . split ( '<STR_LIT::>' ) <EOL> if which_end == '<STR_LIT>' and int ( lport ) != port : <EOL> continue <EOL> if which_end == '<STR_LIT>' and int ( rport ) != port : <EOL> continue <EOL> remotes . add ( rhost ) <EOL> return remotes <EOL> def _netbsd_remotes_on ( port , which_end ) : <EOL> '''<STR_LIT>''' <EOL> port = int ( port ) <EOL> remotes = set ( ) <EOL> try : <EOL> cmd = salt . utils . shlex_split ( '<STR_LIT>' . format ( port ) ) <EOL> data = subprocess . check_output ( cmd ) <EOL> except subprocess . CalledProcessError as ex : <EOL> log . error ( '<STR_LIT>' . format ( ex . returncode ) ) <EOL> raise <EOL> lines = salt . utils . to_str ( data ) . split ( '<STR_LIT:\n>' ) <EOL> for line in lines : <EOL> chunks = line . split ( ) <EOL> if not chunks : <EOL> continue <EOL> if '<STR_LIT>' in chunks [ <NUM_LIT:1> ] : <EOL> continue <EOL> if len ( chunks ) < <NUM_LIT:2> : <EOL> continue <EOL> local = chunks [ <NUM_LIT:5> ] . split ( '<STR_LIT:.>' ) <EOL> lport = local . pop ( ) <EOL> lhost = '<STR_LIT:.>' . join ( local ) <EOL> remote = chunks [ <NUM_LIT:6> ] . split ( '<STR_LIT:.>' ) <EOL> rport = remote . pop ( ) <EOL> rhost = '<STR_LIT:.>' . join ( remote ) <EOL> if which_end == '<STR_LIT>' and int ( lport ) != port : <EOL> continue <EOL> if which_end == '<STR_LIT>' and int ( rport ) != port : <EOL> continue <EOL> remotes . add ( rhost ) <EOL> return remotes <EOL> def _openbsd_remotes_on ( port , which_end ) : <EOL> '''<STR_LIT>''' <EOL> remotes = set ( ) <EOL> try : <EOL> data = subprocess . check_output ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> except subprocess . CalledProcessError : <EOL> log . error ( '<STR_LIT>' ) <EOL> raise <EOL> lines = data . split ( '<STR_LIT:\n>' ) <EOL> for line in lines : <EOL> if '<STR_LIT>' not in line : <EOL> continue <EOL> chunks = line . split ( ) <EOL> local_host , local_port = chunks [ <NUM_LIT:3> ] . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> remote_host , remote_port = chunks [ <NUM_LIT:4> ] . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> if which_end == '<STR_LIT>' and int ( remote_port ) != port : <EOL> continue <EOL> if which_end == '<STR_LIT>' and int ( local_port ) != port : <EOL> continue <EOL> remotes . add ( remote_host ) <EOL> return remotes <EOL> def _windows_remotes_on ( port , which_end ) : <EOL> r'''<STR_LIT>''' <EOL> remotes = set ( ) <EOL> try : <EOL> data = subprocess . check_output ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> except subprocess . CalledProcessError : <EOL> log . error ( '<STR_LIT>' ) <EOL> raise <EOL> lines = salt . utils . to_str ( data ) . split ( '<STR_LIT:\n>' ) <EOL> for line in lines : <EOL> if '<STR_LIT>' not in line : <EOL> continue <EOL> chunks = line . split ( ) <EOL> local_host , local_port = chunks [ <NUM_LIT:1> ] . rsplit ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> remote_host , remote_port = chunks [ <NUM_LIT:2> ] . rsplit ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> if which_end == '<STR_LIT>' and int ( remote_port ) != port : <EOL> continue <EOL> if which_end == '<STR_LIT>' and int ( local_port ) != port : <EOL> continue <EOL> remotes . add ( remote_host ) <EOL> return remotes <EOL> def _linux_remotes_on ( port , which_end ) : <EOL> '''<STR_LIT>''' <EOL> remotes = set ( ) <EOL> try : <EOL> data = subprocess . check_output ( <EOL> [ '<STR_LIT>' , '<STR_LIT>' . format ( port ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ) <EOL> except subprocess . CalledProcessError as ex : <EOL> if ex . returncode == <NUM_LIT:1> : <EOL> log . warning ( '<STR_LIT>' ) <EOL> return remotes <EOL> log . error ( '<STR_LIT>' . format ( ex . returncode ) ) <EOL> raise <EOL> lines = salt . utils . to_str ( data ) . split ( '<STR_LIT:\n>' ) <EOL> for line in lines : <EOL> chunks = line . split ( ) <EOL> if not chunks : <EOL> continue <EOL> if '<STR_LIT>' in chunks [ <NUM_LIT:0> ] : <EOL> continue <EOL> if '<STR_LIT>' not in chunks [ - <NUM_LIT:1> ] : <EOL> continue <EOL> local , remote = chunks [ <NUM_LIT:8> ] . split ( '<STR_LIT>' ) <EOL> _ , lport = local . rsplit ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> rhost , rport = remote . rsplit ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> if which_end == '<STR_LIT>' and int ( rport ) != port : <EOL> continue <EOL> if which_end == '<STR_LIT>' and int ( lport ) != port : <EOL> continue <EOL> remotes . add ( rhost . strip ( "<STR_LIT>" ) ) <EOL> return remotes </s>
