def networkToEndIp(network):
    end = IPv4Network(network)[-1]
    return int(end)
Esempio n. 2
0
    def summary_and_un_categorized_sheet_processing(self, temp_df):
        """
        This method takes a copy of the full dataframe.  Processes it and
        returns the mashed data for writing the Summary and Uncategorized
        Sheet.
        """

        # Building out dataset for summary and uncategorized processing.
        temp_df = temp_df.loc[
            # (temp_df['/Cidr'] != 32) &
            (temp_df['network'] != '100.88.0.0/29') &
            (temp_df['network'] != '100.64.0.0/29') &
            (temp_df['network_view'] != 'Public-IP') &
            (temp_df['network_view'] != 'CDSTEST') &
            (temp_df['network_view'] != 'IPR-HMB')]
        temp_df = temp_df[~temp_df['/Cidr'].isin(
            [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])]
        temp_df = temp_df[
            ~temp_df['extattrs_IPR Designation_value'].str.contains(
                'leaf', na=False)]
        temp_df = temp_df[
            ~temp_df['extattrs_IPR Designation_value'].str.contains(
                'retired', na=False)]
        temp_df = temp_df[
            ~temp_df['extattrs_IPR Designation_value'].str.contains(
                'dup', na=False)]
        temp_df = temp_df[
            ~temp_df['extattrs_IPR Designation_value'].str.contains(
                'ignore', na=False)]
        temp_df = temp_df[
            ~temp_df['extattrs_IPR Designation_value'].str.contains(
                'divest', na=False)]
        temp_df = temp_df[
            ~temp_df['extattrs_IPR Designation_value'].str.contains(
                'drop reserve', na=False)]
        temp_df = temp_df[
            ~temp_df['extattrs_IPR Designation_value'].str.contains(
                'parent', na=False)]
        temp_df = temp_df[
            ~temp_df['extattrs_IPR Designation_value'].str.contains(
                'decom', na=False)]

        # Convert the dataframe to a dictionary with the index as keys.
        temp_dict = temp_df.to_dict('index')

        # Builds dictionaries for the data to be stored in.
        temp_summary_dict = {}
        temp_uncategorized_dict = {}

        """
        Builds the two dictionaries based on private or cgn matching 
        requirements.  If a subnet is is neither a private or cgn ip address
        it is considered un-categorized.

        An extension was created for the ipaddr module located in the Builder
        directory.  Needed to do this in order to create a carrier grade nat
        filter.
        """
        for key in temp_dict.keys():
            if IPv4Network(temp_dict[key]['network']).is_private:
                temp_summary_dict.update({key: temp_dict[key]})
                continue
            elif Cgn.is_cgn(IPv4Network(temp_dict[key]['network'])):
                temp_summary_dict.update({key: temp_dict[key]})
                continue
            else:
                temp_uncategorized_dict.update({key: temp_dict[key]})

        # Build dataframes from the newly created dictionaries.
        summary_df = pd.DataFrame.from_dict(temp_summary_dict, orient='index')
        uncategorized_df = pd.DataFrame.from_dict(
            temp_uncategorized_dict, orient='index')

        # Dataframe cleanup prior to conflict and overlap check.
        header_dict = self.env_cls.header_row_dict()
        summary_df.rename(columns=header_dict, inplace=True)
        summary_df = summary_df.reset_index()
        del summary_df['index']
        summary_df['Index'] = summary_df.index + 10001

        # Overlap and Conflict Check against the master_df dataframe.
        summary_overlaps, summary_conflicts = \
            self.conflict_overlap_check(summary_df)

        # Updates the summary dataframe with the overlap and conflict data.
        summary_df = self.update_df_conflict_overlap_data(summary_overlaps,
                                                          summary_conflicts,
                                                          summary_df)

        return summary_df, uncategorized_df
Esempio n. 3
0
 def prefixlen(self, arg):
     """prefixlen setter method"""
     self.network_object = IPv4Network("{0}/{1}".format(
         str(self.ip_object), arg),
                                       strict=False)
Esempio n. 4
0
            # We have 3 cases regarding aqnet/qipinfo:
            # - One contains the other: this is a split or a merge
            # - aqnet.ip < qipinfo.address.ip (or there is no qipinfo): the
            #   network was deleted from QIP
            # - qipinfo.address.ip < aqnet.ip (or there is no aqnet): a new
            #   network was added to QIP
            if aqnet and qipinfo and (aqnet.ip in qipinfo.address
                                      or qipinfo.address.ip in aqnet.network):
                # This is a split or a merge. The trick here is to perform
                # multiple network additions/deletions inside the same
                # transaction even in incremental mode, to maintain relational
                # integrity

                startip = min(aqnet.ip, qipinfo.address.ip)
                prefixlen = min(aqnet.cidr, qipinfo.address.prefixlen)
                supernet = IPv4Network("%s/%s" % (startip, prefixlen))
                # We may modify aqnet.network below, so save the original value
                orig_net = aqnet.network

                # Always deleting & possibly recreating aqnet would make things
                # simpler, but we can't do that due to the unique constraint on
                # the IP address and the non-null foreign key constraints in
                # other tables. So we need a flag to remember if we want to keep
                # the original object or not
                if aqnet.ip == qipinfo.address.ip:
                    self.logger.client_info("Setting network {0:a} prefix "
                                            "length to {1}".format(
                                                aqnet,
                                                qipinfo.address.prefixlen))
                    aqnet.cidr = qipinfo.address.prefixlen
                    keep_aqnet = True
Esempio n. 5
0
    def __init__(self,
                 nonce,
                 category,
                 to,
                 afi,
                 value,
                 metadata=b'',
                 time=0,
                 v=0,
                 r=0,
                 s=0):

        if category == 0 or category == 1:
            if metadata != b'':
                raise InvalidTransaction("Invalid Metadata")
            metadata = b''
        elif category == 2:
            if type(metadata) == list and len(metadata) % 3 == 0:
                _metadata = []
                _afi = 0
                if type(metadata[0]) == bytes:
                    _bytes = True
                elif type(metadata[0]) == int:
                    _bytes = False
                else:
                    raise InvalidTransaction("Invalid Metadata")
                i = 0
                while i < len(metadata):
                    try:
                        if _bytes:
                            _afi = bytes_to_int(metadata[i])
                            _metadata.append(metadata[i])
                        else:
                            _afi = metadata[i]
                            _metadata.append(encode_int8(metadata[i]))
                        if _afi != 1 and _afi != 2:
                            raise InvalidTransaction("Invalid Metadata AFI")
                    except:
                        raise InvalidTransaction("Invalid Metadata AFI")
                    try:
                        if _bytes:
                            if _afi == 1:
                                ip = IPv4Address(Bytes(metadata[i + 1]))
                            else:
                                ip = IPv6Address(Bytes(metadata[i + 1]))
                            _metadata.append(bytes(ip.packed))
                            addr = normalize_address(metadata[i + 2],
                                                     allow_blank=True)
                            _metadata.append(addr)
                        else:
                            if _afi == 1:
                                ip = IPv4Address(metadata[i + 1])
                            else:
                                ip = IPv6Address(metadata[i + 1])
                            _metadata.append(bytes(ip.packed))
                            addr = normalize_address(metadata[i + 2],
                                                     allow_blank=True)
                            _metadata.append(addr)
                        i += 3
                    except:
                        raise InvalidTransaction("Invalid Metadata")
                metadata = _metadata
            else:
                raise InvalidTransaction("Invalid Metadata")

        elif category == 3:
            if type(metadata) == list and len(metadata) % 4 == 0:
                _metadata = []
                _afi = 0
                if type(metadata[0]) == bytes:
                    _bytes = True
                elif type(metadata[0]) == int:
                    _bytes = False
                else:
                    raise InvalidTransaction("Invalid Metadata")
                i = 0
                while i < len(metadata):
                    try:
                        if _bytes:
                            _afi = bytes_to_int(metadata[i])
                            _metadata.append(metadata[i])
                        else:
                            _afi = metadata[i]
                            _metadata.append(encode_int8(metadata[i]))
                        if _afi != 1 and _afi != 2:
                            raise InvalidTransaction("Invalid Metadata AFI")
                    except:
                        raise InvalidTransaction("Invalid Metadata AFI")
                    try:
                        if _bytes:
                            if _afi == 1:
                                ip = IPv4Address(Bytes(metadata[i + 1]))
                            else:
                                ip = IPv6Address(Bytes(metadata[i + 1]))
                            _metadata.append(bytes(ip.packed))
                            priority = bytes_to_int(metadata[i + 2])
                            if priority < 0 or priority > 255:
                                raise InvalidTransaction(
                                    "Invalid Metadata Priority")
                            _metadata.append(int_to_bytes(priority))
                            weight = bytes_to_int(metadata[i + 3])
                            if weight < 0 or weight > 255:
                                raise InvalidTransaction(
                                    "Invalid Metadata Weight")
                            _metadata.append(int_to_bytes(weight))
                        else:
                            if _afi == 1:
                                ip = IPv4Address(metadata[i + 1])
                            else:
                                ip = IPv6Address(metadata[i + 1])
                            _metadata.append(bytes(ip.packed))
                            priority = metadata[i + 2]
                            if priority < 0 or priority > 255:
                                raise InvalidTransaction(
                                    "Invalid Metadata Priority")
                            _metadata.append(int_to_bytes(priority))
                            weight = metadata[i + 3]
                            if weight < 0 or weight > 255:
                                raise InvalidTransaction(
                                    "Invalid Metadata Weight")
                            _metadata.append(int_to_bytes(weight))
                        i += 4
                    except:
                        raise InvalidTransaction("Invalid Metadata")
                metadata = _metadata
            else:
                raise InvalidTransaction("Invalid Metadata")
        else:
            raise InvalidTransaction("Invalid Category")

        to = normalize_address(to, allow_blank=True)

        if afi != 1 and afi != 2:
            raise InvalidTransaction("Invalid AFI")

        try:
            if afi == 1:
                ipnet = IPv4Network(value)
            else:
                ipnet = IPv6Network(value)
        except:
            if len(value) == 5:
                try:
                    ip = IPv4Address(Bytes(value[:4]))
                    ipnet = IPv4Network(
                        str(ip) + '/' + str(bytes_to_int(value[4])))
                except:
                    raise InvalidTransaction("Invalid Value")
            elif len(value) == 17:
                try:
                    ip = IPv6Address(Bytes(value[:16]))
                    ipnet = IPv6Network(
                        str(ip) + '/' + str(bytes_to_int(value[16])))
                except:
                    raise InvalidTransaction("Invalid Value")
            else:
                raise InvalidTransaction("Invalid Value")
        value = bytes(ipnet.packed) + encode_int8(ipnet.prefixlen)

        super(Transaction, self).__init__(nonce, category, to, afi, value,
                                          metadata, time, v, r, s)
Esempio n. 6
0
def convert_file(interfaces, acls, acl_conds, nats, routes):
    used_acls = set()
    output = ""

    local_addresses = [
        ifc.subnet.ip for _, ifc in interfaces.items() if ifc.subnet
    ]

    # Get default route
    ifcs = filter(lambda (n, ifc): ifc.subnet, interfaces.items())
    try:
        default_route = [r for r in routes if r.subnet.mask == '0.0.0.0'
                         ][0]  # Only the first one
        for _, ifc in ifcs:
            if IPv4Address(default_route.gateway) in IPv4Network(
                    str(ifc.subnet)):
                ifc.subnet.mask = '0.0.0.0'
                break
        else:
            print "<!> Warning: Cannot find interface for the default gateway!"
    except IndexError:
        print "<!> Warning: no default route!"

    ifcs = sorted(ifcs, key=lambda (_, ifc): IPv4Network(ifc.subnet).numhosts)

    # Interfaces switch/case
    for chain, acldir, variable in [('InF', 'in', 'srcIp'),
                                    ('OutF', 'out', 'dstIp')]:
        output += "CHAIN {} DROP:\n".format(chain)
        for _, ifc in ifcs:
            if ifc.acls:
                dir_acls = filter(lambda (n, d): d == acldir, ifc.acls)
                for name, direction in dir_acls:
                    if name not in acls:
                        output += "({} == {}, ACCEPT)\n".format(
                            variable, ifc.subnet)
                    else:
                        output += "({} == {}, GOTO(ACL_{}))\n".format(
                            variable, ifc.subnet, name)
                        used_acls.add(name)
            else:
                output += "({} == {}, ACCEPT)\n".format(variable, ifc.subnet)
        output += "\n"

    # NATs
    inside_subnets = [
        ifc.subnet for _, ifc in ifcs if ifc.nat and ifc.nat == 'inside'
    ]
    outside_subnets = [
        ifc.subnet for _, ifc in ifcs if ifc.nat and ifc.nat == 'outside'
    ]

    nats_inside = [n for n in nats if n.direction == 'inside']
    nats_outside = [n for n in nats if n.direction == 'outside']

    rules_inside = []
    rules_outside = []

    def nat_match(nat, outside=False):
        if outside:
            # TODO: support NAT outside
            raise RuntimeError("NAT outside is not currently implemented.")

        # not static
        if nat.list_id:
            acl = acl_conds[nat.list_id]
            cond_in = [' || '.join('({})'.format(c) for c in acl)]
            cond_in.append('not({})'.format(
                ' || '.join('(dstIp == {})'.format(s) for s in inside_subnets +
                            map(lambda x: x.ip, outside_subnets))))
            target_ip = interfaces[nat.if_name].subnet.ip
            target_in = 'NAT(Id, {})'.format(target_ip)
            rules_inside.append('({}, {})'.format(' && '.join(cond_in),
                                                  target_in))
            rules_outside.insert(0, '(state == 1, CHECK-STATE(->))')
        # static
        else:
            conds_in = []
            conds_out = []
            target_in = 'NAT(Id, {}:{})'.format(
                nat.dst_ip,
                'Id' if nat.dst_port == nat.src_port else nat.dst_port)
            target_out = 'NAT({}:{}, Id)'.format(
                nat.src_ip,
                'Id' if nat.dst_port == nat.src_port else nat.src_port)
            if nat.proto:
                conds_in.append('protocol == {}'.format(nat.proto))
                conds_out.append('protocol == {}'.format(nat.proto))
            conds_in.append('srcIp == {}'.format(nat.src_ip))
            conds_out.append('dstIp == {}'.format(nat.dst_ip))
            if nat.proto and nat.src_port and nat.dst_port:
                conds_in.append('srcPort == {}'.format(nat.src_port))
                conds_out.append('dstPort == {}'.format(nat.dst_port))
            conds_in.append('not({})'.format(
                ' || '.join('(dstIp == {})'.format(s) for s in inside_subnets +
                            map(lambda x: x.ip, outside_subnets))))
            conds_out.append('not({})'.format(
                ' || '.join('(srcIp == {})'.format(s) for s in inside_subnets +
                            map(lambda x: x.ip, outside_subnets))))
            rules_inside.append('({}, {})'.format(' && '.join(conds_in),
                                                  target_in))
            rules_outside.append('({}, {})'.format(' && '.join(conds_out),
                                                   target_out))

    for n in nats_inside:
        nat_match(n)
    for n in nats_outside:
        nat_match(n, outside=True)

    # output nats
    output += 'CHAIN InN ACCEPT:\n'
    output += '\n'.join(rules_inside)
    output += '\n(true, ACCEPT)\n\n'

    output += 'CHAIN OutN ACCEPT:\n'
    output += '\n'.join(rules_outside)
    output += '\n(true, ACCEPT)\n\n'

    # ACLs
    for name, acl in acls.items():
        if name in used_acls:
            output += "CHAIN ACL_{} DROP:\n".format(name)
            for rule in acl:
                output += str(rule) + "\n"
            output += "(true, DROP)\n\n"

    return output
