-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace use of netifaces (compiled, unmaintained) with ifaddr (pure-p…
…ython, maintained)
- Loading branch information
Showing
3 changed files
with
20 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,18 @@ | ||
import netifaces | ||
import ifaddr | ||
from typing import List, Tuple | ||
|
||
|
||
def get_interfaces(ignore_local=True): | ||
""" | ||
Retrieves the address of all external interfaces (lo 127.0.0.1 ignored) | ||
:return: List of tuples (interface, addr) | ||
""" | ||
interfaces = netifaces.interfaces() | ||
if_ext = [] | ||
for i in interfaces: | ||
if i == 'lo' and ignore_local: | ||
continue | ||
iface = netifaces.ifaddresses(i).get(netifaces.AF_INET) | ||
if iface: | ||
for j in iface: | ||
if_ext.append((i, j['addr'], j['netmask'])) | ||
|
||
return if_ext | ||
def get_interfaces(ignore_local=True, only_ipv4=True) -> List[Tuple[str, str, int]]: | ||
ifaces = [] | ||
for adapter in ifaddr.get_adapters(): | ||
if adapter.name != 'lo' or not ignore_local: | ||
for ip in adapter.ips: | ||
if type(ip.ip) is str or not only_ipv4: | ||
ifaces.append((adapter.name, ip.ip, ip.network_prefix)) | ||
|
||
return ifaces | ||
|
||
|
||
if __name__ == '__main__': | ||
pass | ||
pass | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters