Skip to content
This repository was archived by the owner on Mar 4, 2024. It is now read-only.

Added get_ip_for_network_interface helper.#60

Open
pengale wants to merge 5 commits intomasterfrom
feature/lp-1602666/bind-network-interface
Open

Added get_ip_for_network_interface helper.#60
pengale wants to merge 5 commits intomasterfrom
feature/lp-1602666/bind-network-interface

Conversation

@pengale
Copy link
Copy Markdown

@pengale pengale commented Sep 2, 2016

Allows a charm to lookup the ip address of a named network interface, or
find a network interface that falls in a cidr range.

Added tests, and fixed things so that tox will actually run test_utils.py.

@juju-solutions/bigdata

Allows a charm to lookup the ip address of a named network interface, or
find a network interface that falls in a cidr range.

Added tests, and fixed things so that tox will actually run test_utils.py.
# address, even though interfaces on physical
# hardware will list this as their ipv6 ip. Just
# skip over this issue for now.
continue
Copy link
Copy Markdown
Contributor

@johnsca johnsca Sep 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be something more like this:

for interface in interfaces:
    for ip_version in (netifaces.AF_INET, netifaces.AF_INET6):
        for address in netifaces.ifaddresses(interface)[ip_version]:
            ip = address['addr']
            ip = getattr(ip, 'decode', lambda e: ip)('utf8')  # force unicode
            ip = ipaddress.ip_address(ip)
            if ip.is_link_local:
                continue
            if ip in ipaddress.ip_network(network_interface):
                return ip

Specifically:

  • Loop over all returned addresses, don't just try the first
  • Use is_link_local
  • I like hasattr(..., 'decode') better than encode / decode, but that's more personal preference

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants