mirror of
https://github.com/Brandon-Rozek/website.git
synced 2024-11-09 10:40:34 -05:00
1.7 KiB
1.7 KiB
title | date | draft | tags | medium_enabled | ||
---|---|---|---|---|---|---|
How to get list of IP Addresses in Python | 2020-09-07T20:03:24-04:00 | false |
|
true |
Elemag on gave a quick solution on StackOverflow on how to determine IP addresses with multiple NICS.
His one-line solution:
import netifaces
ip_addresses = [netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifaces.ifaddresses(iface)]
Expanded out to see what is happening
import netifaces
interface_list = netifaces.interfaces()
# Get addresses, netmask, etc. information
address_entries = (netifaces.ifaddresses(iface) for iface in interface_list)
# Only pay attention to ipv4 address types
ipv4_address_entries = (address[netifaces.AF_INET] for address in address_entries if netifaces.AF_INET in address)
# Since multiple addresses can be associated, only look at the first ip address
ipv4_addresses = [address[0]['addr'] for address in ipv4_address_entries]
We can easily adjust this to ask for IPv6 addresses by using netifaces.AF_INET6
instead.
import netifaces
interface_list = netifaces.interfaces()
# Get addresses, netmask, etc. information
address_entries = (netifaces.ifaddresses(iface) for iface in interface_list)
# Only pay attention to ipv6 address types
ipv6_address_entries = (address[netifaces.AF_INET6] for address in address_entries if netifaces.AF_INET6 in address)
# Since multiple addresses can be associated, only look at the first ip address
ipv6_addresses = [address[0]['addr'] for address in ipv6_address_entries]