generated from NethServer/ns8-kickstart
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added get-available-interfaces action
- Loading branch information
Showing
3 changed files
with
91 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# | ||
# Copyright (C) 2024 Nethesis S.r.l. | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# | ||
|
||
import json | ||
import sys | ||
|
||
import network | ||
|
||
interfaces = network.list_interfaces() | ||
interfaces_names = [interface["name"] for interface in interfaces] | ||
|
||
json.dump({ | ||
"data": interfaces_names | ||
}, sys.stdout) |
16 changes: 16 additions & 0 deletions
16
imageroot/actions/get-available-interfaces/validate-output.json
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"$id": "http://schema.nethserver.org/dnsmasq/get-available-interfaces.json", | ||
"title": "Interfaces", | ||
"description": "List available interfaces that dnsmasq can be configured on.", | ||
"type": "object", | ||
"properties": { | ||
"data": { | ||
"type": "array", | ||
"items": { | ||
"type": "string", | ||
"description": "Interface name to pass to configure-module." | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# | ||
# Copyright (C) 2024 Nethesis S.r.l. | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# | ||
|
||
import json | ||
import subprocess | ||
|
||
|
||
def __filter_interface(interface): | ||
""" | ||
Filter the interfaces that are not useful for the configuration. | ||
""" | ||
# filter by name | ||
interface_name = interface["ifname"] | ||
if interface_name.startswith("lo"): | ||
return False | ||
if interface_name.startswith("wg"): | ||
return False | ||
|
||
# filter by configuration status | ||
if "addr_info" not in interface: | ||
return False | ||
if interface["addr_info"] == []: | ||
return False | ||
|
||
# return True if the interface is not filtered | ||
return True | ||
|
||
|
||
def __format_interface(interface): | ||
""" | ||
Format the interface that can be used internally for responses. | ||
""" | ||
interface_data = { | ||
"name": interface["ifname"], | ||
"addresses": [] | ||
} | ||
for address in interface["addr_info"]: | ||
if address["family"] == "inet": | ||
interface_data["addresses"].append({ | ||
"address": address["local"], | ||
"netmask": address["prefixlen"], | ||
"broadcast": address["broadcast"] | ||
}) | ||
|
||
return interface_data | ||
|
||
|
||
def list_interfaces(): | ||
""" | ||
Returns a list of interfaces with their available addresses. | ||
""" | ||
subprocess_result = subprocess.run(["ip", "-j", "addr"], check=True, capture_output=True) | ||
return [__format_interface(interface) for interface in json.loads(subprocess_result.stdout) if __filter_interface(interface)] |