Esempio n. 7
0
 def test_getting_ips(self):
     self.assertEquals('10.1.0.254', str(IPv4Network('10.1.0.0/24')[-2]))
Esempio n. 8
0
 class dummy_subnet2:
     address = IPv4Network("192.168.1.0/24")
     reserved_addresses = 10
     vlan = VLANData.vlan_dummy2
Esempio n. 9
0
 def ipv4_addr_object(self):
     """Return an IPv4Network object representing the address on this interface; if there is no address, return IPv4Network('127.0.0.1/32')"""
     try:
         return IPv4Network('%s/%s' % (self.ipv4_addr, self.ipv4_netmask))
     except:
         return self.default_ipv4_addr_object
Esempio n. 10
0
    def render(self, session, dbuser, ip, netmask, prefixlen,
               network_environment, **arguments):
        if netmask:
            # There must me a faster way, but this is the easy one
            net = IPv4Network("127.0.0.0/%s" % netmask)
            prefixlen = net.prefixlen
        if prefixlen < 8 or prefixlen > 32:
            raise ArgumentError("The prefix length must be between 8 and 32.")

        dbnet_env = NetworkEnvironment.get_unique_or_default(
            session, network_environment)
        self.az.check_network_environment(dbuser, dbnet_env)

        dbnetwork = get_net_id_from_ip(session,
                                       ip,
                                       network_environment=dbnet_env)

        if prefixlen <= dbnetwork.cidr:
            raise ArgumentError("The specified --prefixlen must be bigger "
                                "than the current value.")

        subnets = dbnetwork.network.subnet(new_prefix=prefixlen)

        # Collect IP addresses that will become network/broadcast addresses
        # after the split
        bad_ips = []
        for subnet in subnets:
            bad_ips.append(subnet.ip)
            bad_ips.append(subnet.broadcast)

        q = session.query(AddressAssignment.ip)
        q = q.filter_by(network=dbnetwork)
        q = q.filter(AddressAssignment.ip.in_(bad_ips))
        used_addrs = q.all()
        if used_addrs:
            raise ArgumentError(
                "Network split failed, because the following "
                "subnet IP and/or broadcast addresses are "
                "assigned to hosts: %s" %
                ", ".join([str(addr.ip) for addr in used_addrs]))

        q = session.query(ARecord.ip)
        q = q.filter_by(network=dbnetwork)
        q = q.filter(ARecord.ip.in_(bad_ips))
        used_addrs = q.all()
        if used_addrs:
            raise ArgumentError(
                "Network split failed, because the following "
                "subnet IP and/or broadcast addresses are "
                "registered in the DNS: %s" %
                ", ".join([str(addr.ip) for addr in used_addrs]))

        # Reason of the initial value: we keep the name of the first segment
        # (e.g.  "foo"), and the next segment will be called "foo_2"
        name_idx = 2

        dbnets = []
        for subnet in dbnetwork.network.subnet(new_prefix=prefixlen):
            # Skip the original
            if subnet.ip == dbnetwork.ip:
                continue

            # Generate a new name. Make it unique, even if the DB does not
            # enforce that currently
            while True:
                # TODO: check if the new name is too long
                name = "%s_%d" % (dbnetwork.name, name_idx)
                name_idx += 1
                q = session.query(Network)
                q = q.filter_by(network_environment=dbnet_env)
                q = q.filter_by(name=name)
                if q.count() == 0:
                    break

                # Should not happen...
                if name_idx > 1000:  # pragma: no cover
                    raise AquilonError(
                        "Could not generate a unique network "
                        "name in a reasonable time, bailing out")

            # Inherit location & side from the supernet
            newnet = Network(
                name=name,
                network=subnet,
                network_environment=dbnet_env,
                location=dbnetwork.location,
                side=dbnetwork.side,
                comments="Created by splitting {0:a}".format(dbnetwork))
            session.add(newnet)
            dbnets.append(newnet)

        dbnetwork.cidr = prefixlen
        session.flush()

        for newnet in dbnets:
            fix_foreign_links(session, dbnetwork, newnet)

        session.flush()
Esempio n. 11
0
 def destination(self):
     # TODO: cache the IPv4Network object
     return IPv4Network("%s/%s" % (self.dest_ip, self.dest_cidr))
Esempio n. 12
0
        'p3': 'D',
        'p4': 'C',
        'p5': 'C',
        'p6': 'C'
    }
}

peer_groups = {'pg1': [1, 2, 3, 4]}
VNH_2_IP = {'VNH': list(IPNetwork('172.0.0.1/28'))}
VNH_2_mac = {'VNH': 'AA:00:00:00:00:00'}
#VNH_2_IP={'VNHB':'172.0.0.126','VNHC':'172.0.0.151','VNHA':'172.0.0.101','VNHD':'172.0.0.176'}
#VNH_2_mac={'VNHA':'A1:A1:A1:A1:A1:00','VNHC':'C1:C1:C1:C1:C1:00','VNHB':'B1:B1:B1:B1:B1:00',
#               'VNHD':'D1:D1:D1:D1:D1:00'}

prefixes = {
    'p1': IPv4Network('11.0.0.0/24'),
    'p2': IPv4Network('12.0.0.0/24'),
    'p3': IPv4Network('13.0.0.0/24'),
    'p4': IPv4Network('14.0.0.0/24'),
    'p5': IPv4Network('15.0.0.0/24'),
    'p6': IPv4Network('16.0.0.0/24')
}
port_2_participant = {1: 'A', 2: 'B', 21: 'B', 22: 'B', 3: 'C', 4: 'D'}


class SDX(object):
    """Represent a SDX platform configuration"""
    def __init__(self):
        self.participants = []
        self.sdx_ports = {}
        self.participant_id_to_in_var = {}
Esempio n. 13
0
        asns = asns.split()
        assert asns

    except Exception:
        if DEBUG: print_exc()
        sys.exit(1)

    asns = [asn.replace('AS', '') for asn in asns]

    nets = list()
    collapsed = None
    for asn in asns:
        try:
            result = db.get_as_prefixes(asn)
            if result:
                for net in result:
                    if IPNetwork(net).version == 4 and IPNetwork(
                            net).version == af:
                        nets.append(IPv4Network(net))
                    if IPNetwork(net).version == 6 and IPNetwork(
                            net).version == af:
                        nets.append(IPv6Network(net))

            collapsed = [str(net) for net in collapse_address_list(nets)]

        except Exception:
            if DEBUG: print_exc()
            pass

    if collapsed: print '\n'.join(collapsed)
Esempio n. 14
0
## Pyretic-specific imports
from pyretic.lib.corelib import *
from pyretic.lib.std import *

## SDX-specific imports
from pyretic.sdx.lib.language import *

## Generic imports
from ipaddr import IPv4Network

##
## SDX high-level policy, written by the participant
##
A_policy = ((match(dstport=80) >> match(srcport=22) >> fwd(2)) +
            (match(dstport=6666) >> fwd(3)) +
            (match_prefixes_set({IPv4Network('11.0.0.0/8')}) >> fwd(2)))
##
## Data structures. Eventually, will be filled dynamically trough ExaBGP
##
participants_announcements = {
    1: {
        2: [
            IPv4Network('11.0.0.0/8'),
            IPv4Network('12.0.0.0/8'),
            IPv4Network('14.0.0.0/8')
        ],
        3: [
            IPv4Network('11.0.0.0/8'),
            IPv4Network('12.0.0.0/8'),
            IPv4Network('13.0.0.0/8')
        ]
Esempio n. 15
0
 class dummy_subnet2:
     address = IPv4Network("192.168.1.0/24")
     reserved_addresses_bottom = 10
     reserved_addresses_top = 2
     vlan = VLANData.vlan_dummy2
Esempio n. 16
0
 def __init__(self, *args, **kwargs):
     super(BaseIOSIntfLine, self).__init__(*args, **kwargs)
     self.ifindex = None  # Optional, for user use
     self.default_ipv4_addr_object = IPv4Network('127.0.0.1/32')
Esempio n. 17
0
 class dummy_subnet4(dummy_subnet2):
     address = IPv4Network("192.168.3.0/24")
Esempio n. 18
0
    def _ip_in_subnets(self, ip, subnets):
        for s in subnets:
            if ip in IPv4Network(s):
                return True

        return False
Esempio n. 19
0
    def _add_iface(self, iface):
        cnx = DockerConnexion(self.host)
        """Add another interface to the docker container"""
        prefix = "ssh %s" % self.host if self.host != 'localhost' else ''
        br, eth, ip = iface
        # ip addr show docker0 -> check the bridge presence
        cmd = "%s ip addr show %s" % (prefix, br)
        _LOGGER.debug("Trying to execute: %s", cmd)
        p = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE, shell=True)
        (ip_info, stderr) = p.communicate()
        if p.returncode != 0:
            _LOGGER.error(stderr)
            raise AddIfaceException(stderr, br)
        match = re.search("inet\s+([^\s]+)\s", ip_info)
        if match:
            br_ip = match.groups()[0]
            IPv4Network(br_ip)
        else:
            raise AddIfaceException("Cannot find ip for bridge %s" % br, br)
        # test if it's an ovs bridge
        rc = sp.call("%s ovs-vsctl br-exists %s &> /dev/null" % (prefix, br),
                     shell=True)
        if rc == 0:
            # It's an ovs bridge
            cmd = "%s ovs-docker add-port %s %s %s" % (prefix, br, eth,
                                                       self.name)
            cmd += " --ipaddress=%s" % ip if ip != "dhcp" else ""

            _LOGGER.debug("Trying to execute: %s", cmd)
            p = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE, shell=True)
            (_, stderr) = p.communicate()
            if p.returncode != 0:
                _LOGGER.error(stderr)
                raise AddIfaceException(stderr, br)
        else:
            # it's a system bridge

            # Get the pid of the container
            cmd = "docker inspect -f '{{.State.Pid}}' %s" % self.name
            (rc, out, err) = cnx.launch(cmd)
            if rc != 0:
                _LOGGER.error(err)
                raise AddIfaceException(err, br)
            pid = out.strip()
            if_a_name = "v%spl%s" % (eth, pid)
            if_b_name = "v%spg%s" % (eth, pid)
            cmd = 'mkdir -p /var/run/netns \n ' + \
                  'ln -s /proc/{pid}/ns/net /var/run/netns/{pid} \n ' + \
                  'ip link add {a_if} type veth peer name {b_if} \n' + \
                  'brctl addif %s {a_if} \n' % br + \
                  'ip link set {a_if} up \n' + \
                  'ip link set {b_if} netns {pid} \n' + \
                  'ip netns exec {pid} ip link set dev {b_if} name %s \n' % eth + \
                  'ip netns exec {pid} ip link set %s up\n' % eth + \
                  'rm -f /var/run/netns/{pid}'
            if self.host != 'localhost':
                cmd = 'ssh -T %s <<ENDSSH \n ' % self.host + \
                      '%s \n' % cmd + \
                      'ENDSSH'
            cmd = cmd.format(pid=pid, a_if=if_a_name, b_if=if_b_name)
            _LOGGER.debug("Trying to execute: %s", cmd)
            p = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE, shell=True)
            (_, stderr) = p.communicate()
            if p.returncode != 0:
                _LOGGER.error(stderr)
                raise AddIfaceException(stderr, br)
