예제 #1
0
    def __init__(self, *args, **kwargs):
        """Intialize the varible that are used in establishing connection to the ACS and\
           Intialize an HTTP SOAP client which will authenticate with the ACS server.

        :param ``*args``: the arguments to be used if any
        :type ``*args``: tuple
        :param ``**kwargs``: extra args to be used if any (mainly contains username, password, ipadress and port)
        :type ``**kwargs``: dict
        """
        self.args = args
        self.kwargs = kwargs
        self.username = self.kwargs['username']
        self.password = self.kwargs['password']
        self.ipaddr = self.kwargs['ipaddr']
        self.port = self.kwargs.get('port', None)
        self.cli_port = self.kwargs.pop('cli_port', '22')
        self.cli_username = self.kwargs.pop('cli_username', None)
        self.cli_password = self.kwargs.pop('cli_password', None)
        self.color = self.kwargs.pop('color', None)
        self.options = self.kwargs.pop('options', None)
        AxirosACS.CPE_wait_time = self.kwargs.pop('wait_time',
                                                  AxirosACS.CPE_wait_time)

        if self.options:
            options = [x.strip() for x in self.options.split(',')]
            for opt in options:
                if opt.startswith('wan-static-ipv6:'):
                    ipv6_address = opt.replace('wan-static-ipv6:', '').strip()
                    if "/" not in opt:
                        ipv6_address += "/64"
                    self.ipv6_interface = ipaddress.IPv6Interface(ipv6_address)
                    self.gwv6 = self.ipv6_interface.ip

        if self.port is not None:
            target = self.ipaddr + ":" + self.port
        else:
            target = self.ipaddr

        self.wsdl = "http://" + target + "/live/CPEManager/DMInterfaces/soap/getWSDL"

        session = Session()
        session.auth = HTTPBasicAuth(self.username, self.password)

        self.client = Client(
            wsdl=self.wsdl,
            transport=Transport(session=session,
                                cache=InMemoryCache(timeout=3600 * 3)),
            wsse=UsernameToken(self.username, self.password),
        )

        # to spawn pexpect on cli
        if all([self.ipaddr, self.cli_username, self.cli_password]):
            bft_pexpect_helper.spawn.__init__(
                self,
                command="ssh",
                args=[
                    '%s@%s' % (self.cli_username, self.ipaddr), '-p',
                    self.cli_port, '-o', 'StrictHostKeyChecking=no', '-o',
                    'UserKnownHostsFile=/dev/null', '-o',
                    'ServerAliveInterval=60', '-o', 'ServerAliveCountMax=5'
                ])
            self.check_connection(self.cli_username, self.name,
                                  self.cli_password)
            self.print_connected_console_msg(self.ipaddr, self.cli_port,
                                             self.color, self.name)
        # this should be populater ONLY when using __main__
        self.cpeid = self.kwargs.pop('cpeid', None)
예제 #2
0
파일: vpp_format.py 프로젝트: zhongyoub/vpp
def unformat_api_address_with_prefix_t(o):
    if o.address.af == 1:
        return ipaddress.IPv6Interface((o.address.un.ip6, o.len))
    if o.address.af == 0:
        return ipaddress.IPv4Interface((o.address.un.ip4, o.len))
    raise ValueError('Unknown address family {}'.format(o))
예제 #3
0
                f'{network["ovs_bridge"]} is not a valid ovs_bridge')
        validated['ovs_bridge'] = network['ovs_bridge']

        if 'ovs_vlan' in network:
            if (not isinstance(network['ovs_vlan'], bool)
                    and isinstance(network['ovs_vlan'], int)
                    and MIN_VLAN <= network['ovs_vlan'] <= MAX_VLAN):
                validated['ovs_vlan'] = network['ovs_vlan']
            else:
                raise ValueError(
                    f'{network["ovs_vlan"]} is not a valid ovs_vlan')

        validated['address4'] = ipaddress.IPv4Interface(network['address4'])

        if 'address6' in network:
            validated['address6'] = ipaddress.IPv6Interface(
                network['address6'])

        validated['gateway4'] = ipaddress.IPv4Address(network['gateway4'])

        if 'gateway6' in network:
            validated['gateway6'] = ipaddress.IPv6Address(network['gateway6'])

        if not isinstance(network['nameservers'], list):
            raise ValueError(
                f'{network["nameservers"]} is not a valid list of nameservers')
        validated['nameservers'] = [
            ipaddress.IPv4Address(nameserver)
            for nameserver in network['nameservers']
        ]

        return {'network': validated}
예제 #4
0
파일: vpp_format.py 프로젝트: zxt2012bs/vpp
def unformat_api_address_with_prefix_t(o):
    if o.address.af == 1:
        return ipaddress.IPv6Interface((o.address.un.ip6, o.len))
    if o.address.af == 0:
        return ipaddress.IPv4Interface((o.address.un.ip4, o.len))
    return None
예제 #5
0
    (Pri(10, 'foo', 100.0, True), Pri),  # dataclass
    (Pri(10, 'foo', 100.0, True), Optional[Pri]),
    (None, Optional[Pri]),
    (pathlib.Path('/tmp/foo'), pathlib.Path),  # Extended types
    (pathlib.Path('/tmp/foo'), Optional[pathlib.Path]),
    (None, Optional[pathlib.Path]),
    (pathlib.PurePath('/tmp/foo'), pathlib.PurePath),
    (pathlib.PurePosixPath('/tmp/foo'), pathlib.PurePosixPath),
    (pathlib.PureWindowsPath('C:\\tmp'), pathlib.PureWindowsPath),
    (uuid.UUID("8f85b32c-a0be-466c-87eb-b7bbf7a01683"), uuid.UUID),
    (ipaddress.IPv4Address("127.0.0.1"), ipaddress.IPv4Address),
    (ipaddress.IPv6Address("::1"), ipaddress.IPv6Address),
    (ipaddress.IPv4Network("127.0.0.0/8"), ipaddress.IPv4Network),
    (ipaddress.IPv6Network("::/128"), ipaddress.IPv6Network),
    (ipaddress.IPv4Interface("192.168.1.1/24"), ipaddress.IPv4Interface),
    (ipaddress.IPv6Interface("::1/128"), ipaddress.IPv6Interface),
    (decimal.Decimal(10), decimal.Decimal),
    (datetime.now(), datetime),
    (date.today(), date),
]

# these types can only be instantiated on their corresponding system
if os.name == "posix":
    types.append((pathlib.PosixPath('/tmp/foo'), pathlib.PosixPath))
if os.name == "nt":
    types.append((pathlib.WindowsPath('C:\\tmp'), pathlib.WindowsPath))

if sys.version_info[:3] >= (3, 9, 0):
    types.extend([([1, 2], list[int]), ({'a': 1}, dict[str, int]), ((1, 1), tuple[int, int])])

types_combinations: List = list(map(lambda c: list(more_itertools.flatten(c)), itertools.combinations(types, 2)))
예제 #6
0
    def __init__(self, *args, **kwargs):
        self.args = args
        self.kwargs = kwargs
        name = kwargs.pop('name', None)
        ipaddr = kwargs.pop('ipaddr', None)
        color = kwargs.pop('color', 'black')
        username = kwargs.pop('username', 'root')
        password = kwargs.pop('password', 'bigfoot1')
        port = kwargs.pop('port', '22')
        output = kwargs.pop('output', sys.stdout)
        reboot = kwargs.pop('reboot', False)
        location = kwargs.pop('location', None)
        pre_cmd_host = kwargs.pop('pre_cmd_host', None)
        cmd = kwargs.pop('cmd', None)
        post_cmd_host = kwargs.pop('post_cmd_host', None)
        post_cmd = kwargs.pop('post_cmd', None)
        cleanup_cmd = kwargs.pop('cleanup_cmd', None)
        env = kwargs.pop('env', None)
        lan_network = kwargs.pop('lan_network',
                                 ipaddress.IPv4Network(u"192.168.1.0/24"))
        lan_gateway = kwargs.pop('lan_gateway',
                                 ipaddress.IPv4Address(u"192.168.1.1"))

        self.http_proxy = kwargs.pop('http_proxy', None)

        if pre_cmd_host is not None:
            sys.stdout.write("\tRunning pre_cmd_host.... ")
            sys.stdout.flush()
            phc = pexpect.spawn(command='bash',
                                args=['-c', pre_cmd_host],
                                env=env)
            phc.expect(pexpect.EOF, timeout=120)
            print("\tpre_cmd_host done")

        if ipaddr is not None:
            pexpect.spawn.__init__(self,
                                   command="ssh",
                                   args=[
                                       '%s@%s' % (username, ipaddr), '-p',
                                       port, '-o', 'StrictHostKeyChecking=no',
                                       '-o', 'UserKnownHostsFile=/dev/null',
                                       '-o', 'ServerAliveInterval=60', '-o',
                                       'ServerAliveCountMax=5'
                                   ])

            self.ipaddr = ipaddr

        if cleanup_cmd is not None:
            self.cleanup_cmd = cleanup_cmd
            atexit.register(self.run_cleanup_cmd)

        if cmd is not None:
            sys.stdout.write("\tRunning cmd.... ")
            sys.stdout.flush()
            pexpect.spawn.__init__(self,
                                   command="bash",
                                   args=['-c', cmd],
                                   env=env)
            self.ipaddr = None
            print("\tcmd done")

        self.name = name
        self.color = color
        self.output = output
        self.username = username
        if username != "root":
            self.prompt.append('%s\\@.*:.*$' % username)
        self.password = password
        self.port = port
        self.location = location
        self.env = env
        self.lan_network = lan_network
        self.lan_gateway = lan_gateway
        self.tftp_device = self

        # we need to pick a non-conflicting private network here
        # also we want it to be consistant and not random for a particular
        # board
        if self.gw is None:
            if (lan_gateway - lan_network.num_addresses).is_private:
                self.gw = lan_gateway - lan_network.num_addresses
            else:
                self.gw = lan_gateway + lan_network.num_addresses

        self.gw_ng = ipaddress.IPv4Interface(
            str(self.gw).decode('utf-8') + '/' + str(lan_network.netmask))
        self.nw = self.gw_ng.network
        self.gw_prefixlen = self.nw.prefixlen

        # override above values if set in wan options
        if 'options' in kwargs:
            options = [x.strip() for x in kwargs['options'].split(',')]
            for opt in options:
                if opt.startswith('wan-static-ip:'):
                    value = opt.replace('wan-static-ip:', '')
                    self.gw = ipaddress.IPv4Address(value.split('/')[0])
                    if '/' not in value:
                        value = value + (u'/24')
                    # TODO: use IPv4 and IPv6 interface object everywhere in this class
                    self.gw_ng = ipaddress.IPv4Interface(value)
                    self.nw = self.gw_ng.network
                    self.gw_prefixlen = self.nw.prefixlen
                    self.static_ip = True
                if opt.startswith('wan-static-ipv6:'):
                    if "/" in opt:
                        ipv6_interface = ipaddress.IPv6Interface(
                            opt.replace('wan-static-ipv6:', ''))
                        self.gwv6 = ipv6_interface.ip
                        self.ipv6_prefix = ipv6_interface._prefixlen
                    else:
                        self.gwv6 = ipaddress.IPv6Address(
                            opt.replace('wan-static-ipv6:', ''))
                if opt.startswith('wan-static-route:'):
                    self.static_route = opt.replace('wan-static-route:',
                                                    '').replace('-', ' via ')
                # TODO: remove wan-static-route at some point above
                if opt.startswith('static-route:'):
                    self.static_route = opt.replace('static-route:',
                                                    '').replace('-', ' via ')
                if opt == 'wan-dhcp-client':
                    self.wan_dhcp = True
                if opt == 'wan-no-eth0':
                    self.wan_no_eth0 = True
                if opt == 'wan-no-dhcp-sever':
                    self.wan_dhcp_server = False
                if opt == 'wan-dhcp-client-v6':
                    self.wan_dhcpv6 = True
                if opt.startswith('mgmt-dns:'):
                    value = opt.replace('mgmt-dns:', '')
                    self.mgmt_dns = ipaddress.IPv4Address(value.split('/')[0])

        try:
            i = self.expect([
                "yes/no", "assword:", "Last login", username + ".*'s password:"******"Unable to connect to %s." % name)
        except pexpect.EOF as e:
            if hasattr(self, "before"):
                print(self.before)
            raise Exception("Unable to connect to %s." % name)
        if i == 0:
            self.sendline("yes")
            i = self.expect(["Last login", "assword:"])
        if i == 1 or i == 3:
            self.sendline(password)
        else:
            pass
        # if we did initially get a prompt wait for one here
        if i < 4:
            self.expect(self.prompt)

        if ipaddr is None:
            self.sendline('hostname')
            self.expect('hostname')
            self.expect(self.prompt)
            ipaddr = self.ipaddr = self.before.strip()

        self.sendline('alias mgmt')
        idx = self.expect(['alias mgmt=', pexpect.TIMEOUT], timeout=10)
        if idx == 0:
            self.expect(self.prompt)
            self.sendline('alias apt="mgmt apt"; alias apt-get="mgmt apt-get"')
            self.expect_exact(
                'alias apt="mgmt apt"; alias apt-get="mgmt apt-get"')
        self.expect(self.prompt)

        if self.port != 22:
            cprint("%s port %s device console = %s" %
                   (ipaddr, port, colored(color, color)),
                   None,
                   attrs=['bold'])
        else:
            cprint("%s device console = %s" % (ipaddr, colored(color, color)),
                   None,
                   attrs=['bold'])

        if post_cmd_host is not None:
            sys.stdout.write("\tRunning post_cmd_host.... ")
            sys.stdout.flush()
            phc = pexpect.spawn(command='bash',
                                args=['-c', post_cmd_host],
                                env=env)
            i = phc.expect([pexpect.EOF, pexpect.TIMEOUT, 'password'])
            if i > 0:
                print("\tpost_cmd_host did not complete, it likely failed\n")
            else:
                print("\tpost_cmd_host done")

        if post_cmd is not None:
            sys.stdout.write("\tRunning post_cmd.... ")
            sys.stdout.flush()
            env_prefix = ""
            for k, v in env.iteritems():
                env_prefix += "export %s=%s; " % (k, v)

            self.sendline(env_prefix + post_cmd)
            self.expect(self.prompt)
            print("\tpost_cmd done")

        if reboot:
            self.reset()

        self.logfile_read = output
예제 #7
0
    def __init__(self, *args, **kwargs):
        self.args = args
        self.kwargs = kwargs
        name = kwargs.pop('name', None)
        ipaddr = kwargs.pop('ipaddr', None)
        color = kwargs.pop('color', 'black')
        username = kwargs.pop('username', 'root')
        password = kwargs.pop('password', 'bigfoot1')
        port = kwargs.pop('port', '22')
        output = kwargs.pop('output', sys.stdout)
        reboot = kwargs.pop('reboot', False)
        location = kwargs.pop('location', None)
        self.dev_array = kwargs.pop('dev_array', None)
        pre_cmd_host = kwargs.pop('pre_cmd_host', None)
        cmd = kwargs.pop('cmd', None)
        post_cmd_host = kwargs.pop('post_cmd_host', None)
        post_cmd = kwargs.pop('post_cmd', None)
        cleanup_cmd = kwargs.pop('cleanup_cmd', None)
        lan_network = ipaddress.IPv4Interface(
            six.text_type(kwargs.pop('lan_network', "192.168.1.0/24"))).network
        lan_gateway = ipaddress.IPv4Interface(
            six.text_type(kwargs.pop('lan_gateway', "192.168.1.1/24"))).ip

        self.http_proxy = kwargs.pop('http_proxy', None)

        if pre_cmd_host is not None:
            sys.stdout.write("\tRunning pre_cmd_host.... ")
            sys.stdout.flush()
            phc = bft_pexpect_helper.spawn(command='bash',
                                           args=['-c', pre_cmd_host],
                                           env=self.dev.env)
            phc.expect(pexpect.EOF, timeout=120)
            print("\tpre_cmd_host done")

        if ipaddr is not None:
            bft_pexpect_helper.spawn.__init__(
                self,
                command="ssh",
                args=[
                    '%s@%s' % (username, ipaddr), '-p', port, '-o',
                    'StrictHostKeyChecking=no', '-o',
                    'UserKnownHostsFile=/dev/null', '-o',
                    'ServerAliveInterval=60', '-o', 'ServerAliveCountMax=5'
                ])

            self.ipaddr = ipaddr

        if cleanup_cmd is not None:
            self.cleanup_cmd = cleanup_cmd
            atexit.register(self.run_cleanup_cmd)

        if cmd is not None:
            sys.stdout.write("\tRunning cmd.... ")
            sys.stdout.flush()
            bft_pexpect_helper.spawn.__init__(self,
                                              command="bash",
                                              args=['-c', cmd],
                                              env=self.dev.env)
            self.ipaddr = None
            print("\tcmd done")

        self.name = name
        self.color = color
        self.output = output
        self.username = username
        if username != "root":
            self.prompt.append('%s\\@.*:.*$' % username)
        self.password = password
        self.port = port
        self.location = location
        self.lan_network = lan_network
        self.lan_gateway = lan_gateway
        self.tftp_device = self

        self.check_connection(username, name, password)

        # attempts to fix the cli colums size
        self.set_cli_size(200)

        # we need to pick a non-conflicting private network here
        # also we want it to be consistant and not random for a particular
        # board
        if self.gw is None:
            if (lan_gateway - lan_network.num_addresses).is_private:
                self.gw = lan_gateway - lan_network.num_addresses
            else:
                self.gw = lan_gateway + lan_network.num_addresses

        self.gw_ng = ipaddress.IPv4Interface(
            six.text_type(str(self.gw) + '/' + str(lan_network.prefixlen)))
        self.nw = self.gw_ng.network
        self.gw_prefixlen = self.nw.prefixlen

        # override above values if set in wan options
        if 'options' in kwargs:
            options = [x.strip() for x in kwargs['options'].split(',')]
            for opt in options:
                if opt.startswith('wan-static-ip:'):
                    value = six.text_type(opt.replace('wan-static-ip:', ''))
                    if '/' not in value:
                        value = value + (u'/24')
                    self.gw_ng = ipaddress.IPv4Interface(value)
                    self.nw = self.gw_ng.network
                    self.gw_prefixlen = self.nw._prefixlen
                    self.gw = self.gw_ng.ip
                    self.static_ip = True
                if opt.startswith('wan-static-ipv6:'):
                    ipv6_address = six.text_type(
                        opt.replace('wan-static-ipv6:', ''))
                    if "/" not in opt:
                        ipv6_address += "/%s" % six.text_type(
                            str(self.ipv6_prefix))
                    self.ipv6_interface = ipaddress.IPv6Interface(ipv6_address)
                    self.ipv6_prefix = self.ipv6_interface._prefixlen
                    self.gwv6 = self.ipv6_interface.ip
                if opt.startswith('wan-static-route:'):
                    self.static_route = opt.replace('wan-static-route:',
                                                    '').replace('-', ' via ')
                # TODO: remove wan-static-route at some point above
                if opt.startswith('static-route:'):
                    self.static_route = opt.replace('static-route:',
                                                    '').replace('-', ' via ')
                if opt == 'wan-dhcp-client':
                    self.wan_dhcp = True
                if opt == 'wan-no-eth0':
                    self.wan_no_eth0 = True
                if opt == 'wan-no-dhcp-sever':
                    self.wan_dhcp_server = False
                if opt == 'wan-dhcp-client-v6':
                    self.wan_dhcpv6 = True
                if opt.startswith('mgmt-dns:'):
                    value = six.text_type(opt.replace('mgmt-dns:', ''))
                    self.mgmt_dns = ipaddress.IPv4Interface(value).ip
                else:
                    self.mgmt_dns = "8.8.8.8"

        if ipaddr is None:
            self.sendline('hostname')
            self.expect('hostname')
            self.expect(self.prompt)
            ipaddr = self.ipaddr = self.before.strip()

        self.sendline('alias mgmt')
        idx = self.expect(
            ['alias mgmt=', 'alias: mgmt: not found', pexpect.TIMEOUT],
            timeout=10)
        if idx == 0:
            self.expect(self.prompt)
            self.shim = "mgmt"
            self.sendline('alias apt="mgmt apt"; alias apt-get="mgmt apt-get"')
            self.expect_exact(
                'alias apt="mgmt apt"; alias apt-get="mgmt apt-get"')
        self.expect(self.prompt)

        self.print_connected_console_msg(ipaddr, port, color, name)

        if post_cmd_host is not None:
            sys.stdout.write("\tRunning post_cmd_host.... ")
            sys.stdout.flush()
            phc = bft_pexpect_helper.spawn(command='bash',
                                           args=['-c', post_cmd_host],
                                           env=self.dev.env)
            i = phc.expect([pexpect.EOF, pexpect.TIMEOUT, 'password'])
            if i > 0:
                print("\tpost_cmd_host did not complete, it likely failed\n")
            else:
                print("\tpost_cmd_host done")

        if post_cmd is not None:
            sys.stdout.write("\tRunning post_cmd.... ")
            sys.stdout.flush()
            env_prefix = ""
            for k, v in self.dev.env.items():
                env_prefix += "export %s=%s; " % (k, v)

            self.sendline(env_prefix + post_cmd)
            self.expect(self.prompt)
            print("\tpost_cmd done")

        if reboot:
            self.reset()

        self.logfile_read = output
예제 #8
0
파일: test_base.py 프로젝트: samriang/pybsd
 def test_no_lo_if(self):
     self.assertSequenceEqual(self.system.lo_if.ifsv4, [ipaddress.IPv4Interface('127.0.0.1/8')],
                     'incorrect lo_if ifsv4')
     self.assertSequenceEqual(self.system.lo_if.ifsv6, [ipaddress.IPv6Interface('::1/110')],
                     'incorrect lo_if ifsv6')
예제 #9
0
파일: test_base.py 프로젝트: samriang/pybsd
 def test_ext_if_ifsv6(self):
     self.assertSequenceEqual(self.system.ext_if.ifsv6, [ipaddress.IPv6Interface('1c02:4f8:0f0:14e6::/110')],
                     'incorrect ext_if ifsv6')
예제 #10
0
파일: fields.py 프로젝트: pycopia/devtest
 def python_value(self, value):
     return None if value is None else ipaddress.IPv6Interface(value)
예제 #11
0
def get_win_ifaddrs():
    """
    A method for retrieving info of the network
    interfaces. Returns a nested dictionary of
    interfaces in Windows.
    """
    # based on code from jaraco and many other attempts
    # on internet.
    # Fixed by <@gpotter2> from scapy's implementation to
    # add IPv6 support + fix structures

    import ctypes
    import struct
    import ipaddress
    import ctypes.wintypes
    from ctypes.wintypes import DWORD, WCHAR, BYTE, BOOL
    from socket import AF_INET
    
    # from iptypes.h
    MAX_ADAPTER_ADDRESS_LENGTH = 8
    MAX_DHCPV6_DUID_LENGTH = 130

    GAA_FLAG_INCLUDE_PREFIX = ctypes.c_ulong(0x0010)
    
    class in_addr(Structure):
        _fields_ = [("byte", ctypes.c_ubyte * 4)]


    class in6_addr(ctypes.Structure):
        _fields_ = [("byte", ctypes.c_ubyte * 16)]


    class sockaddr_in(ctypes.Structure):
        _fields_ = [("sin_family", ctypes.c_short),
                    ("sin_port", ctypes.c_ushort),
                    ("sin_addr", in_addr),
                    ("sin_zero", 8 * ctypes.c_char)]


    class sockaddr_in6(ctypes.Structure):
        _fields_ = [("sin6_family", ctypes.c_short),
                    ("sin6_port", ctypes.c_ushort),
                    ("sin6_flowinfo", ctypes.c_ulong),
                    ("sin6_addr", in6_addr),
                    ("sin6_scope_id", ctypes.c_ulong)]


    class SOCKADDR_INET(ctypes.Union):
        _fields_ = [("Ipv4", sockaddr_in),
                    ("Ipv6", sockaddr_in6),
                    ("si_family", ctypes.c_short)]
    LPSOCKADDR_INET = ctypes.POINTER(SOCKADDR_INET)

    class SOCKET_ADDRESS(ctypes.Structure):
        _fields_ = [
            ('address', LPSOCKADDR_INET),
            ('length', ctypes.c_int),
            ]

    class _IP_ADAPTER_ADDRESSES_METRIC(ctypes.Structure):
        _fields_ = [
            ('length', ctypes.c_ulong),
            ('interface_index', DWORD),
            ]

    class _IP_ADAPTER_ADDRESSES_U1(ctypes.Union):
        _fields_ = [
            ('alignment', ctypes.c_ulonglong),
            ('metric', _IP_ADAPTER_ADDRESSES_METRIC),
            ]

    class IP_ADAPTER_UNICAST_ADDRESS(ctypes.Structure):
        pass
    PIP_ADAPTER_UNICAST_ADDRESS = ctypes.POINTER(IP_ADAPTER_UNICAST_ADDRESS)
    IP_ADAPTER_UNICAST_ADDRESS._fields_ = [
            ("length", ctypes.c_ulong),
            ("flags", DWORD),
            ("next", PIP_ADAPTER_UNICAST_ADDRESS),
            ("address", SOCKET_ADDRESS),
            ("prefix_origin", ctypes.c_int),
            ("suffix_origin", ctypes.c_int),
            ("dad_state", ctypes.c_int),
            ("valid_lifetime", ctypes.c_ulong),
            ("preferred_lifetime", ctypes.c_ulong),
            ("lease_lifetime", ctypes.c_ulong),
            ("on_link_prefix_length", ctypes.c_ubyte)
            ]

    class IP_ADAPTER_PREFIX(ctypes.Structure):
        pass
    PIP_ADAPTER_PREFIX = ctypes.POINTER(IP_ADAPTER_PREFIX)
    IP_ADAPTER_PREFIX._fields_ = [
        ("alignment", ctypes.c_ulonglong),
        ("next", PIP_ADAPTER_PREFIX),
        ("address", SOCKET_ADDRESS),
        ("prefix_length", ctypes.c_ulong)
        ]

    class IP_ADAPTER_ADDRESSES(ctypes.Structure):
        pass
    LP_IP_ADAPTER_ADDRESSES = ctypes.POINTER(IP_ADAPTER_ADDRESSES)
    
    # for now, just use void * for pointers to unused structures
    PIP_ADAPTER_ANYCAST_ADDRESS = ctypes.c_void_p
    PIP_ADAPTER_MULTICAST_ADDRESS = ctypes.c_void_p
    PIP_ADAPTER_DNS_SERVER_ADDRESS = ctypes.c_void_p
    #PIP_ADAPTER_PREFIX = ctypes.c_void_p
    PIP_ADAPTER_WINS_SERVER_ADDRESS_LH = ctypes.c_void_p
    PIP_ADAPTER_GATEWAY_ADDRESS_LH = ctypes.c_void_p
    PIP_ADAPTER_DNS_SUFFIX = ctypes.c_void_p

    IF_OPER_STATUS = ctypes.c_uint # this is an enum, consider http://code.activestate.com/recipes/576415/
    IF_LUID = ctypes.c_uint64

    NET_IF_COMPARTMENT_ID = ctypes.c_uint32
    GUID = ctypes.c_byte*16
    NET_IF_NETWORK_GUID = GUID
    NET_IF_CONNECTION_TYPE = ctypes.c_uint # enum
    TUNNEL_TYPE = ctypes.c_uint # enum

    IP_ADAPTER_ADDRESSES._fields_ = [
        ('length', ctypes.c_ulong),
        ('interface_index', DWORD),
        ('next', LP_IP_ADAPTER_ADDRESSES),
        ('adapter_name', ctypes.c_char_p),
        ('first_unicast_address', PIP_ADAPTER_UNICAST_ADDRESS),
        ('first_anycast_address', PIP_ADAPTER_ANYCAST_ADDRESS),
        ('first_multicast_address', PIP_ADAPTER_MULTICAST_ADDRESS),
        ('first_dns_server_address', PIP_ADAPTER_DNS_SERVER_ADDRESS),
        ('dns_suffix', ctypes.c_wchar_p),
        ('description', ctypes.c_wchar_p),
        ('friendly_name', ctypes.c_wchar_p),
        ('byte', BYTE * MAX_ADAPTER_ADDRESS_LENGTH),
        ('physical_address_length', DWORD),
        ('flags', DWORD),
        ('mtu', DWORD),
        ('interface_type', DWORD),
        ('oper_status', IF_OPER_STATUS),
        ('ipv6_interface_index', DWORD),
        ('zone_indices', DWORD * 16),
        ('first_prefix', PIP_ADAPTER_PREFIX),
        ('transmit_link_speed', ctypes.c_uint64),
        ('receive_link_speed', ctypes.c_uint64),
        ('first_wins_server_address', PIP_ADAPTER_WINS_SERVER_ADDRESS_LH),
        ('first_gateway_address', PIP_ADAPTER_GATEWAY_ADDRESS_LH),
        ('ipv4_metric', ctypes.c_ulong),
        ('ipv6_metric', ctypes.c_ulong),
        ('luid', IF_LUID),
        ('dhcpv4_server', SOCKET_ADDRESS),
        ('compartment_id', NET_IF_COMPARTMENT_ID),
        ('network_guid', NET_IF_NETWORK_GUID),
        ('connection_type', NET_IF_CONNECTION_TYPE),
        ('tunnel_type', TUNNEL_TYPE),
        ('dhcpv6_server', SOCKET_ADDRESS),
        ('dhcpv6_client_duid', ctypes.c_byte * MAX_DHCPV6_DUID_LENGTH),
        ('dhcpv6_client_duid_length', ctypes.c_ulong),
        ('dhcpv6_iaid', ctypes.c_ulong),
        ('first_dns_suffix', PIP_ADAPTER_DNS_SUFFIX),
        ]

    def GetAdaptersAddresses(af=0):
        """
        Returns an iteratable list of adapters.
        param:
         - af: the address family to read on
        """ 
        size = ctypes.c_ulong()
        AF_UNSPEC = 0
        flags = GAA_FLAG_INCLUDE_PREFIX
        GetAdaptersAddresses = ctypes.windll.iphlpapi.GetAdaptersAddresses
        GetAdaptersAddresses.argtypes = [
            ctypes.c_ulong,
            ctypes.c_ulong,
            ctypes.c_void_p,
            ctypes.POINTER(IP_ADAPTER_ADDRESSES),
            ctypes.POINTER(ctypes.c_ulong),
        ]
        GetAdaptersAddresses.restype = ctypes.c_ulong
        res = GetAdaptersAddresses(af, flags, None, None, size)
        if res != 0x6f: # BUFFER OVERFLOW -> populate size
            raise RuntimeError("Error getting structure length (%d)" % res)
        pointer_type = ctypes.POINTER(IP_ADAPTER_ADDRESSES)
        buffer = ctypes.create_string_buffer(size.value)
        struct_p = ctypes.cast(buffer, pointer_type)
        res = GetAdaptersAddresses(af, flags, None, struct_p, size)
        if res != 0x0: # NO_ERROR
            raise RuntimeError("Error retrieving table (%d)" % res)
        while struct_p:
            yield struct_p.contents
            struct_p = struct_p.contents.next

    result = []
    # In theory, we could use AF_UNSPEC = 0, but it doesn't work in practice
    for i in itertools.chain(GetAdaptersAddresses(AF_INET), GetAdaptersAddresses(AF_INET6)):
        #print("--------------------------------------")
        #print("IF: {0}".format(i.description))
        #print("\tdns_suffix: {0}".format(i.dns_suffix))
        #print("\tinterface type: {0}".format(i.interface_type))
        fu = i.first_unicast_address.contents
        ad = fu.address.address.contents
        #print("\tfamily: {0}".format(ad.family))
        if ad.si_family == AF_INET:
            ip_bytes = bytes(bytearray(ad.Ipv4.sin_addr))
            ip = ipaddress.IPv4Address(ip_bytes)
            ip_if = ipaddress.IPv4Interface(u("{0}/{1}".format(ip, fu.on_link_prefix_length)))
        elif ad.si_family == AF_INET6:
            ip_bytes = bytes(bytearray(ad.Ipv6.sin6_addr))
            ip = ipaddress.IPv6Address(ip_bytes)
            ip_if = ipaddress.IPv6Interface(u("{0}/{1}".format(ip, fu.on_link_prefix_length)))
        #print("\tipaddress: {0}".format(ip))
        #print("\tnetmask: {0}".format(ip_if.netmask))
        #print("\tnetwork: {0}".format(ip_if.network.network_address))
        #print("\tbroadcast: {0}".format(ip_if.network.broadcast_address))
        #print("\tmask length: {0}".format(fu.on_link_prefix_length))
        data = {}
        data['addr'] = "{0}".format(ip)
        data['netmask'] = "{0}".format(ip_if.netmask)
        data['broadcast'] = "{0}".format(ip_if.network.broadcast_address)
        data['network'] = "{0}".format(ip_if.network.network_address)

        name = i.description 
        #result[i.description] = { ad.family : d}
        iface = {}
        for interface in result:
            if name in interface.keys():
                iface = interface
                break
        if iface:
            iface[name][ad.si_family] = data
        else:
            iface[name] = { ad.si_family : data }
            result.append(iface)

    return result
예제 #12
0
 def test_ifsv6(self):
     interface = Interface(name='re0', ips='aa:aa:0:0::1/110')
     self.assertSequenceEqual(interface.ifsv4, [], 'incorrect ifsv4')
     self.assertSequenceEqual(interface.ifsv6,
                              [ipaddress.IPv6Interface('aa:aa::1/110')],
                              'incorrect ifsv6')
예제 #13
0
def checkSpf(mailHeaders):
    """Check if Ip from SPF received is in dig querry"""
    receivedSpf = mailHeaders['Received-SPF']
    if receivedSpf:
        spfResult = dict()
        if re.search('Softfail', receivedSpf):
            spfResult['spfResult'] = 'Softfail'
            return spfResult
        elif re.search('Fail', receivedSpf):
            spfResult['spfResult'] = 'Fail'
            return spfResult
        elif re.search('Neutral',receivedSpf):
            spfResult['spfResult'] = 'Neutral'
            return spfResult
        elif re.search('Permerror', receivedSpf):
            spfResult['spfResult'] = 'Permerror'
            return spfResult
        elif re.search('TempError', receivedSpf):
            spfResult['spfResult'] = 'Temperror'
            return spfResult
        elif re.search('None', receivedSpf):
            spfResult['spfResult'] = 'None'
            return spfResult
        elif re.search('Pass', receivedSpf):
            spfResult['spfResult'] = 'Pass'
            try:
                senderDomain = re.search('(\@[^\s]*)', receivedSpf).group(1)
            except AttributeError:
                # Domain not found in the original string
                senderDomain = '' # apply your error handling
            try:
                senderDomain = senderDomain.split('@')
            except AttributeError:
                senderDomain = '' #apply your error handling
                senderDomain = senderDomain[1].split(';')
            try:
                senderIp = re.search('(\client-ip=[^\s]*)',receivedSpf).group(1)
            except AttributeError:
                senderIp = '' # apply your error handling
                senderIp = senderIp.split('client-ip=')
                senderIp = senderIp[1].split(';')
            try:
                senderIp = ipaddress.ip_address(senderIp[0])
            except:
                senderIp = ''
            #print(senderIp[0])
            try:
                digResult = sp.check_output(["dig", "+short","TXT",senderDomain[0]]).decode("ascii")
            except sp.CalledProcessError:
                return spfResult
            #ipNetworks = re.findall(r'(([0-9]{1,3}.){3}.([0-9]{1,3}))(\/\d\d)?',digResult)
            ipNetworks = re.findall( r'(([0-9]+(?:\.[0-9]+){3})+/+[0-9]{2})|([0-9]+(?:\.[0-9]+){3})', digResult)
            print(ipNetworks)
            if ipNetworks != None:
                results = [t[0] for t in ipNetworks]
                #print(len(results))
                for i, val in enumerate(results):
                    try:
                        network = val
                    except:
                        network = ''
                        #print(network)
                    if not network:
                        spfResult['spfResult'] = 'Pass'
                        return spfResult
                    else:
                        try:
                            network = ipaddress.ip_network(network)
                            if senderIp in ipaddress.ip_network(network):
                                spfResult['spfTrue'] = 'True'
                                return spfResult
                            else:
                                if i == len(results):
                                    spfResult['spfTrue'] = 'False'
                                    return spfResult
                        except ValueError:
                            network = ipaddress.IPv6Interface(network)
                            if senderIp in network.network:
                                spfResult['spfTrue'] = 'True'
                                return spfResult
                            else:
                                if i== len(results):
                                    spfResult['spfTrue'] = 'False'
                                    return spfResult
        else:
            return spfResult
예제 #14
0
def generate_configs_alt(output_path):
    logging.debug("generate_configs_alt")

    # servers
    for peer in pm.peers:
        if peer.peertype == PeerType.CLIENT:
            continue
        filename = f'{output_path}/{peer.alias}.conf'
        logging.info(f'Generating configuration file for {filename}')
        with open(filename, 'w') as config:
            # Write Interface configuration
            config.write('[Interface]\n')
            if peer.alias:
                config.write(f'# Alias: {peer.alias}\n')
            config.write(f'PrivateKey = {peer.private_key}\n')
            address = peer.address
            if peer.address6 != '':
                address = f'{address},{peer.address6}'
            config.write(f'Address = {address}\n')
            config.write(f'ListenPort = {peer.listen_port}\n')
            config.write(
                f'PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE; ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -A POSTROUTING -o eth0 -j MASQUERADE; iptables -A INPUT -s 192.168.195.0/24 -p udp -m udp --dport 53 -m conntrack --ctstate NEW -j ACCEPT; ip6tables -A INPUT -s fd42:42:42::0/64 -p udp -m udp --dport 53 -m conntrack --ctstate NEW -j ACCEPT\n'
            )
            config.write(
                f'PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -D POSTROUTING -o eth0 -j MASQUERADE; iptables -D INPUT -s 192.168.195.0/24 -p udp -m udp --dport 53 -m conntrack --ctstate NEW -j ACCEPT; iptables -D INPUT -s fd42:42:42::0/64 -p udp -m udp --dport 53 -m conntrack --ctstate NEW -j ACCEPT\n'
            )
            config.write(f'SaveConfig = false\n')

            for p in pm.peers:
                if p.address == peer.address:
                    continue

                config.write('\n[Peer]\n')
                print(p.private_key)
                if p.alias:
                    config.write(f'# Alias: {p.alias}\n')
                config.write(f'PublicKey = {wg.pubkey(p.private_key)}\n')
                address = p.address
                if p.address6 != '':
                    address = f'{address},{p.address6}'
                if (p.address == pm.master.address):
                    config.write(
                        f'Endpoint = {p.public_address}:{p.listen_port}\n')
                    config.write('PersistentKeepalive = 25\n')
                    config.write(
                        f'AllowedIPs = {pm.ip4interface},{pm.ip6interface}\n')
                else:
                    config.write(f'AllowedIPs = {address}\n')

                ipreshared = find_hash(peer.private_key, p.private_key) % len(
                    pm.preshared)
                logging.debug(ipreshared)
                preshared = pm.preshared[ipreshared]
                config.write(f'PresharedKey = {preshared}\n')

    # clients
    logging.debug("generate_configs_alt")
    for peer in pm.peers:
        if peer.peertype == PeerType.SERVER:
            continue
        for p in pm.peers:
            if p.address == peer.address:  #self,ignore
                continue
            if p.peertype == PeerType.CLIENT:
                continue
            filename = f'{output_path}/{pm.prefix}-{p.alias}-{peer.alias}.conf'
            logging.info(f'Generating configuration file for {filename}')
            with open(filename, 'w') as config:
                config.write('[Interface]\n')
                if peer.alias:
                    config.write(f'# Alias: {peer.alias}\n')
                config.write(f'PrivateKey = {peer.private_key}\n')
                config.write(f'# PublicKey = {wg.pubkey(peer.private_key)}\n')
                address = peer.address
                if peer.address6 != '':
                    address = f'{address},{peer.address6}'
                config.write(f'Address = {address}\n')
                listen_port = str(random.randrange(50000, 59999))
                if peer.listen_port != '':
                    listen_port = peer.listen_port
                config.write(f'ListenPort = {listen_port}\n')
                addr4 = ipaddress.IPv4Interface(p.address)
                addr6 = ipaddress.IPv6Interface(p.address6)
                config.write(f'DNS = {addr4.ip},{addr6.ip}\n')
                config.write(f'MTU = 1280')

                config.write('\n[Peer]\n')
                print(p.private_key)
                if p.alias:
                    config.write(f'# Alias: {p.alias}\n')
                config.write(f'PublicKey = {wg.pubkey(p.private_key)}\n')
                if p.peertype == PeerType.DYNAMIC:
                    config.write(
                        f'AllowedIPs = {pm.ip4interface},{pm.ip6interface}\n')
                if p.peertype == PeerType.CLIENT:
                    config.write(f'AllowedIPs = 0.0.0.0/0,::/0\n')
                config.write(
                    f'Endpoint = {p.public_address}:{p.listen_port}\n')
                if p.keep_alive:
                    config.write('PersistentKeepalive = 25\n')

                ipreshared = find_hash(peer.private_key, p.private_key) % len(
                    pm.preshared)
                logging.debug(ipreshared)
                preshared = pm.preshared[ipreshared]
                config.write(f'PresharedKey = {preshared}\n')
예제 #15
0
    def __init__(self, *args, **kwargs):
        """Instance initialization."""
        self.args = args
        self.kwargs = kwargs
        name = kwargs.pop("name", None)
        ipaddr = kwargs.pop("ipaddr", None)
        color = kwargs.pop("color", "black")
        username = kwargs.pop("username", "root")
        password = kwargs.pop("password", "bigfoot1")
        port = kwargs.pop("port", "22")
        output = kwargs.pop("output", sys.stdout)
        reboot = kwargs.pop("reboot", False)
        location = kwargs.pop("location", None)
        self.dev_array = kwargs.pop("dev_array", None)
        pre_cmd_host = kwargs.pop("pre_cmd_host", None)
        cmd = kwargs.pop("cmd", None)
        post_cmd_host = kwargs.pop("post_cmd_host", None)
        post_cmd = kwargs.pop("post_cmd", None)
        cleanup_cmd = kwargs.pop("cleanup_cmd", None)
        lan_network = ipaddress.IPv4Interface(
            six.text_type(kwargs.pop("lan_network", "192.168.1.0/24"))).network
        lan_gateway = ipaddress.IPv4Interface(
            six.text_type(kwargs.pop("lan_gateway", "192.168.1.1/24"))).ip

        self.http_proxy = kwargs.pop("http_proxy", ipaddr + ':8080')

        if pre_cmd_host is not None:
            sys.stdout.write("\tRunning pre_cmd_host.... ")
            sys.stdout.flush()
            phc = bft_pexpect_helper.spawn(command="bash",
                                           args=["-c", pre_cmd_host],
                                           env=self.dev.env)
            phc.expect(pexpect.EOF, timeout=120)
            print("\tpre_cmd_host done")

        self.legacy_add = False
        # introducing a hack till json schema does not get updated
        if not self.dev_array:
            self.legacy_add = True
            arr_names = {"lan": "lan_clients", "wan": "wan_clients"}
            for k, v in arr_names.items():
                if k in name:
                    self.dev_array = v

        if ipaddr is not None:
            bft_pexpect_helper.spawn.__init__(
                self,
                command="ssh",
                args=[
                    "%s@%s" % (username, ipaddr),
                    "-p",
                    port,
                    "-o",
                    "StrictHostKeyChecking=no",
                    "-o",
                    "UserKnownHostsFile=/dev/null",
                    "-o",
                    "ServerAliveInterval=60",
                    "-o",
                    "ServerAliveCountMax=5",
                ],
            )

            self.ipaddr = ipaddr

        if cleanup_cmd is not None:
            self.cleanup_cmd = cleanup_cmd
            atexit.register(self.run_cleanup_cmd)

        if cmd is not None:
            sys.stdout.write("\tRunning cmd.... ")
            sys.stdout.flush()
            bft_pexpect_helper.spawn.__init__(self,
                                              command="bash",
                                              args=["-c", cmd],
                                              env=self.dev.env)
            self.ipaddr = None
            print("\tcmd done")

        self.name = name
        self.color = color
        self.output = output
        self.username = username
        if username != "root":
            self.prompt.append("%s\\@.*:.*$" % username)
        self.password = password
        self.port = port
        self.location = location
        self.lan_network = lan_network
        self.lan_gateway = lan_gateway
        self.tftp_device = self
        self.dante = False

        self.check_connection(username, name, password)

        # attempts to fix the cli colums size
        self.set_cli_size(200)

        # we need to pick a non-conflicting private network here
        # also we want it to be consistant and not random for a particular
        # board
        if self.gw is None:
            if (lan_gateway - lan_network.num_addresses).is_private:
                self.gw = lan_gateway - lan_network.num_addresses
            else:
                self.gw = lan_gateway + lan_network.num_addresses

        self.gw_ng = ipaddress.IPv4Interface(
            six.text_type(str(self.gw) + "/" + str(lan_network.prefixlen)))
        self.nw = self.gw_ng.network
        self.gw_prefixlen = self.nw.prefixlen

        # override above values if set in wan options
        if "options" in kwargs:
            options = [x.strip() for x in kwargs["options"].split(",")]
            for opt in options:
                if opt.startswith("wan-static-ip:"):
                    value = six.text_type(opt.replace("wan-static-ip:", ""))
                    if "/" not in value:
                        value = value + (u"/24")
                    self.gw_ng = ipaddress.IPv4Interface(value)
                    self.nw = self.gw_ng.network
                    self.gw_prefixlen = self.nw._prefixlen
                    self.gw = self.gw_ng.ip
                    self.static_ip = True
                if opt.startswith("wan-static-ipv6:"):
                    ipv6_address = six.text_type(
                        opt.replace("wan-static-ipv6:", ""))
                    if "/" not in opt:
                        ipv6_address += "/%s" % six.text_type(
                            str(self.ipv6_prefix))
                    self.ipv6_interface = ipaddress.IPv6Interface(ipv6_address)
                    self.ipv6_prefix = self.ipv6_interface._prefixlen
                    self.gwv6 = self.ipv6_interface.ip
                if opt.startswith("wan-static-route:"):
                    self.static_route = opt.replace("wan-static-route:",
                                                    "").replace("-", " via ")
                # TODO: remove wan-static-route at some point above
                if opt.startswith("static-route:"):
                    self.static_route = opt.replace("static-route:",
                                                    "").replace("-", " via ")
                if opt == "wan-dhcp-client":
                    self.wan_dhcp = True
                if opt == "wan-no-eth0":
                    self.wan_no_eth0 = True
                if opt == "wan-no-dhcp-sever":
                    self.wan_dhcp_server = False
                if opt == "wan-dhcp-client-v6":
                    self.wan_dhcpv6 = True
                if opt.startswith("mgmt-dns:"):
                    value = six.text_type(opt.replace("mgmt-dns:", ""))
                    self.mgmt_dns = ipaddress.IPv4Interface(value).ip
                else:
                    self.mgmt_dns = "8.8.8.8"
                if opt == 'dante':
                    self.dante = True

        if ipaddr is None:
            self.sendline("hostname")
            self.expect("hostname")
            self.expect(self.prompt)
            ipaddr = self.ipaddr = self.before.strip()

        self.sendline("alias mgmt")
        idx = self.expect(
            ["alias mgmt=", "alias: mgmt: not found", pexpect.TIMEOUT],
            timeout=10)
        if idx == 0:
            self.expect(self.prompt)
            self.shim = "mgmt"
            self.sendline('alias apt="mgmt apt"; alias apt-get="mgmt apt-get"')
            self.expect_exact(
                'alias apt="mgmt apt"; alias apt-get="mgmt apt-get"')
        self.expect(self.prompt)

        self.print_connected_console_msg(ipaddr, port, color, name)

        if post_cmd_host is not None:
            sys.stdout.write("\tRunning post_cmd_host.... ")
            sys.stdout.flush()
            phc = bft_pexpect_helper.spawn(command="bash",
                                           args=["-c", post_cmd_host],
                                           env=self.dev.env)
            i = phc.expect([pexpect.EOF, pexpect.TIMEOUT, "password"])
            if i > 0:
                print("\tpost_cmd_host did not complete, it likely failed\n")
            else:
                print("\tpost_cmd_host done")

        if post_cmd is not None:
            sys.stdout.write("\tRunning post_cmd.... ")
            sys.stdout.flush()
            env_prefix = ""
            for k, v in self.dev.env.items():
                env_prefix += "export %s=%s; " % (k, v)

            self.sendline(env_prefix + post_cmd)
            self.expect(self.prompt)
            print("\tpost_cmd done")

        if reboot:
            self.reset()

        self.logfile_read = output
예제 #16
0
          file=open('output.txt', 'a'))

    ip_ver = ip_details.version

    print("IP Version:", ip_ver, file=open('output.txt', 'a'))

    if ip_ver == 4:

        print("\nIP Address:",
              ipaddress.IPv4Interface(input_ip).ip,
              file=open('output.txt', 'a'))

    elif ip_ver == 6:

        print("\nIP Address:",
              ipaddress.IPv6Interface(input_ip).ip,
              file=open('output.txt', 'a'))

    print("\nIP Network:",
          ip_details,
          " or ",
          ip_details.with_netmask,
          file=open('output.txt', 'a'))

    print("\nSubnet Mask:", ip_details.netmask, file=open('output.txt', 'a'))

    print("\nPrefix Length:",
          ip_details.prefixlen,
          file=open('output.txt', 'a'))

    print("\nHost Wild card Mask:",
예제 #17
0
 def assertStaticIPv6(self, netattrs, ipinfo):
     requires_ipaddress()
     ipv6_address = str(
         ipaddress.IPv6Interface(six.text_type(netattrs['ipv6addr']))
     )
     assert ipv6_address in ipinfo['ipv6addrs']
def get_addresses_for_iface(host_uid, iface_uid, syscat_url, logger):
    '''
    Retrieve a list of addresses for an interface.
    Return them as a list of IPv4Interface and IPv6Interface objects.
    '''
    returnval = []  # Accumulator for the result
    # IPv4 addresses
    # Extract the list of addresses for this interface
    logger.debug('Retrieving IPv4 addresses for interface %s:%s', host_uid,
                 iface_uid)
    url = '{}/raw/v1/devices/{}/Interfaces/networkInterfaces/{}/Addresses/ipv4Addresses'.format(
        syscat_url, host_uid, iface_uid)
    logger.debug('Using URL %s', url)
    addresponse = requests.get(url)
    # If we got a sensible response, assemble the list
    if addresponse.status_code == 200:
        logger.debug(
            'IPv4 addresses retrieved from Syscat for interface %s:\n%s',
            iface_uid, jsonify(addresponse.json()))
        for addr in addresponse.json():
            # Sanity-check: is this thing even valid?
            if 'uid' not in addr:
                logger.warnng(
                    'Interface lacks a UID. Somebody bypassed the API: %s',
                    addr)
                continue
            # Both UID and prefixlength -> create and add an entry for it
            elif 'uid' in addr and 'prefixlength' in addr:
                # Now create and add this entry
                logger.debug('Adding IP address %s/%s', addr['uid'],
                             addr['prefixlength'])
                returnval.append(
                    ipaddress.IPv4Interface(
                        '%s/%s' % (addr['uid'], addr['prefixlength'])))
            # Both UID and netmask -> create and add an entry for it
            elif 'uid' in addr and 'netmask' in addr:
                # Now create and add this entry
                logger.debug('Adding IP address %s/%s', addr['uid'],
                             addr['netmask'])
                returnval.append(
                    ipaddress.IPv4Interface('%s/%s' %
                                            (addr['uid'], addr['netmask'])))
            else:
                logger.error(
                    'Address had a UID, but neither a prefixlength nor a netmask: %s',
                    addr)
    # No IPv4 addresses for this interface: log this fact and carry on
    elif addresponse.status_code == 404:
        logger.debug('No IPv4 addresses found for %s:%s', host_uid, iface_uid)
    # Unexpected response code; fail noisily
    else:
        logger.error(
            'Unexpected response while querying IPv4 addresses for %s:%s - %s %s',
            host_uid, iface_uid, addresponse.status_code, addresponse.text)
        return False
    #
    # IPv6 addresses
    # Extract the list of addresses for this interface
    logger.debug('Retrieving IPv6 addresses for interface %s:%s', host_uid,
                 iface_uid)
    url = '{}/raw/v1/devices/{}/Interfaces/networkInterfaces/{}/Addresses/ipv6Addresses'.format(
        syscat_url, host_uid, iface_uid)
    logger.debug('Using URL %s', url)
    addresponse = requests.get(url)
    # If we got a sensible response, assemble the list
    if addresponse.status_code == 200:
        logger.debug(
            'IPv6 addresses retrieved from Syscat for interface %s:\n%s',
            iface_uid, jsonify(addresponse.json()))
        for addr in addresponse.json():
            # Sanity-check: is this thing even valid?
            if 'uid' not in addr:
                logger.error(
                    'Interface lacks a UID. Somebody bypassed the API: %s',
                    addr)
            # Both UID and prefixlength -> create and add an entry for it
            elif 'uid' in addr and 'prefixlength' in addr:
                # Now create and add this entry
                logger.debug('Adding IP address %s/%s', addr['uid'],
                             addr['prefixlength'])
                returnval.append(
                    ipaddress.IPv6Interface(
                        '%s/%s' % (addr['uid'], addr['prefixlength'])))
            else:
                logger.error('Address had a UID, but no prefixlength: %s',
                             addr)
    # No IPv6 addresses for this interface; log the fact
    elif addresponse.status_code == 404:
        logger.debug('No IPv6 addresses found for %s:%s', host_uid, iface_uid)
    # Unexpected response code; fail noisily
    else:
        logger.error(
            'Unexpected response while querying IPv6 addresses for %s:%s - %s %s',
            host_uid, iface_uid, addresponse.status_code, addresponse.text)
        return False
    #
    # Return what we found
    logger.debug('Full Syscat address list for %s:%s\n%s', host_uid, iface_uid,
                 jsonify(returnval))
    return returnval
예제 #19
0
 def assertStaticIPv6(self, netattrs, ipinfo):
     ipv6_address = str(ipaddress.IPv6Interface(str(netattrs['ipv6addr'])))
     assert ipv6_address in ipinfo['ipv6addrs']
예제 #20
0
    def check_interface(self, ifname, iface, ipcfg, routes):
        print('check_interface: testing '
              'ifname:{}\niface:\n{}\n\nipcfg:\n{}'.format(ifname, iface,
                                                           ipcfg))
        # FIXME: remove check?
        # initial check, do we have the correct iface ?
        print('ifname={}'.format(ifname))
        self.assertTrue(type(ipcfg) == dict, "%s is not dict" % (ipcfg))
        print("ipcfg['interface']={}".format(ipcfg['interface']))
        self.assertEqual(ifname, ipcfg['interface'])

        # check physical interface attributes (skip bond members, macs change)
        if iface['type'] in ['physical'] and 'bond-master' not in iface:
            for key in ['mac_address']:
                print("checking mac on iface: %s" % iface['name'])
                if key in iface and iface[key]:
                    self.assertEqual(iface[key].lower(),
                                     ipcfg[key].lower())

        # we can check mtu on all interfaces
        for key in ['mtu']:
            if key in iface and iface[key]:
                print("checking mtu on iface: %s" % iface['name'])
                self.assertEqual(int(iface[key]),
                                 int(ipcfg[key]))

        # check subnet related attributes
        subnets = iface.get('subnets')
        if subnets is None:
            subnets = []
        for subnet in subnets:
            config_inet_iface = None
            found_inet_iface = None
            print('validating subnet:\n%s' % subnet)
            if 'address' in subnet and subnet['address']:
                # we will create to ipaddress.IPvXInterface objects
                # one based on config, and other from collected data
                # and compare.
                config_ipstr = subnet['address']
                if 'netmask' in subnet:
                    config_ipstr += "/%s" % subnet['netmask']

                # One more bit is how to construct the
                # right Version interface, detecting on ":" in address
                # detect ipv6 or v4
                if ':' in subnet['address']:
                    # v6
                    config_inet_iface = ipaddress.IPv6Interface(config_ipstr)
                    ip_func = ipaddress.IPv6Interface
                    addresses = ipcfg.get('inet6', [])
                else:
                    # v4
                    config_inet_iface = ipaddress.IPv4Interface(config_ipstr)
                    ip_func = ipaddress.IPv4Interface
                    addresses = ipcfg.get('inet4', [])

                # find a matching
                print('found addresses: %s' % addresses)
                for ip in addresses:
                    print('cur ip=%s\nsubnet=%s' % (ip, subnet))
                    # drop /CIDR if present for matching
                    if (ip['address'].split("/")[0] ==
                       subnet['address'].split("/")[0]):
                        print('found a match!')
                        found_ipstr = ip['address']
                        if ('netmask' in subnet or '/' in subnet['address']):
                            found_ipstr += "/%s" % ip.get('prefixlen')
                        found_inet_iface = ip_func(found_ipstr)
                        print('returning inet iface')
                        break

                # check ipaddress interface matches (config vs. found)
                self.assertIsNotNone(config_inet_iface)
                self.assertIsNotNone(found_inet_iface)
                self.assertEqual(config_inet_iface, found_inet_iface)

            def __find_gw_config(subnet):
                gateways = []
                if 'gateway' in subnet:
                    gateways.append(subnet.get('gateway'))
                for route in subnet.get('routes', []):
                    gateways += __find_gw_config(route)
                # drop duplicate gateways (static routes)
                return list(set(gateways))

            # handle gateways by looking at routing table
            configured_gws = __find_gw_config(subnet)
            print('iface:%s configured_gws: %s' % (ifname, configured_gws))
            for gw_ip in configured_gws:
                self.logger.debug('found a gateway in subnet config: %s',
                                  gw_ip)
                if ":" in gw_ip:
                    route_d = routes['6']
                else:
                    route_d = routes['4']

                found_gws = [line for line in route_d.split('\n')
                             if 'UG' in line and gw_ip in line]
                self.logger.debug('found gateways in guest output:\n%s',
                                  found_gws)

                print('found_gws: %s\nexpected: %s' % (found_gws,
                                                       configured_gws))
                # we only need to check that we found at least one as we walk
                self.assertGreater(len(found_gws), 0)
                for fgw in found_gws:
                    if ":" in gw_ip:
                        (dest, gw, flags, metric, ref, use, iface) = \
                            fgw.split()
                    else:
                        (dest, gw, genmask, flags, metric, ref, use, iface) = \
                            fgw.split()
                    self.logger.debug('configured gw:%s found gw:%s',
                                      gw_ip, gw)
                    self.assertEqual(gw_ip, gw)
예제 #21
0
   def makeInterfaceConfiguration(self,
                                  interfaceName,
                                  ipv4Interface = ipaddress.IPv4Interface('0.0.0.0/0'),
                                  ipv4Gateway   = None,
                                  ipv6Interface = None,
                                  ipv6Gateway   = None,
                                  metric        = 1,
                                  pdnInterface  = None,
                                  createDummy   = False):

      # ====== Create header ================================================
      interfaceConfiguration = \
         'network:\n' + \
         '  version: 2\n' + \
         '  renderer: networkd\n'
      if createDummy == False:
        interfaceConfiguration = interfaceConfiguration + \
           '  ethernets:\n' + \
           '    ' + interfaceName + ':\n'
      else:
        interfaceConfiguration = interfaceConfiguration + \
           '  bridges:\n' + \
           '    ' + interfaceName + ':\n' + \
           '      interfaces: [ ]\n'


      # ====== Addressing ===================================================
      networks = []
      if ipv4Interface == ipaddress.IPv4Interface('0.0.0.0/0'):
         interfaceConfiguration = interfaceConfiguration + '      dhcp4: true\n'
      else:
         interfaceConfiguration = interfaceConfiguration + '      dhcp4: false\n'

      if ((ipv6Interface == None) and (ipv6Interface == ipaddress.IPv6Interface('::/0'))):
         interfaceConfiguration = interfaceConfiguration + '      dhcp6: true\n'
      else:
         interfaceConfiguration = interfaceConfiguration + '      dhcp6: false\n'
         interfaceConfiguration = interfaceConfiguration + '      accept-ra: no\n'

      if ( (ipv4Interface != ipaddress.IPv4Interface('0.0.0.0/0')) or
           ((ipv6Interface == None) and (ipv6Interface == ipaddress.IPv6Interface('::/0'))) ):

         interfaceConfiguration = interfaceConfiguration + '      addresses:\n'

         if ipv4Interface != ipaddress.IPv4Interface('0.0.0.0/0'):
            interfaceConfiguration = interfaceConfiguration + '        - ' + \
               str(ipv4Interface.ip) + '/' + \
               str(ipv4Interface.network.prefixlen) + \
               '\n'
            networks.append(ipv4Interface.network)

         if ((ipv6Interface == None) and (ipv6Interface == ipaddress.IPv6Interface('::/0'))):
            interfaceConfiguration = interfaceConfiguration + '        - ' + \
               str(ipv6Interface.ip) + '/' + \
               str(ipv6Interface.network.prefixlen) + \
               '\n'
            networks.append(ipv6Interface,network)

      # ====== Routing ======================================================
      postUpRules, preDownRules = None, None
      if ( ((ipv4Gateway != None) and (ipv4Gateway != ipaddress.IPv4Address('0.0.0.0'))) or
           ((ipv6Gateway != None) and (ipv6Gateway != ipaddress.IPv6Address('::'))) ):

         interfaceConfiguration = interfaceConfiguration + '      routes:\n'

         gateways = []
         if ((ipv4Gateway != None) and (ipv4Gateway != ipaddress.IPv4Address('0.0.0.0'))):
            interfaceConfiguration = interfaceConfiguration + \
                '       - to: 0.0.0.0/0\n' + \
                '         via: ' + str(ipv4Gateway) + '\n' + \
                '         metric: ' + str(metric) + '\n'
            gateways.append(ipv4Gateway)

         if ((ipv6Gateway != None) and (ipv6Gateway != ipaddress.IPv6Address('::'))):
            interfaceConfiguration = interfaceConfiguration + \
                '       - to: ::/0\n' + \
                '         via: ' + str(ipv6Gateway) + '\n' + \
                '         metric: ' + str(metric) + '\n'
            gateways.append(ipv6Gateway)

         if ((pdnInterface != None) and (len(networks) > 0) and (len(gateways) > 0)):
            postUpRules, preDownRules = self.makeRoutingRules(pdnInterface, interfaceName, networks, gateways)

      return [ interfaceConfiguration, postUpRules, preDownRules ]
예제 #22
0
    def __init__(self, *args, **kwargs):

        self.cm_network = ipaddress.IPv4Network(
            six.text_type(kwargs.pop('cm_network', "192.168.200.0/24")))
        self.cm_gateway = ipaddress.IPv4Address(
            six.text_type(kwargs.pop('cm_gateway', "192.168.200.1")))
        self.mta_network = ipaddress.IPv4Network(
            six.text_type(kwargs.pop('mta_network', "192.168.201.0/24")))
        self.mta_gateway = ipaddress.IPv4Address(
            six.text_type(kwargs.pop('mta_gateway', "192.168.201.1")))
        self.open_network = ipaddress.IPv4Network(
            six.text_type(kwargs.pop('open_network', "192.168.202.0/24")))
        self.open_gateway = ipaddress.IPv4Address(
            six.text_type(kwargs.pop('open_gateway', "192.168.202.1")))
        self.prov_network = ipaddress.IPv4Network(
            six.text_type(kwargs.pop('prov_network', "192.168.3.0/24")))
        self.prov_gateway = ipaddress.IPv4Address(
            six.text_type(kwargs.pop('prov_gateway', "192.168.3.222")))
        self.prov_ip = ipaddress.IPv4Address(
            six.text_type(kwargs.pop('prov_ip', "192.168.3.1")))

        self.prov_iface = ipaddress.IPv6Interface(
            six.text_type(
                kwargs.pop('prov_ipv6',
                           "2001:dead:beef:1::1/%s" % self.ipv6_prefix)))
        self.prov_ipv6, self.prov_nw_ipv6 = self.prov_iface.ip, self.prov_iface.network

        self.cm_gateway_v6_iface = ipaddress.IPv6Interface(
            six.text_type(
                kwargs.pop('cm_gateway_v6',
                           "2001:dead:beef:4::cafe/%s" % self.ipv6_prefix)))
        self.cm_gateway_v6, self.cm_network_v6 = self.cm_gateway_v6_iface.ip, self.cm_gateway_v6_iface.network
        self.cm_network_v6_start = ipaddress.IPv6Address(
            six.text_type(
                kwargs.pop('cm_network_v6_start', "2001:dead:beef:4::10")))
        self.cm_network_v6_end = ipaddress.IPv6Address(
            six.text_type(
                kwargs.pop('cm_network_v6_end', "2001:dead:beef:4::100")))
        self.open_gateway_iface = ipaddress.IPv6Interface(
            six.text_type(
                kwargs.pop('open_gateway_v6',
                           "2001:dead:beef:6::cafe/%s" % self.ipv6_prefix)))
        self.open_gateway_v6, self.open_network_v6 = self.open_gateway_iface.ip, self.open_gateway_iface.network
        self.open_network_v6_start = ipaddress.IPv6Address(
            six.text_type(
                kwargs.pop('open_network_v6_start', "2001:dead:beef:6::10")))
        self.open_network_v6_end = ipaddress.IPv6Address(
            six.text_type(
                kwargs.pop('open_network_v6_end', "2001:dead:beef:6::100")))
        self.prov_gateway_v6 = ipaddress.IPv6Address(
            six.text_type(
                kwargs.pop('prov_gateway_v6', "2001:dead:beef:1::cafe")))

        # we're storing a list of all /56 subnets possible from erouter_net_iface.
        # As per docsis, /56 must be the default pd length
        self.erouter_net_iface = ipaddress.IPv6Interface(
            six.text_type(kwargs.pop('erouter_net',
                                     "2001:dead:beef:e000::/51")))
        self.erouter_net = list(
            self.erouter_net_iface.network.subnets(
                56 - self.erouter_net_iface._prefixlen))

        self.sip_fqdn = kwargs.pop(
            'sip_fqdn', u"08:54:43:4F:4D:4C:41:42:53:03:43:4F:4D:00")
        self.time_server = ipaddress.IPv4Address(
            six.text_type(kwargs.pop('time_server', str(self.prov_ip))))
        self.timezone = self.get_timzone_offset(
            six.text_type(kwargs.pop('timezone', "UTC")))
        self.syslog_server = ipaddress.IPv4Address(
            six.text_type(kwargs.pop('syslog_server', str(self.prov_ip))))
        if 'options' in kwargs:
            options = [x.strip() for x in kwargs['options'].split(',')]
            for opt in options:
                # Not a well supported config, will go away at some point
                if opt.startswith('wan-cmts-provisioner'):
                    self.wan_cmts_provisioner = True
                    self.shared_tftp_server = True
                    # This does run one.. but it's handled via the provisioning code path
                    self.standalone_provisioner = False

        self.gw = self.prov_ip
        self.gwv6 = self.prov_ipv6
        self.nw = self.prov_network
        return super(DebianISCProvisioner, self).__init__(*args, **kwargs)
예제 #23
0
    def check_interface(self, ifname, iface, ipcfg, routes):
        print('check_interface: testing '
              'ifname:{}\niface:\n{}\n\nipcfg:\n{}'.format(
                  ifname, iface, ipcfg))
        # FIXME: remove check?
        # initial check, do we have the correct iface ?
        print('ifname={}'.format(ifname))
        self.assertTrue(type(ipcfg) == dict, "%s is not dict" % (ipcfg))
        print("ipcfg['interface']={}".format(ipcfg['interface']))
        self.assertEqual(ifname, ipcfg['interface'])

        # check physical interface attributes (skip bond members, macs change)
        if iface['type'] in ['physical'] and 'bond-master' not in iface:
            for key in ['mac_address']:
                print("checking mac on iface: %s" % iface['name'])
                if key in iface and iface[key]:
                    self.assertEqual(iface[key].lower(), ipcfg[key].lower())

        # we can check mtu on all interfaces
        for key in ['mtu']:
            if key in iface and iface[key]:
                print("checking mtu on iface: %s" % iface['name'])
                self.assertEqual(int(iface[key]), int(ipcfg[key]))

        # check subnet related attributes
        subnets = iface.get('subnets')
        if subnets is None:
            subnets = []
        for subnet in subnets:
            config_inet_iface = None
            found_inet_iface = None
            print('validating subnet:\n%s' % subnet)
            if 'address' in subnet and subnet['address']:
                # we will create to ipaddress.IPvXInterface objects
                # one based on config, and other from collected data
                # and compare.
                config_ipstr = subnet['address']
                if 'netmask' in subnet:
                    config_ipstr += "/%s" % subnet['netmask']

                # One more bit is how to construct the
                # right Version interface, detecting on ":" in address
                # detect ipv6 or v4
                if ':' in subnet['address']:
                    # v6
                    config_inet_iface = ipaddress.IPv6Interface(config_ipstr)
                    ip_func = ipaddress.IPv6Interface
                    addresses = ipcfg.get('inet6', [])
                else:
                    # v4
                    config_inet_iface = ipaddress.IPv4Interface(config_ipstr)
                    ip_func = ipaddress.IPv4Interface
                    addresses = ipcfg.get('inet4', [])

                # find a matching
                print('found addresses: %s' % addresses)
                for ip in addresses:
                    print('cur ip=%s\nsubnet=%s' % (ip, subnet))
                    # drop /CIDR if present for matching
                    if (ip['address'].split("/")[0] == subnet['address'].split(
                            "/")[0]):
                        print('found a match!')
                        found_ipstr = ip['address']
                        if ('netmask' in subnet or '/' in subnet['address']):
                            found_ipstr += "/%s" % ip.get('prefixlen')
                        found_inet_iface = ip_func(found_ipstr)
                        print('returning inet iface')
                        break

                # check ipaddress interface matches (config vs. found)
                self.assertIsNotNone(config_inet_iface)
                self.assertIsNotNone(found_inet_iface)
                self.assertEqual(config_inet_iface, found_inet_iface)

            def __find_gw_config(subnet):
                gateways = []
                if 'gateway' in subnet:
                    gateways.append(subnet.get('gateway'))
                for route in subnet.get('routes', []):
                    gateways += __find_gw_config(route)
                # drop duplicate gateways (static routes)
                return list(set(gateways))

            def __has_gw_route(line, gw_ip):
                # For Disco and older releases we match
                #    'UG' matches route_* gateways
                # For Eoan which does not contain net-tools/route commands
                #  'default' matches ip_route/ip_6_route default routes
                #  'via' matches ip_route/ip_6_route non-default gateways
                gwk = {'UG', 'default', 'via'}
                return gwk.intersection(set(line.split())) and gw_ip in line

            # handle gateways by looking at routing table
            configured_gws = __find_gw_config(subnet)
            print('iface:%s configured_gws: %s' % (ifname, configured_gws))
            for gw_ip in configured_gws:
                self.logger.debug('found a gateway in subnet config: %s',
                                  gw_ip)
                if ":" in gw_ip:
                    route_d = (routes['6'] if len(routes['6']) else
                               routes['ip_6_route_show'])
                else:
                    route_d = (routes['4'] if len(routes['4']) else
                               routes['ip_route_show'])

                found_gws = [
                    line for line in route_d.split('\n')
                    if __has_gw_route(line, gw_ip)
                ]
                self.logger.debug('found gateways in guest output:\n%s',
                                  found_gws)

                print('found_gws: %s\nexpected: %s' %
                      (found_gws, configured_gws))
                # we only need to check that we found at least one as we walk
                self.assertGreater(len(found_gws), 0)
                for fgw in found_gws:
                    fgw_split = fgw.split()
                    # in ip_route_show/ip_6_route_show we end up with
                    # lines which have one or more 'via' sections.
                    # in the ipv6 case, we see many nexthops with
                    # fe80::2, which we want to ignore. This loop
                    # lets us find the actual gateway value by
                    # walking until we find a non fe80::2 gateway
                    if 'via' in fgw_split:
                        for el_idx, elem in enumerate(fgw_split):
                            if elem == 'via':
                                if fgw_split[el_idx + 1] != "fe80::2":
                                    gw_idx = el_idx + 1
                                    break
                    else:
                        gw_idx = 1

                    self.logger.debug('configured gw:%s found gw:%s', gw_ip,
                                      fgw_split[gw_idx])
                    self.assertEqual(gw_ip, fgw_split[gw_idx])
예제 #24
0
import ipaddress

IPv4addresses = [('192.168.45.5',24),\
                 ('194.15.10.10',20),\
                 ('128.45.78.1',16),\
                 ('120.11.10.9',32),\
                 ('95.255.255.4',8),\
                 ('41.45.11.5',2),\
                 ('48.45.10.8',10),\
                 ('45.74.74.74',30),\
                 ('12.12.4.5',26),\
                 ('10.78.1.9',4)]

for address in IPv4addresses:
    IPv6prefix = address[1] + 16 * 6

    IPv6_4 = ("::FFFF:{}/{}".format(address[0], IPv6prefix))

    IPv6 = ipaddress.IPv6Interface(IPv6_4)
    print("%s/%i gets converted to %s" % (address[0], address[1], IPv6))
예제 #25
0
    def __init__(self, *args, **kwargs):
        """Instance initialization."""
        self.cm_network = ipaddress.IPv4Network(
            six.text_type(kwargs.pop("cm_network", "192.168.200.0/24")))
        self.cm_gateway = ipaddress.IPv4Address(
            six.text_type(kwargs.pop("cm_gateway", "192.168.200.1")))
        self.mta_network = ipaddress.IPv4Network(
            six.text_type(kwargs.pop("mta_network", "192.168.201.0/24")))
        self.mta_gateway = ipaddress.IPv4Address(
            six.text_type(kwargs.pop("mta_gateway", "192.168.201.1")))
        self.open_network = ipaddress.IPv4Network(
            six.text_type(kwargs.pop("open_network", "192.168.202.0/24")))
        self.open_gateway = ipaddress.IPv4Address(
            six.text_type(kwargs.pop("open_gateway", "192.168.202.1")))
        self.prov_network = ipaddress.IPv4Network(
            six.text_type(kwargs.pop("prov_network", "192.168.3.0/24")))
        self.prov_gateway = ipaddress.IPv4Address(
            six.text_type(kwargs.pop("prov_gateway", "192.168.3.222")))
        self.prov_ip = ipaddress.IPv4Address(
            six.text_type(kwargs.pop("prov_ip", "192.168.3.1")))

        self.prov_iface = ipaddress.IPv6Interface(
            six.text_type(
                kwargs.pop("prov_ipv6",
                           "2001:dead:beef:1::1/%s" % self.ipv6_prefix)))
        self.prov_ipv6, self.prov_nw_ipv6 = self.prov_iface.ip, self.prov_iface.network

        self.cm_gateway_v6_iface = ipaddress.IPv6Interface(
            six.text_type(
                kwargs.pop("cm_gateway_v6",
                           "2001:dead:beef:4::cafe/%s" % self.ipv6_prefix)))
        self.cm_gateway_v6, self.cm_network_v6 = (
            self.cm_gateway_v6_iface.ip,
            self.cm_gateway_v6_iface.network,
        )
        self.cm_network_v6_start = ipaddress.IPv6Address(
            six.text_type(
                kwargs.pop("cm_network_v6_start", "2001:dead:beef:4::10")))
        self.cm_network_v6_end = ipaddress.IPv6Address(
            six.text_type(
                kwargs.pop("cm_network_v6_end", "2001:dead:beef:4::100")))
        self.open_gateway_iface = ipaddress.IPv6Interface(
            six.text_type(
                kwargs.pop("open_gateway_v6",
                           "2001:dead:beef:6::cafe/%s" % self.ipv6_prefix)))
        self.open_gateway_v6, self.open_network_v6 = (
            self.open_gateway_iface.ip,
            self.open_gateway_iface.network,
        )
        self.open_network_v6_start = ipaddress.IPv6Address(
            six.text_type(
                kwargs.pop("open_network_v6_start", "2001:dead:beef:6::10")))
        self.open_network_v6_end = ipaddress.IPv6Address(
            six.text_type(
                kwargs.pop("open_network_v6_end", "2001:dead:beef:6::100")))
        self.prov_gateway_v6 = ipaddress.IPv6Address(
            six.text_type(
                kwargs.pop("prov_gateway_v6", "2001:dead:beef:1::cafe")))

        # we're storing a list of all /56 subnets possible from erouter_net_iface.
        # As per docsis, /56 must be the default pd length
        self.erouter_net_iface = ipaddress.IPv6Interface(
            six.text_type(kwargs.pop("erouter_net",
                                     "2001:dead:beef:e000::/51")))
        self.erouter_net = list(
            self.erouter_net_iface.network.subnets(
                56 - self.erouter_net_iface._prefixlen))

        # In order to add a route for every delegated prefix.
        # We'll maintain fix ip route for each host.
        self.erouter_fixed_ip_start = kwargs.pop("erouter_fixed_ip_start",
                                                 None)
        if self.erouter_fixed_ip_start:
            # IP syntax : <ipv6>/<prefixlen>
            self.erouter_fixed_ip_start = ipaddress.IPv6Interface(
                self.erouter_fixed_ip_start)

        self.sip_fqdn = kwargs.pop(
            "sip_fqdn",
            "09:53:49:50:43:45:4e:54:45:52:09:42:4f:41:52:44:46:41:52:4d:03:43:4F:4D:00",
        )

        self.configure_time_server = True
        self.time_server = ipaddress.IPv4Address(str(self.prov_ip))

        self.dhcp_snooping = kwargs.pop("dhcp_snooping", False)
        if self.dhcp_snooping:
            snoop_ip, snoop_port = kwargs.pop("dhcp_snooping_target").split(
                ";")
            self.snooper = spawn_ssh_pexpect(snoop_ip,
                                             prompt=self.prompt,
                                             port=snoop_port,
                                             color="yellow")
            self.snooper.sendline("echo 'snooper connected!!'")
            self.snooper.expect(self.snooper.prompt)

        time_server = kwargs.pop("time_server", None)
        if time_server:
            self.configure_time_server = False
            self.time_server = ipaddress.IPv4Address(time_server)

        self.timezone = self.get_timzone_offset(
            six.text_type(kwargs.pop("timezone", "UTC")))
        self.syslog_server = ipaddress.IPv4Address(
            six.text_type(kwargs.pop("syslog_server", str(self.prov_ip))))
        if "options" in kwargs:
            options = [x.strip() for x in kwargs["options"].split(",")]
            for opt in options:
                # Not a well supported config, will go away at some point
                if opt.startswith("wan-cmts-provisioner"):
                    self.wan_cmts_provisioner = True
                    self.shared_tftp_server = True
                    # This does run one.. but it's handled via the provisioning code path
                    self.standalone_provisioner = False

        self.gw = self.prov_ip
        self.gwv6 = self.prov_ipv6
        self.nw = self.prov_network
        super(DebianISCProvisioner, self).__init__(*args, **kwargs)
예제 #26
0
def getIp(prefix):
    return ipaddress.IPv6Interface(prefix).network.network_address
예제 #27
0
파일: vpp_format.py 프로젝트: zhongyoub/vpp
def unformat_api_ip6_address_with_prefix_t(o):
    return ipaddress.IPv6Interface((o.address, o.len))
예제 #28
0
def remove_duplicates(raw_file):
    addrs = raw_file.readlines()
    for i in range(len(addrs)):
        addrs[i] = ipaddress.IPv6Interface(addrs[i][:-1])
    uniques = set(addrs)
    return uniques
예제 #29
0
 def make_IPv6Interface(self, spec):
     prefix = randrange(128)
     return ipaddress.IPv6Interface(
         (bytes(randrange(256) for _ in range(16)), prefix)
     )
예제 #30
0
class Fixture:
    T = 123
    T_INT = 123
    T_INT_STR = 123
    ANY = 123
    INT = 123
    FLOAT = 1.23
    BOOL = True
    LIST = [1, 2, 3]
    TUPLE = (1, )
    TUPLE_DUMPED = [1]
    DEQUE = collections.deque([1, 2, 3])
    SET = {1, 2, 3}
    FROZEN_SET = frozenset([1, 2, 3])
    CHAIN_MAP = collections.ChainMap({"a": 1, "b": 2}, {"c": 3, "d": 4})
    MAPS_LIST = [{"a": 1, "b": 2}, {"c": 3, "d": 4}]
    DICT = {"a": 1, "b": 2}
    ORDERED_DICT = collections.OrderedDict(a=1, b=2)
    COUNTER: Counter[str] = collections.Counter(a=1, b=2)
    BYTES = b"123"
    BYTES_BASE64 = "MTIz\n"
    BYTE_ARRAY = bytearray(b"123")
    STR = "123"
    ENUM = MyEnum.a
    INT_ENUM = MyIntEnum.a
    STR_ENUM = MyStrEnum.a
    FLAG = MyFlag.a
    INT_FLAG = MyIntFlag.a
    DATA_CLASS = MyDataClass(a=1, b=2)
    T_DATA_CLASS = MyDataClass(a=1, b=2)
    DATA_CLASS_WITH_UNION = MyDataClassWithUnion(a=1, b=2)
    NONE = None
    DATETIME = datetime(2018, 10, 29, 12, 46, 55, 308495)
    DATETIME_STR = "2018-10-29T12:46:55.308495"
    DATE = DATETIME.date()
    DATE_STR = "2018-10-29"
    TIME = DATETIME.time()
    TIME_STR = "12:46:55.308495"
    TIMEDELTA = timedelta(3.14159265358979323846)
    TIMEZONE = timezone(timedelta(hours=3))
    UUID = uuid.UUID("3c25dd74-f208-46a2-9606-dd3919e975b7")
    UUID_STR = "3c25dd74-f208-46a2-9606-dd3919e975b7"
    IP4ADDRESS_STR = "127.0.0.1"
    IP4ADDRESS = ipaddress.IPv4Address(IP4ADDRESS_STR)
    IP6ADDRESS_STR = "::1"
    IP6ADDRESS = ipaddress.IPv6Address(IP6ADDRESS_STR)
    IP4NETWORK_STR = "127.0.0.0/8"
    IP4NETWORK = ipaddress.IPv4Network(IP4NETWORK_STR)
    IP6NETWORK_STR = "::/128"
    IP6NETWORK = ipaddress.IPv6Network(IP6NETWORK_STR)
    IP4INTERFACE_STR = "192.168.1.1/24"
    IP4INTERFACE = ipaddress.IPv4Interface(IP4INTERFACE_STR)
    IP6INTERFACE_STR = "::1/128"
    IP6INTERFACE = ipaddress.IPv6Interface(IP6INTERFACE_STR)
    DECIMAL = decimal.Decimal("1.33")
    DECIMAL_STR = "1.33"
    FRACTION = fractions.Fraction("1/3")
    FRACTION_STR = "1/3"
    MUTABLE_STRING = MutableString(STR)
    MUTABLE_STRING_STR = STR
    CUSTOM_PATH = CustomPath("/a/b/c")
    CUSTOM_PATH_STR = "/a/b/c"
    CUSTOM_SERIALIZE = "_FOOBAR_"
    GENERIC_SERIALIZABLE_LIST_INT = GenericSerializableList([1, 2, 3])
    GENERIC_SERIALIZABLE_LIST_STR = GenericSerializableList(["a", "b", "c"])