예제 #1
0
 def __init__(self, prefix, macaddr, gateway=None):
     self.prefix = ipaddress.IPv4Interface(prefix)
     self.macaddr = macaddr
     self.gateway = gateway
     return
예제 #2
0
    def __init__(self, *args, **kwargs):
        """Instance initialization."""
        deprecate(
            "Warning!",
            message="This DebianBox class is deprecated",
            category=UserWarning,
        )
        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 columns size
        self.set_cli_size(200)

        # we need to pick a non-conflicting private network here
        # also we want it to be consistent 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-server":
                    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.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
예제 #3
0
def unformat_api_ip4_address_with_prefix_t(o):
    return ipaddress.IPv4Interface((o.address, o.len))
예제 #4
0
	def run(self, params, args):

		networks = []
		for row in self.call('list.network', [ 'dns=true' ]):
			networks.append(row)

		local_conf = pathlib.Path('/opt/stack/share/templates/named.conf.j2')
		if local_conf.is_file():
			template_file = jinja2.Template(local_conf.read_text(), lstrip_blocks=True, trim_blocks=True)
		else:
			raise CommandError(self, 'Unable to parse template file: /opt/stack/share/templates/named.conf.j2')

		s = '<stack:file stack:name="/etc/named.conf" stack:perms="0644">\n'
		template_vars = {'edit_warning': stack.text.DoNotEdit()}

		acl = ['127.0.0.0/24']
		for network in networks:
			acl.append(str(ipaddress.IPv4Interface(f"{network['address']}/{network['mask']}")))
		template_vars['acl_list'] = acl

		fwds = self.getAttr('Kickstart_PublicDNSServers')
		if fwds is None:
			#
			# in the case of only one interface on the frontend,
			# then Kickstart_PublicDNSServers will not be
			# defined and Kickstart_PrivateDNSServers will have
			# the correct DNS servers
			#
			fwds = self.getAttr('Kickstart_PrivateDNSServers')
		if fwds is not None:
			fwds = fwds.strip()
	
		if fwds:
			template_vars['forwarders'] = ';'.join(fwds.split(','))

		if self.os == 'redhat':
			template_vars.update({
				'directory': '/var/named',
				'dumpfile': '/var/named/data/cache_dump.db',
				'statfile': '/var/named/data/named_stats.txt',
				'hintfile': 'named.ca',
				'localzone': 'named.localhost',
				'loopzone': 'named.local',
			})
		elif self.os == 'sles':
			template_vars.update({
				'directory': '/var/lib/named',
				'dumpfile': '/var/log/named_dump.db',
				'statfile': '/var/log/named.stats',
				'hintfile': 'root.hint',
				'localzone': 'localhost.zone',
				'loopzone': '127.0.0.zone',
			})

		# Generate the Forward Lookups
		fw_zones = []
		for network in networks:
			fw_zones.append({'name': network["network"], 'zone': network["zone"]})
		template_vars['fwzones'] = fw_zones

		# Generate the reverse lookups
		# For every network, get the base subnet,
		# and reverse it. This is the format
		# that named understands
		rev_zones = []
		z = {}
		for network in networks:
			sn = self.getSubnet(network['address'], network['mask'])
			sn.reverse()
			r_sn = '.'.join(sn)
			if not r_sn in z:
				z[r_sn] = []
			z[r_sn].append(network)
		for zone in z:
			if len(z[zone]) == 1:
				name = z[zone][0]["network"]
				comment_name = name
			else:
				n = '.'.join(list(map(lambda x: x["network"], z[zone]))).encode()
				name = hex(zlib.crc32(n) & 0xffffffff)[2:]
				comment_name = "networks - %s" % ','.join(list(map(lambda x: x["network"], z[zone])))
			rev_zones.append({'name': name, 'zone': zone, 'comment_name': comment_name})
		template_vars['revzones'] = rev_zones

		# Check if there are local modifications to named.conf
		local_conf = pathlib.Path('/etc/named.conf.local')
		if local_conf.is_file():
			template_vars['local_conf'] = local_conf.read_text()

		s += template_file.render(template_vars)

		s += '</stack:file>\n'

		self.beginOutput()
		self.addOutput('', s)
		self.endOutput(padChar='')
예제 #5
0
def get_kubernetes_service_ip():
    '''Get the IP address for the kubernetes service based on the cidr.'''
    interface = ipaddress.IPv4Interface(service_cidr())
    # Add .1 at the end of the network
    ip = interface.network.network_address + 1
    return ip.exploded
예제 #6
0
 def ipv4(self):
     return ipaddress.IPv4Interface(self.get_metadata("ipv4"))
예제 #7
0
    def jvision_init(self, **kwargs):
        """
        Initialize keyword for Jvision Server. The parameters are
        device, interface, server_ip_address, dut_ip_address
        decoder_path, decoder_port

        Usage:

        jvision init    device=<server handle>  interface=<Interface of server facing DUT>
        server_ip_address=<server ip address>   dut_ip_address=<dut ip address>
        decoder_path=<location of decoder>      decoder_port=<decoder part>    mgmt_ip=<flag for mgmt_ip>

        Example :

        jvision init    device=${h0}    interface=eth1
        server_ip_address=1.1.1.1/24   dut_ip_address=1.1.1.2/24
        decoder_path=  grpc=/opt/jvision/grpc/oc/    decoder_port=4321    mgmt_ip=False
        """
        if not kwargs.get('device') or not kwargs.get('interface') \
                or not kwargs.get('server_ip_address') or not kwargs.get('dut_ip_address'):
            t.log(level='ERROR',
                  message='missing mandatory server '
                  'or interface or server ip address or dut ip address')
            raise ValueError(
                "server & dut information are mandatory arguments")
        count = 5
        server = kwargs.get('device')
        interface = kwargs.get('interface')
        if kwargs.get('mgmt_ip'):
            mgmt_ip = ast.literal_eval(kwargs.get('mgmt_ip'))
        else:
            mgmt_ip = False
        self.server_ip_address = kwargs.get('server_ip_address')
        self.dut_ip_address = kwargs.get('dut_ip_address')
        if not server.su():
            raise ValueError("cannot login to server as root user")
        if mgmt_ip == False:
            try:
                t.log(level='INFO',
                      message='configuring IP address on interface facing DUT')
                server.shell(command="ifconfig %s 0" % interface + '\n')
                server.shell(command='ifconfig ' + interface + ' ' +
                             self.server_ip_address + '\n')
                t.log(level='INFO',
                      message='verify IP address configuration on interface')
                response = server.shell(command="ifconfig %s" % interface +
                                        '\n')
                server_ip = str(ipaddress.IPv4Interface(
                    self.server_ip_address)).split("/")
                match = re.search(
                    'inet' + '.*' + server_ip[0] + '.*' + 'Mask:',
                    response.response())
                if match:
                    t.log(
                        level='INFO',
                        message="Server IP address is configured successfully")
                else:
                    t.log(level='ERROR',
                          message=
                          "Server IP address is not configured successfully")
                t.log(level='INFO', message='configuring static route to DUT')
                dut_network = str(
                    ipaddress.IPv4Network(
                        self.dut_ip_address,
                        strict=False).with_netmask).split("/")
                dut_ip = str(ipaddress.IPv4Interface(
                    self.dut_ip_address)).split("/")
                server.shell(command='route add -net ' + dut_network[0] + ' ' +
                             'netmask' + ' ' + dut_network[1] + ' ' + 'dev' +
                             ' ' + interface + '\n')
                response = server.shell(command="netstat -nr" + '\n')
                match = re.search(
                    dut_network[0] + '.*' + dut_network[1] + '.*',
                    response.response())
                if match:
                    t.log(level='INFO',
                          message="Route to DUT is configured successfully")
                else:
                    t.log(
                        level='ERROR',
                        message="Route to DUT is not configured successfully")
            except:
                t.log(level='ERROR', message='shell command execution failed')
                raise ValueError(
                    "cannot configure IP address and route on server")
        dut_ip = str(ipaddress.IPv4Interface(self.dut_ip_address)).split("/")
        t.log(level='INFO', message='Verify if DUT is reachable')
        response = server.shell(
            command=('ping ' + dut_ip[0] + ' ' + '-c' + ' ' + str(count)) +
            '\n')
        match = re.search(
            str(count) + ' ' + 'packets transmitted,' + ' ' + str(count) +
            ' ' + 'received,' + ' ' + '0% packet loss', response.response())
        if match:
            t.log(level='INFO', message="Ping successful to DUT")
        else:
            t.log(level='ERROR', message="Ping unsuccessful to DUT")
        if not kwargs.get('decoder_path'):
            t.log(level='ERROR', message='missing mandatory params')
            raise ValueError("missing mandatory params")
        if not isinstance(kwargs.get('decoder_port'), dict) and \
                isinstance(kwargs.get('decoder_path'), dict):
            t.log(level='ERROR', message='params must be dictionary')
            raise TypeError("params must be dictionary")
        else:
            if kwargs.get('decoder_path').get('grpc'):
                self.decoder_path['grpc'] = str(
                    kwargs.get('decoder_path')['grpc'])
                self.decoder_port['grpc'] = int(
                    kwargs.get('decoder_port')['grpc'])
            if kwargs.get('decoder_path').get('udp'):
                self.decoder_path['udp'] = str(
                    kwargs.get('decoder_path')['udp'])
                self.decoder_port['udp'] = int(
                    kwargs.get('decoder_port')['udp'])
            if kwargs.get('decoder_path').get('gnmi'):
                self.decoder_path['gnmi'] = str(
                    kwargs.get('decoder_path')['gnmi'])
                self.decoder_port['gnmi'] = int(
                    kwargs.get('decoder_port')['gnmi'])
            if kwargs.get('decoder_path').get('gnmi-dialout'):
                self.decoder_path['gnmi-dialout'] = str(
                    kwargs.get('decoder_path')['gnmi-dialout'])
                self.decoder_port['gnmi-dialout'] = int(
                    kwargs.get('decoder_port')['gnmi-dialout'])
        self.jv_db_server = kwargs.get('db_server', None)
예제 #8
0
def interface_lookup(ip_address, interfaces, routes):

    ip_address = ipaddress.ip_address(ip_address)

    interface_networks = {}
    matching_interfaces = {}
    selected_interface = ""

    route_networks = {}
    matching_routes = {}
    selected_route = ""

    # lookup interface subnets

    ## build dictionary of all interface networks

    for interface, attributes in interfaces.items():

        if attributes["ipv4_config"]:

            for ipv4_config in attributes["ipv4_config"]:

                interface_networks[interface] = ipaddress.IPv4Interface(
                    ipv4_config["ip_address"] + "/" + ipv4_config["mask"])

    ## check if ip address matches any interface networks

    for interface, network in interface_networks.items():

        if ip_address in network.network:

            matching_interfaces[interface] = network

    ## if multiple matching interfaces find the most specific

    if len(matching_interfaces) == 1:

        selected_interface = list(matching_interfaces)[0]

    elif len(matching_interfaces) > 1:

        for interface, network in matching_interfaces.items():

            if selected_interface:

                if network.network.netmask > selected_interface_network.network.netmask:

                    selected_interface = interface
                    selected_interface_network = network
            else:

                selected_interface = interface
                selected_interface_network = network

    else:

        selected_interface = ""

    ## return if an interface selected

    if selected_interface:
        return selected_interface

    # lookup routes if no interface found

    ## build dictionary of all routes

    for id, route in enumerate(routes):

        route_networks[id] = ipaddress.IPv4Network(route["network"] + "/" +
                                                   route["mask"])

    ## check if ip address matches any routes

    for id, route in route_networks.items():

        if ip_address in route:

            matching_routes[id] = route

    ## if multiple matching routes find the most specific

    if len(matching_routes) == 1:

        selected_route = list(matching_routes)[0]

    elif len(matching_routes) > 1:

        for id, route in matching_routes.items():

            if selected_route:

                if route.netmask > selected_route_network.netmask:

                    selected_route = id
                    selected_route_network = route

            else:

                selected_route = id
                selected_route_network = route

    else:

        selected_route = ""

    ## resolve interface if a route selected

    if str(selected_route):

        selected_interface = routes[selected_route]["interface"]

    ## return if an interface selected

    if selected_interface:
        return selected_interface

    else:
        return ""
예제 #9
0
#!/usr/bin/env python3
import json
import requests
import ipaddress
import sys

aws_ip_url = 'https://ip-ranges.amazonaws.com/ip-ranges.json'

try:
    ip = sys.argv[1]
except IndexError:
    raise SystemExit(f"Usage: {sys.argv[0]} <ip address to check>")

try:
    ip_ranges = requests.get(aws_ip_url, timeout=30).json()['prefixes']
except requests.exceptions.HTTPError as errh:
    raise SystemExit(errh)
except requests.exceptions.ConnectionError as errc:
    raise SystemExit(errc)
except requests.exceptions.Timeout as errt:
    raise SystemExit(errt)
except requests.exceptions.RequestException as err:
    raise SystemExit(err)

for net in ip_ranges:
    if ipaddress.IPv4Interface(ip) in ipaddress.IPv4Network(net["ip_prefix"]):
        print(json.dumps(net, indent=1))
예제 #10
0
partitionName = "test-partition"

# some lists for passed/failed test info
TESTS_PASSED = []
TESTS_FAILED = []

configChecks = ConfigChecks()
staticIPsChecks = ConfigChecks('/usr/local/etc/tredly/static-ips.conf')
sshChecks = ConfigChecks('/etc/ssh/sshd_config', ' ')
unboundChecks = ConfigChecks('/usr/local/etc/unbound/unbound.conf', ': ')
rcConfChecks = ConfigChecks('/etc/rc.conf', '=')

containerNetworkInterface = 'bridge1'
containerNetworkIP = '10.0.0.0'
containerNetworkCIDR = '16'
containerNetwork = ipaddress.IPv4Interface(containerNetworkIP + '/' +
                                           containerNetworkCIDR)
containerNetworkNetmask = str(containerNetwork.with_netmask).split('/', 1)[-1]

e_header("Running container subnet change test")
# modify the container subnet
cmd = [
    'tredly', 'config', 'container', 'subnet',
    str(containerNetwork.network)
]
process = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdOut, stdErr = process.communicate()
stdOutString = stdOut.decode("utf-8", "replace")
stdErrString = stdErr.decode('utf-8', 'replace')
print(stdOutString)

# get the interface for the bridge
예제 #11
0
def ext_pillar(minion_id, pillar, *args, **kwargs):
    """
    Query NetBox API for minion data
    """
    if minion_id == "*":
        log.info("There's no data to collect from NetBox for the Master")
        return {}
    # Pull settings from kwargs
    api_url = kwargs["api_url"].rstrip("/")
    api_token = kwargs.get("api_token")
    site_details = kwargs.get("site_details", True)
    site_prefixes = kwargs.get("site_prefixes", True)
    proxy_username = kwargs.get("proxy_username", None)
    proxy_return = kwargs.get("proxy_return", True)
    ret = {}

    # Fetch device from API
    headers = {}
    if api_token:
        headers = {"Authorization": f"Token {api_token}"}
    device_search_url = "{api_url}/{app}/{endpoint}".format(
        api_url=api_url, app="dcim", endpoint="devices"
    )
    device_search_results = salt.utils.http.query(
        device_search_url, params={"name": minion_id}, header_dict=headers, decode=True
    )
    search_results = device_search_results
    if len(search_results["dict"]["results"]) == 0:
        vm_search_url = "{api_url}/{app}/{endpoint}".format(
            api_url=api_url, app="virtualization", endpoint="virtual-machines"
        )
        vm_search_results = salt.utils.http.query(
            vm_search_url, params={"name": minion_id}, header_dict=headers, decode=True
        )
        search_results = vm_search_results
    # Check status code for API call
    if "error" in search_results:
        log.error(
            f'API query failed for "{minion_id}", status code: {search_results["status"]}'
        )
        log.error(search_results["error"])
        return ret
    # Assign results from API call to "netbox" key
    if len(search_results["dict"]["results"]) == 0:
        log.error(f'No device found for "{minion_id}"')
        return ret
    if len(search_results["dict"]["results"]) > 1:
        log.error(f'More than one device found for "{minion_id}"')
        return ret
    if "vcpus" not in search_results["dict"]["results"][0]:
        device_url = "{api_url}/{app}/{endpoint}/{id}/".format(
            api_url=api_url,
            app="dcim",
            endpoint="devices",
            id=search_results["dict"]["results"][0]["id"],
        )
        device_results = salt.utils.http.query(
            device_url, header_dict=headers, decode=True
        )

    else:
        device_url = "{api_url}/{app}/{endpoint}/{id}/".format(
            api_url=api_url,
            app="virtualization",
            endpoint="virtual-machines",
            id=search_results["dict"]["results"][0]["id"],
        )
        device_results = salt.utils.http.query(
            device_url, header_dict=headers, decode=True
        )

    if "error" in device_results:
        log.error(
            f'API query failed for "{minion_id}", status code: {search_results["status"]}'
        )
        log.error(search_results["error"])
        return ret

    ret["netbox"] = device_results["dict"]
    site_id = 0
    site_name = ""
    if not ret["netbox"]["site"]:
        # eg virtual maschine in multi-site cluster
        site_details = False
        site_prefixes = False
    else:
        site_id = ret["netbox"]["site"]["id"]
        site_name = ret["netbox"]["site"]["name"]
    service_url = "{api_url}/{app}/{endpoint}".format(
        api_url=api_url, app="ipam", endpoint="services"
    )
    service_results = salt.utils.http.query(
        service_url, header_dict=headers, decode=True
    )
    services = service_results["dict"]["results"]
    if len(services) >= 1:
        ret["netbox"]["services"] = {}
        for service in services:
            ret["netbox"]["services"][service["name"]] = service

    query_param = "device_id"
    app = "dcim"
    if "vcpus" in ret["netbox"]:
        app = "virtualization"
        query_param = "virtual_machine_id"

    ret["netbox"]["interfaces"] = {}
    interface_url = "{api_url}/{app}/{endpoint}/".format(
        api_url=api_url, app=app, endpoint="interfaces"
    )

    interface_results = salt.utils.http.query(
        interface_url,
        params={query_param: search_results["dict"]["results"][0]["id"], "limit": 1000},
        header_dict=headers,
        decode=True,
    )
    if "error" in interface_results:
        log.error(
            f'API query failed for "{minion_id}", status code: {interface_results["status"]}'
        )
        log.error(interface_results["error"])
        return ret
    else:
        for interface in interface_results["dict"]["results"]:
            ret["netbox"]["interfaces"][interface["name"]] = interface
            ret["netbox"]["interfaces"][interface["name"]]["ipaddresses"] = []

    if "vcpus" in ret["netbox"]:
        query_param = "virtual_machine_id"
    ipaddress_url = "{api_url}/{app}/{endpoint}/".format(
        api_url=api_url, app="ipam", endpoint="ip-addresses"
    )
    ipaddress_results = salt.utils.http.query(
        ipaddress_url,
        params={query_param: search_results["dict"]["results"][0]["id"]},
        header_dict=headers,
        decode=True,
    )
    if "error" in ipaddress_results:
        log.error(
            f'API query failed for "{minion_id}", status code: {ipaddress_results["status"]}'
        )
        log.error(ipaddress_results["error"])
        return ret
    ipaddresses = ipaddress_results["dict"]["results"]
    ## Get all interfaces for device

    for ipaddress in ipaddresses:
        interface_id = ipaddress["assigned_object_id"]
        app = "dcim"
        if "vcpus" in ret["netbox"]:
            app = "virtualization"
        interface_url = "{api_url}/{app}/{endpoint}/{id}/".format(
            api_url=api_url, app=app, endpoint="interfaces", id=interface_id
        )
        interface_results = salt.utils.http.query(
            interface_url, header_dict=headers, decode=True
        )
        if "error" in interface_results:
            log.error(
                f'API query failed for "{minion_id}", status code: {interface_results["status"]}'
            )
            log.error(interface_results["error"])
            return ret
        if interface_results["dict"]["name"] not in ret["netbox"]["interfaces"]:
            ret["netbox"]["interfaces"][
                interface_results["dict"]["name"]
            ] = interface_results["dict"]
        if (
            "ipaddresses"
            not in ret["netbox"]["interfaces"][interface_results["dict"]["name"]]
        ):
            ret["netbox"]["interfaces"][interface_results["dict"]["name"]][
                "ipaddresses"
            ] = []
        ret["netbox"]["interfaces"][interface_results["dict"]["name"]][
            "ipaddresses"
        ].append(ipaddress)

    if site_details:
        log.debug(
            f'Retrieving site details for "{minion_id}" - site {site_name} (ID {site_id})'
        )
        site_url = "{api_url}/{app}/{endpoint}/{site_id}/".format(
            api_url=api_url, app="dcim", endpoint="sites", site_id=site_id
        )
        site_details_ret = salt.utils.http.query(
            site_url, header_dict=headers, decode=True
        )
        if "error" in site_details_ret:
            log.error(f"Unable to retrieve site details for {site_name} (ID {site_id})")
            log.error(
                f'Status code: {site_details_ret["status"]}, error: {site_details_ret["error"]}'
            )
        else:
            ret["netbox"]["site"] = site_details_ret["dict"]
    if site_prefixes:
        log.debug(
            f'Retrieving site prefixes for "{minion_id}" - site {site_name} (ID {site_id})'
        )
        prefixes_url = "{api_url}/{app}/{endpoint}".format(
            api_url=api_url, app="ipam", endpoint="prefixes"
        )
        site_prefixes_ret = salt.utils.http.query(
            prefixes_url, params={"site_id": site_id}, header_dict=headers, decode=True
        )
        if "error" in site_prefixes_ret:
            log.error(
                f"Unable to retrieve site prefixes for {site_name} (ID {site_id})"
            )
            log.error(
                f'Status code: {site_prefixes_ret["status"]}, error: {site_prefixes_ret["error"]}'
            )
        else:
            ret["netbox"]["site"]["prefixes"] = site_prefixes_ret["dict"]["results"]
    if proxy_return:
        # Attempt to add "proxy" key, based on platform API call
        try:
            # Fetch device from API
            platform_results = salt.utils.http.query(
                ret["netbox"]["platform"]["url"], header_dict=headers, decode=True
            )
            # Check status code for API call
            if "error" in platform_results:
                log.info(
                    f'API query failed for "{minion_id}": {platform_results["error"]}'
                )
            # Assign results from API call to "proxy" key if the platform has a
            # napalm_driver defined.
            napalm_driver = platform_results["dict"].get("napalm_driver")
            if napalm_driver:
                ret["proxy"] = {
                    "host": str(
                        ipaddress.IPv4Interface(
                            ret["netbox"]["primary_ip4"]["address"]
                        ).ip
                    ),
                    "driver": napalm_driver,
                    "proxytype": "napalm",
                }
                if proxy_username:
                    ret["proxy"]["username"] = proxy_username

        except Exception:
            log.debug(f'Could not create proxy config data for "{minion_id}"')

    tag_list = []
    for tag in ret["netbox"]["tags"]:
        tag_list.append(tag["slug"])
    ret["netbox"]["tag_list"] = tag_list

    return ret
예제 #12
0
print(ip_list2)



#Main
for i in range(len(ip_list2)):
    
    ipf = 0
    ip = ip_list2[i]
    date_list = []
    ping_list = []
    #print (ip)
    length = len(data)
    for index,column in df.iterrows() :
        #print(list)
        if ip == str(ipaddress.IPv4Interface(column["ipv4"]).network):
            date_list.append(column["date"])
            ping_list.append(column["ping"])
    #print(date_list)   
    #print(ping_list)
    ipf = func3(ip,date_list,ping_list,n,ipf)
    print()
    func4(ip,date_list,ping_list,m,t,ipf)



def func3(ip,date_list,ping_list,n,ipf):
    #故障状態算出プログラム
    f = 0
    time1 = datetime.datetime.now()
    time2 = datetime.datetime.now()
예제 #13
0
 def addIPv4Address (self, value):
   self._v4addresses.append(ipaddress.IPv4Interface(unicode(value)))
예제 #14
0
 def __init__(self, prefix):
     self.prefix = ipaddress.IPv4Interface(prefix)
예제 #15
0
    # new_mask = u'0.0.0.255'
    new_mask = new.hostmask
    new_mask_bin = struct.unpack("!L", new_mask.packed)[0]
    new_netip_bin = struct.unpack("!L", new_netip.packed)[0]

    # because origin ip address does not provide mask info
    #  0.0.0.5       = 172.30.1.5 & 0.0.0.255
    org_hostonly_bin = org_ip_bin & new_mask_bin

    # 172.30.2.5 = 172.30.2.0 | 0.0.0.5
    new_ip_bin = new_netip_bin | org_hostonly_bin
    new_ip = socket.inet_ntoa(struct.pack("!L", new_ip_bin))

    new_prefix = new.network.prefixlen
    new_addr = new_ip + '/' + str(new_prefix)
    return new_addr


if __name__ == '__main__':
    org_addr = unicode(sys.argv[1])
    new_netrange = unicode(sys.argv[2])

    new = ipaddress.IPv4Interface(new_netrange)
    if new.network.prefixlen == 32:
        sys.exit(1)

    new_addr = generate_new_addr(org_addr, new_netrange)

    # 172.30.10.81/24
    print new_addr
예제 #16
0
    add action=drop chain=wan_fwdin comment="Drop all packets from public internet which should not exist in public network" src-address-list=RFC6890
    add action=accept chain=wan_fwdin comment="Allow dst-natted packets" connection-nat-state=dstnat
    add action=drop chain=wan_fwdout comment="Drop all packets from local network to internet which should not exist in public network" dst-address-list=RFC6890  
    add action=accept chain=wan_fwdout comment="Allow all other connections"
}""")

lans = [ x.strip() for x in config["DEFAULT"]["LanInterfaces"].split(",") ]
lan_networks = [ ipaddress.ip_network(x.strip()) for x in config["DEFAULT"]["LanNetworks"].split(",")]

wans = list()

for wan in [ x.strip() for x in config["DEFAULT"]["WanInterfaces"].split(",") ]:
    if config.has_section(wan):
        wans.append({
            "interface": config[wan]["interface"],
            "ip": ipaddress.IPv4Interface(config[wan]["ip"]),
            "gateway": config[wan]["gateway"],
            "monitor": config[wan]["link monitor"],
            "weight": int(config[wan]["weight"])
        })

# calculate weights
weight_sum = 0

for wan in wans:
    if "weight" in wan:
        weight_sum += wan["weight"]
    else:
        weight_sum += 1

# interface lists
예제 #17
0
def network(address):
    return str(ipa.IPv4Interface(address).network)
예제 #18
0
 def __call__(self, form, field):
     try:
         ip = ipaddress.IPv4Interface(f'192.168.0.1/{field.data}')
     except ipaddress.NetmaskValueError:
         raise ValidationError(self.message)
예제 #19
0
    for each_device in cfg_devices:
        print("Device: " + each_device['name'] + " - SN: " + each_device['serial'] + " ip-device: "+ each_device['primary_ip']['address'])
        try:
            nb_import[each_device['serial']]
        except:
            nb_import[each_device['serial']]=dict()
        
        # MAC
        mac = macs[each_device.get('name')]
        if mac != 'None' and mac is not None:
            nb_import[each_device['serial']]['mac'] = mac
        #Hostname
        nb_import[each_device['serial']]['hostname'] = each_device['name']

        # IP + Mask
        nb_import[each_device['serial']]['ip'] = str(ipaddress.IPv4Interface(each_device['primary_ip']['address']).ip)
        nb_import[each_device['serial']]['subnet_mask'] = str(ipaddress.IPv4Interface(each_device['primary_ip']['address']).netmask)

        # Router / Geteway
        device_network = ipaddress.ip_interface(each_device['primary_ip']['address']).network
        if valid_ip(nb_cfg['config']['router']):
            nb_import[each_device['serial']]['router'] = nb_cfg['config']['router']
        elif nb_cfg['config']['router'] == -1:
            gw = ipaddress.IPv4Network(device_network)[-2]
            nb_import[each_device['serial']]['router'] = str(gw)
        else:
            gw = ipaddress.IPv4Network(device_network)[1]
            nb_import[each_device['serial']]['router'] = str(gw)

        # Domain
        if nb_cfg['config'].get('domain') is not None:
예제 #20
0
파일: tools.py 프로젝트: mvalja/autodata
def returnNetworkZone(inputIP):  #Input should be 192.168.0.1/24
    if (isinstance(inputIP, str)):
        ip = ipaddress.IPv4Interface(inputIP)
        output = str(ip.network)
        return output
예제 #21
0
def get_dns_ip():
    '''Get an IP address for the DNS server on the provided cidr.'''
    interface = ipaddress.IPv4Interface(service_cidr())
    # Add .10 at the end of the network
    ip = interface.network.network_address + 10
    return ip.exploded
예제 #22
0
파일: ufw.py 프로젝트: kellyjonbrazil/jc
def _parse_to_from(linedata, direction, rule_obj=None):

    if rule_obj is None:
        rule_obj = {}

    # pull out rule index, if they exist: [ 1]
    if direction == 'to':
        RE_LINE_NUM = re.compile(r'\[[ 0-9]+\]\s')
        line_number_match = re.search(RE_LINE_NUM, linedata)
        if line_number_match:
            rule_obj['index'] = line_number_match.group(0).replace('[', '').replace(']', '').strip()
            linedata = re.sub(RE_LINE_NUM, '', linedata)
        else:
            rule_obj['index'] = None

    # pull out comments, if they exist
    if direction == 'from':
        RE_COMMENT = re.compile(r'#.+$')
        comment_match = re.search(RE_COMMENT, linedata)
        if comment_match:
            rule_obj['comment'] = comment_match.group(0).lstrip('#').strip()
            linedata = re.sub(RE_COMMENT, '', linedata)
        else:
            rule_obj['comment'] = None

    # pull (v6)
    RE_V6 = re.compile(r'\(v6\)')
    v6_match = re.search(RE_V6, linedata)
    if v6_match:
        rule_obj['network_protocol'] = 'ipv6'
        linedata = re.sub(RE_V6, '', linedata)
    elif not rule_obj.get('network_protocol'):
        rule_obj['network_protocol'] = 'ipv4'

    # pull 'Anywhere' if exists. Assign to 0.0.0.0/0 or ::/0 depending on if (v6) is found
    if 'Anywhere' in linedata:
        if rule_obj.get('network_protocol') == 'ipv6':
            rule_obj[direction + '_ip'] = '::'
            rule_obj[direction + '_ip_prefix'] = '0'
        elif rule_obj.get('network_protocol') == 'ipv4':
            rule_obj[direction + '_ip'] = '0.0.0.0'
            rule_obj[direction + '_ip_prefix'] = '0'
        linedata = linedata.replace('Anywhere', '')

    # pull out interface (after 'on')
    linedata_list = linedata.split(' on ', maxsplit=1)

    if len(linedata_list) > 1:
        rule_obj[direction + '_interface'] = linedata_list[1].strip()
        linedata = linedata_list[0]
    else:
        rule_obj[direction + '_interface'] = 'any'

    # pull tcp/udp/etc. transport - strip on '/'
    linedata_list = linedata.rsplit('/', maxsplit=1)
    if len(linedata_list) > 1:
        if linedata_list[1].strip() in ['tcp', 'udp', 'ah', 'esp', 'gre', 'ipv6', 'igmp']:
            rule_obj[direction + '_transport'] = linedata_list[1].strip()
            linedata = linedata_list[0]
        else:
            rule_obj[direction + '_transport'] = 'any'
    else:
        rule_obj[direction + '_transport'] = 'any'

    # pull out ipv4 or ipv6 addresses
    linedata_list = linedata.split()
    new_linedata_list = []

    valid_ip = None
    for item in linedata_list:
        try:
            valid_ip = ipaddress.IPv4Interface(item)
        except Exception:
            try:
                valid_ip = ipaddress.IPv6Interface(item)
            except Exception:
                new_linedata_list.append(item)

    if valid_ip:
        rule_obj[direction + '_ip'] = str(valid_ip.ip)
        rule_obj[direction + '_ip_prefix'] = str(valid_ip.with_prefixlen.split('/')[1])
        linedata = ' '.join(new_linedata_list)

    # find the numeric port(s)
    linedata_list = linedata.split(',')
    port_list = []
    port_ranges = []
    for item in linedata_list:
        if item.strip().isnumeric():
            port_list.append(item.strip())
        elif ':' in item:
            p_range = item.strip().split(':', maxsplit=1)
            port_ranges.append(
                {
                    "start": p_range[0],
                    "end": p_range[1]
                }
            )

    if port_list or port_ranges:
        rule_obj[direction + '_service'] = None
        linedata = ''

    if port_list:
        rule_obj[direction + '_ports'] = port_list

    if port_ranges:
        rule_obj[direction + '_port_ranges'] = port_ranges

    # only thing left should be the service name.
    if linedata.strip():
        rule_obj[direction + '_service'] = linedata.strip()
        rule_obj[direction + '_transport'] = None

    # check if to/from IP addresses exist. If not, set to 0.0.0.0/0 or ::/0
    if direction + '_ip' not in rule_obj:
        if rule_obj.get('network_protocol') == 'ipv6':
            rule_obj[direction + '_ip'] = '::'
            rule_obj[direction + '_ip_prefix'] = '0'
        elif rule_obj.get('network_protocol') == 'ipv4':
            rule_obj[direction + '_ip'] = '0.0.0.0'
            rule_obj[direction + '_ip_prefix'] = '0'

    # finally set default ports if no ports exist and there should be some
    if direction + '_transport' in rule_obj:
        if rule_obj[direction + '_transport'] in ['tcp', 'udp', 'any']:
            if not port_list and not port_ranges:
                rule_obj[direction + '_port_ranges'] = [
                    {
                        'start': '0',
                        'end': '65535'
                    }
                ]
                rule_obj[direction + '_service'] = None

    return rule_obj
예제 #23
0
def get_deprecated_dns_ip():
    '''We previously hardcoded the dns ip. This function returns the old
    hardcoded value for use with older versions of cdk_addons.'''
    interface = ipaddress.IPv4Interface(service_cidr())
    ip = interface.network.network_address + 10
    return ip.exploded
예제 #24
0
 def __init__(self, name, ip, description, status="down"):
     self.name = name
     self.ip = ipaddress.IPv4Interface(ip)
     self.description = description
     self.status = status
예제 #25
0
from tests.common import config_reload

from test_voq_init import check_voq_interfaces

from tests.common.helpers.sonic_db import VoqDbCli, SonicDbKeyNotFound
from tests.common.helpers.assertions import pytest_assert
from tests.common.utilities import wait_until
from test_voq_disrupts import check_bgp_neighbors
logger = logging.getLogger(__name__)

pytestmark = [
    pytest.mark.topology('t2')
]

ADDR = ipaddress.IPv4Interface(u"50.1.1.1/24")


def test_cycle_voq_intf(duthosts, all_cfg_facts, nbrhosts, nbr_macs):
    """
    Delete and recreate VOQ interface through config save/load.  Verify interface
    is removed after configdb reload and then recreated after loading initial minigraph.

    Args:
        duthosts: The duthosts fixture
        all_cfg_facts: all_cfg_facts fixture from voq conftest
        nbrhosts: nbrhosts fixture
        nbr_macs: nbr_macs fixture from voq conftest

    """
예제 #26
0
파일: importer.py 프로젝트: hj1996/dom
import ipaddress

f = open("extrentiable_attibutes.csv", "r")
data = f.readlines()
ex = {}
for info in data:
    info = info.split(",")
    ex[info[0]] = info[1]
fw = open("result2.csv", "w")
fw.write("sep=;\n")
for line in data:
    if "network" not in line:
        line = line.split(",")
        key = unicode(line[0])
        print key
        ip = ipaddress.IPv4Interface(key)
        ip = ip.with_netmask
        ip = ip.split("/")
        ip = ["network"] + ip + [line[1]] + ["OVERRIDE"]
        print ip
        for data in ip:
            data = data.replace("\n", "")
            fw.write(data)
            fw.write(";")
        fw.write("\n")
    else:
        line = line.split(",")
        header = [
            "header-network", "address*", "netmask*", "EA-" + line[1],
            "EAInherited-" + line[1]
        ]
예제 #27
0
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))
예제 #28
0
 def python_value(self, value):
     return None if value is None else ipaddress.IPv4Interface(value)
예제 #29
0
 def __init__(self):
     ip = self._findIPAddr()
     mask = self._findSubnetMask(ip)
     self.interface = ipaddress.IPv4Interface("%s/%s" % (ip, mask))
예제 #30
0
파일: DZ.py 프로젝트: Pazuuuzu/p4ne
from openpyxl import Workbook

way = glob.glob("C:\\test\config_files\*.txt")
prob = '____________________________________________'
IPadd = []

for file in way:
    with open(file) as newfile:
        for line in newfile:
            IP = re.match(
                "^ ip address (([0-9]{1,3}\.){3}[0-9]{1,3}) (([0-9]{1,3}\.){3}[0-9]{1,3})",
                line)

            if IP:
                IPadd.append(
                    ipaddress.IPv4Interface(
                        str(IP.group(1)) + "/" + str(IP.group(3))))
wb = Workbook()
ws = wb.active

ws['A1'] = 'Subnet'
ws['B1'] = 'Mask'
ws['C1'] = 'GW'

coll = 2
p = 0
print("%20s" % "Net", "%20s" % "GW")
while p < len(IPadd) - 1:
    print("%20s" % IPadd[p].network.network_address,
          "%10s" % IPadd[p].network.prefixlen, "%20s" % IPadd[p].ip)
    p += 1
    n = str(IPadd[p].network.network_address)