Esempio n. 20
0

try:
    db = sql.connect(dbfile)
    dbcur = db.cursor()
    db.execute(
        '''CREATE TABLE IF NOT EXISTS devices (sin_addr TEXT, status_word TEXT,
               extra_array TEXT, vendor_id INTEGER, 
                product_name TEXT, sin_port INTEGER,
               state INTEGER, version INTEGER, device_type INTEGER, sin_family INTEGER,
               serial_number INTEGER, prodct_code INTEGER, product_revision INTEGER)
    ''')
except:
    print "Unable to connect or create device database. Do you have write permissions on this directory?"
print("Ethernet Industrial Protocol Subnet Scanner\n\n\n\n")

mode = None
while True:
    mode = raw_input(
        " 1.Scan a New Subnet\n 2.Re-Scan Previous Subnet \n 3.Visualize previously scanned Subnet \n Slection:"
    )
    if mode == '': quit()
    if mode == '1':

        while True:
            targetnetwork = raw_input(
                "Please enter the network in the following format: network/mask (i.e. 192.168.1.0/24"
            )
            if IPv4Network(targetnetwork): newscan(targetnetwork)
            else: break
Esempio n. 21
0
def _filter_data(file):
    rddi = open_workbook(file)
    rddifirst_sheet = rddi.sheet_by_index(0)

    omc_it_parent_list = list({
        "CDS - Guest Range 1", "PROD WEST", "MGMT WEST", "VOICE WEST",
        "NONAGENCY West", "NONAGENCY EAST", "AGENCY WEST", "Agency West",
        "PROD EAST", "MGMT EAST", "VOICE EAST", "HUB EAST",
        "Agency East Space", "Administrative Container", "AGENCY EMEA SPACE",
        "EMEA - Users @ Bankside",
        'Upper Range Assigned for "other" EMEA sites', "PROD EMEA",
        "MGMT EMEA", "EMEA NON-AGENCY SPACE", "Administrative Container",
        "AGENCY EAST", "VOICE EMEA", "Administrative Container", "AGENCY WEST"
    })

    #  For filtering out data not needed.
    ddi_dict = {
        'MASTER': [],
        'Filt-Cidr-32': [],
        'Filt-100.88-Cidr-29': [],
        'Filt-100.64-Cidr-29': [],
        'Filt-Free-ip': [],
        'Filt-Cidr-15-to-Cidr-1': [],
        'Filt-Public-ip-View': [],
        'Filt-Wan_test-View': [],
        'Filt-OMC-IT-Parent-Subnet': [],
        'Filt-Leaf': [],
        'Filt-Dup': [],
        'Filt-Ignore': [],
        'Filt-Uncategorized': [],
        'Filt-Divest': [],
        'Full-Dataset': [],
        'Filt-Re-IP': [],
        'Filt-Drop Reserve': []
    }
    for i in range(rddifirst_sheet.nrows):
        if i == 0:
            continue
        ddi_dict['Full-Dataset'].append(rddifirst_sheet.row_values(i))
        if '/32' in rddifirst_sheet.row_values(i)[2]:
            ddi_dict['Filt-Cidr-32'].append(rddifirst_sheet.row_values(i))
            continue
        if 'leaf' in rddifirst_sheet.row_values(i)[20]:
            ddi_dict['Filt-Leaf'].append(rddifirst_sheet.row_values(i))
            continue
        if 'dup' in rddifirst_sheet.row_values(i)[20]:
            ddi_dict['Filt-Dup'].append(rddifirst_sheet.row_values(i))
            continue
        if 'ignore' in rddifirst_sheet.row_values(i)[20]:
            ddi_dict['Filt-Ignore'].append(rddifirst_sheet.row_values(i))
            continue
        if 're-ip' in rddifirst_sheet.row_values(i)[20]:
            ddi_dict['Filt-Re-IP'].append(rddifirst_sheet.row_values(i))
            continue
        if 'drop reserve' in rddifirst_sheet.row_values(i)[20]:
            ddi_dict['Filt-Drop Reserve'].append(rddifirst_sheet.row_values(i))
            continue
        if '100.88.0.0/29' in rddifirst_sheet.row_values(i)[3]:
            ddi_dict['Filt-100.88-Cidr-29'].\
                append(rddifirst_sheet.row_values(i))
            continue
        if '100.64.0.0/29' in rddifirst_sheet.row_values(i)[3]:
            ddi_dict['Filt-100.64-Cidr-29'].\
                append(rddifirst_sheet.row_values(i))
            continue
        if 'free ip' in rddifirst_sheet.row_values(i)[5].lower() or \
            'OPEN' in rddifirst_sheet.row_values(i)[5]:
            ddi_dict['Filt-Free-ip'].\
                append(rddifirst_sheet.row_values(i))
            continue
        if rddifirst_sheet.row_values(i)[5] in omc_it_parent_list:
            ddi_dict['Filt-OMC-IT-Parent-Subnet'].append(
                rddifirst_sheet.row_values(i))
            continue
        if int(rddifirst_sheet.row_values(i)[2][1:3]) in range(1, 16):
            ddi_dict['Filt-Cidr-15-to-Cidr-1'].\
                append(rddifirst_sheet.row_values(i))
            continue
        if rddifirst_sheet.row_values(i)[4] == 'Public-IP':
            ddi_dict['Filt-Public-ip-View'].\
                append(rddifirst_sheet.row_values(i))
            continue
        if rddifirst_sheet.row_values(i)[20].strip() == 'divest':
            ddi_dict['Filt-Divest'].append(rddifirst_sheet.row_values(i))
            continue
        if rddifirst_sheet.row_values(i)[4] == 'wan_test':
            ddi_dict['Filt-Wan_test-View'].\
                append(rddifirst_sheet.row_values(i))
            continue
        if IPv4Network(rddifirst_sheet.row_values(i)[1]).is_private or \
                IPv4Network(rddifirst_sheet.row_values(i)[1]).is_cgn:
            ddi_dict['MASTER'].append(rddifirst_sheet.row_values(i))
        else:
            ddi_dict['Filt-Uncategorized'].append(
                rddifirst_sheet.row_values(i))
    return ddi_dict
Esempio n. 22
0
 def test35_existing_network(self):
     self.cleaner.dir_path = 'testdata/sosreport_dir'
     self.cleaner._ip4_add_network('10.0.0.0/8')
     self.assertTrue(
         self.cleaner._ip4_network_in_db(IPv4Network('10.0.0.0/8')) is True)
Esempio n. 23
0
    def parse_line(self, line):
        """
        Parses a line from subnetdata.txt

        Returns None if the line did not contain network information. Otherwise
        it returns the attributes that are interesting to us as a dict.
        """

        # Format of subnetdata.txt:
        # - Fields are separated by tabs
        # - A field is a key/value pair, separated by a space
        # - The value of the DefaultRouters field is a comma-separated list of
        #   IP addresses
        # - The value of the UDF field is a list of "<key>=<value>" pairs,
        #   separated by ';'

        qipinfo = {}

        name = None
        location = None
        network_type = "unknown"
        side = "a"
        routers = []

        fields = line.split("\t")
        for field in fields:
            # The value may contain embedded spaces
            (key, value) = field.split(" ", 1)

            # Some fields contain structured data
            if key == "UDF":
                udf = {}
                for item in value.split(";"):
                    (udfkey, udfvalue) = item.split("=", 1)
                    udf[udfkey] = udfvalue
                value = udf

            qipinfo[key] = value

        # Sanity check
        if "SubnetId" not in qipinfo or "SubnetAddress" not in qipinfo or \
           "SubnetMask" not in qipinfo:
            self.logger.info("WARNING: Line contains no network: %s" % line)
            return None

        if "SubnetName" in qipinfo:
            name = qipinfo["SubnetName"].strip().lower()
        if not name:
            name = qipinfo["SubnetAddress"]

        # Parse the network address/netmask
        try:
            address = IPv4Network(
                "%s/%s" % (qipinfo["SubnetAddress"], qipinfo["SubnetMask"]))
        except AddressValueError:
            raise ValueError("Failed to parse the network address")
        except NetmaskValueError:
            raise ValueError("Failed to parse the netmask")

        # Parse the list of routers
        if "DefaultRouters" in qipinfo:
            for addr in qipinfo["DefaultRouters"].split(","):
                try:
                    routers.append(IPv4Address(addr))
                except AddressValueError:
                    raise ValueError("Bad IP address in DefaultRouters")

        # Extract MS-specific information from the UDF field
        if "UDF" in qipinfo:
            if "LOCATION" in qipinfo["UDF"]:
                # Values in QIP sometimes contain spaces and mixed case
                syslocstr = qipinfo["UDF"]["LOCATION"].strip().lower()
                sysloc = syslocstr.split('.')
                if len(sysloc) >= 3:
                    if sysloc[-3] in self.buildings:
                        location = self.buildings[sysloc[-3]]
                    else:
                        # Do not make "refresh network --all" fail if a new
                        # building does not exist in AQDB yet. Warn once for
                        # every unknown sysloc we encounter.
                        if syslocstr in self.unknown_syslocs:
                            return None
                        self.unknown_syslocs.add(syslocstr)
                        self.logger.client_info(
                            "Unknown building code in sysloc "
                            "%s, ignoring" % syslocstr)
                        return None
                else:
                    raise ValueError("Failed to parse LOCATION")

            if "BUCKET" in qipinfo["UDF"] and location:
                bucket = qipinfo["UDF"]["BUCKET"].strip().lower()
                bunker = bucket + "." + location.name
                if bunker in self.bunkers:
                    location = self.bunkers[bunker]

            if "TYPE" in qipinfo["UDF"]:
                network_type = qipinfo["UDF"]["TYPE"].strip().lower()

            if "SIDE" in qipinfo["UDF"]:
                side = qipinfo["UDF"]["SIDE"].strip().lower()

        # FIXME: How to handle networks with no location? dsdb maps them to
        # sysloc "xx.ny.na", so mimic that for now
        if not location:
            if "xx" in self.buildings:
                location = self.buildings["xx"]
            else:
                # FIXME: the testsuite does not have the "xx" building
                return None

        return QIPInfo(name=name,
                       address=address,
                       location=location,
                       network_type=network_type,
                       side=side,
                       routers=routers)
Esempio n. 24
0
 def test37_dup_networks(self):
     self.cleaner._ip4_add_network('10.0.0.0/8')
     self.cleaner._ip4_add_network('10.0.0.0/8')
     self.assertTrue(
         self.cleaner._ip4_network_in_db(IPv4Network('10.0.0.0/8')) is True)
Esempio n. 25
0
 def ip_network_object(self):
     try:
         return IPv4Network('%s/%s' %
                            (self.ipv4_addr, self.ipv4_netmask)).network
     except:
         return None
Esempio n. 26
0
def ask_ip_settings(d):

    ip_address = ask_ip_address(d, "Please enter IP address for the server:")
    ip_netmask = None
    ip_gateway = None
    ip_dns = None

    while not ip_netmask:

        try:
            (exit_value, dlg_ip_netmask) = d.inputbox(
                "Please enter the netmask for the server:\n"
                "(for example: 255.255.248.0)",
                10,
                60,
                "255.255.255.0",
                nocancel=1,
                backtitle=dialog_title)
            ip_netmask = IPv4Network(ip_address.compressed + '/' +
                                     dlg_ip_netmask)
        except ValueError:
            d.msgbox("Invalid netmask: " + dlg_ip_netmask,
                     7,
                     60,
                     backtitle=dialog_title)

    while not ip_gateway:

        try:
            suggested_gw = str(ip_netmask.network + 1)
            (exit_value, dlg_ip_gateway) = d.inputbox(
                "Please enter IP of the default gateway:",
                10,
                60,
                suggested_gw,
                nocancel=1,
                backtitle=dialog_title)
            ip_gateway = IPv4Address(dlg_ip_gateway)

            if not ip_gateway in ip_netmask:
                d.msgbox("Gateway IP " + str(ip_gateway) +
                         " is not in the entered network\n" +
                         str(ip_netmask.network) + "/" +
                         str(ip_netmask.netmask),
                         7,
                         60,
                         backtitle=dialog_title)
                ip_gateway = None

        except AddressValueError:
            d.msgbox("Invalid gateway IP address: " + dlg_ip_gateway,
                     7,
                     60,
                     backtitle=dialog_title)

    while not ip_dns:

        try:
            (exit_value, dlg_ip_dns) = d.inputbox(
                "Please enter IP of the DNS server to use during install:",
                10,
                60,
                '10.1.1.20',
                nocancel=1,
                backtitle=dialog_title)
            ip_dns = IPv4Address(dlg_ip_dns)

        except AddressValueError:
            d.msgbox("Invalid DNS server IP address: " + dlg_ip_dns,
                     7,
                     60,
                     backtitle=dialog_title)

    return (ip_address, ip_netmask, ip_gateway, ip_dns)
Esempio n. 27
0
File: util.py Progetto: faramirs/wot
def string_to_network(ip_str):
    """ Return an IPv4Network object from a dotted quad IP address/subnet. """
    try:
        return IPv4Network(ip_str)
    except AddressValueError:
        raise TypeError('Input not a valid IP address!')
Esempio n. 28
0
 class user_ipv4:
     address = IPv4Network("192.168.0.0/24")
     gateway = IPv4Address("192.168.0.1")
     reserved_addresses_bottom = 10
     reserved_addresses_top = 2
     vlan = VLANData.vlan_dummy1
Esempio n. 29
0
	def ipv4_address(self):
		"""Return IPv4 address."""
		return IPv4Network('%(address)s/%(netmask)s' % self)
def networkToIp(network):
    start = IPv4Network(network)[0]
    return int(start)