Skip to content

Commit ed1412f

Browse files
committed
prove wrapper function
1 parent 0bb4b22 commit ed1412f

2 files changed

Lines changed: 146 additions & 65 deletions

File tree

lib/armbian-configng/config.ng.jobs.json

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -287,47 +287,59 @@
287287
"description": "Configure network interfaces",
288288
"sub": [
289289
{
290-
"id": "N03",
290+
"id": "N02",
291291
"description": "Wired",
292292
"sub": [
293293
{
294-
"id": "N04",
295-
"description": "Show configuration",
296-
"command": [ "show_message <<< \"$(sudo netplan get ethernets)\"" ],
297-
"condition": "[ -f /etc/netplan/30-*static-interfaces.yaml ] || [ -f /etc/netplan/10-dhcp-all-interfaces.yaml ]",
294+
"id": "N06",
295+
"description": "Show configuration",
296+
"command": [ "netplan_wrapper \"show_message\" \"\" \"\" \"ethernets\"" ],
298297
"status": "Active",
299-
"author": "Igor Pecovnik"
298+
"doc_link": "",
299+
"src_reference": "",
300+
"author": "Igor Pecovnik",
301+
"condition": "[ -f /etc/netplan/30-*static-interfaces.yaml ] || [ -f /etc/netplan/10-dhcp-all-interfaces.yaml ]"
300302
},
301303
{
302-
"id": "N05",
304+
"id": "N07",
303305
"description": "Enable DHCP on all interfaces",
304-
"command": [
305-
"rm -f /etc/netplan/30-*-static-interfaces.yaml",
306-
"netplan set --origin-hint 10-dhcp-all-interfaces renderer=networkd",
307-
"netplan set --origin-hint 10-dhcp-all-interfaces ethernets.all-eth-interfaces.dhcp4=true",
308-
"netplan set --origin-hint 10-dhcp-all-interfaces ethernets.all-eth-interfaces.dhcp6=true",
309-
"netplan set --origin-hint 10-dhcp-all-interfaces ethernets.all-eth-interfaces.match.name=e*",
310-
"show_message <<< \"$(sudo netplan get ethernets)\""
311-
],
306+
"command": [ "netplan_wrapper \"dhcp_all_wired_interfaces\" \"false\" \"10-dhcp-all-interfaces\" \"ethernets\" \"networkd\"" ],
312307
"status": "Active",
313308
"author": "Igor Pecovnik",
314309
"condition": "[ ! -f /etc/netplan/10-dhcp-all-interfaces.yaml ]"
315310
},
316311
{
317-
"id": "N06",
312+
"id": "N08",
318313
"description": "Set fixed IP address",
319-
"command": [
320-
"choose_adapter \"e\"",
321-
"[[ -n \"$IP_ADDRESS\" ]] && netplan_wrapper \"10-dhcp-all-interfaces\" \"ethernets\" \"networkd\" \"${SELECTED_ADAPTER}\" \"$IP_ADDRESS\""
322-
],
314+
"command": [" netplan_wrapper \"set_ip\" \"true\" \"10-dhcp-all-interfaces\" \"ethernets\" \"networkd\""],
323315
"status": "Active",
324316
"doc_link": "",
325317
"src_reference": "",
326318
"author": "Igor Pecovnik",
327-
"condition": "[ -f /etc/netplan/10-dhcp-all-interfaces.yaml ] || true"
319+
"condition": ""
328320
},
329321
{
330-
"id": "N07",
322+
"id": "N09",
323+
"description": "Disable IPV6",
324+
"command": [" netplan_wrapper \"disable_ipv6\" \"false\" \"10-dhcp-all-interfaces\" \"ethernets\" \"networkd\""],
325+
"status": "Pending Review",
326+
"doc_link": "",
327+
"src_reference": "",
328+
"author": "Igor Pecovnik",
329+
"condition": "[ -f /etc/netplan/30-*static-interfaces.yaml ] || [ -f /etc/netplan/10-dhcp-all-interfaces.yaml ]"
330+
},
331+
{
332+
"id": "N10",
333+
"description": "Enable IPV6",
334+
"command": [" netplan_wrapper \"enable_ipv6\" \"false\" \"10-dhcp-all-interfaces\" \"ethernets\" \"networkd\""],
335+
"status": "Pending Review",
336+
"doc_link": "",
337+
"src_reference": "",
338+
"author": "Igor Pecovnik",
339+
"condition": "[ -f /etc/netplan/30-*static-interfaces.yaml ] || [ -f /etc/netplan/10-dhcp-all-interfaces.yaml ]"
340+
},
341+
{
342+
"id": "N11",
331343
"description": "Disable wired networking",
332344
"command": [ "rm -f /etc/netplan/10-dhcp-all-interfaces.yaml /etc/netplan/30-*static-interfaces.yaml" ],
333345
"condition": "[ -f /etc/netplan/30-*static-interfaces.yaml ] || [ -f /etc/netplan/10-dhcp-all-interfaces.yaml ]",
@@ -337,39 +349,43 @@
337349
]
338350
},
339351
{
340-
"id": "N08",
352+
"id": "N03",
341353
"description": "Wireless",
342354
"sub": [
343355
{
344-
"id": "N09",
345-
"description": "Show configuration",
346-
"command": [ "show_message <<< \"$(sudo netplan get wifis)\"" ],
356+
"id": "N25",
357+
"description": "Show configuration",
358+
"command": [ "netplan_wrapper \"show_message\" \"\" \"\" \"wifis\"" ],
347359
"condition": "[ -f /etc/netplan/20-dhcp-wlan-interface.yaml ]",
348360
"status": "Active",
349361
"author": "Igor Pecovnik"
350362
},
351363
{
352-
"id": "N10",
364+
"id": "N26",
353365
"description": "Disable wireless networking",
354366
"command": [ "rm -f /etc/netplan/20-dhcp-wlan-interface.yaml" ],
355367
"condition": "[ -f /etc/netplan/20-dhcp-wlan-interface.yaml ]",
356368
"status": "Active",
357369
"author": "Igor Pecovnik"
358370
},
359371
{
360-
"id": "N11",
361-
"description": "Disable IPV6 in wireless configuration",
362-
"command": [
363-
"wifi_index=$(netplan get wifis | head -1 | cut -d\":\" -f1)",
364-
"netplan set --origin-hint 20-dhcp-wlan-interface wifis.$wifi_index.dhcp6=false",
365-
"show_message <<< \"$(sudo netplan get wifis)\""
366-
],
372+
"id": "N27",
373+
"description": "Disable IPV6",
374+
"command": [" netplan_wrapper \"disable_ipv6\" \"false\" \"20-dhcp-wlan-interface\" \"wifis\" \"networkd\""],
367375
"condition": "[ -f /etc/netplan/20-dhcp-wlan-interface.yaml ]",
368376
"status": "Active",
369377
"author": "Igor Pecovnik"
370378
},
371379
{
372-
"id": "N12",
380+
"id": "N28",
381+
"description": "Enable IPV6",
382+
"command": [" netplan_wrapper \"enable_ipv6\" \"false\" \"20-dhcp-wlan-interface\" \"wifis\" \"networkd\""],
383+
"condition": "[ -f /etc/netplan/20-dhcp-wlan-interface.yaml ]",
384+
"status": "Active",
385+
"author": "Igor Pecovnik"
386+
},
387+
{
388+
"id": "N29",
373389
"description": "Enable DHCP on wireless network interface",
374390
"command": [
375391
"wifi_connect"
@@ -381,16 +397,26 @@
381397
]
382398
},
383399
{
384-
"id": "N02",
385-
"description": "Apply configs",
400+
"id": "N04",
401+
"description": "Show common configs",
402+
"command": [ "show_message <<< \"$(sudo netplan get all)\"" ],
403+
"status": "Active",
404+
"doc_link": "",
405+
"src_reference": "",
406+
"author": "Igor Pecovnik",
407+
"condition": ""
408+
},
409+
{
410+
"id": "N05",
411+
"description": "Apply common configs",
386412
"command": [
387-
"get_user_continue \"This will apply new network configuration\n\nwould you like to continue?\" process_input",
388-
"netplan apply"
413+
"get_user_continue \"This will apply new network configuration\n\nwould you like to continue?\" process_input",
414+
"netplan apply"
389415
],
390416
"status": "Active",
391417
"doc_link": "",
392418
"src_reference": "",
393-
"author": "https://github.com/igorpecovnik",
419+
"author": "Igor Pecovnik",
394420
"condition": ""
395421
}
396422
]

lib/armbian-configng/config.ng.network.sh

Lines changed: 80 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,17 @@ module_options+=(
227227
["choose_adapter,status"]="review"
228228
)
229229
#
230-
# Function to check the internet connection
230+
# Function to select network adapter
231231
#
232232
function choose_adapter() {
233233

234-
local type=$1
235-
local getip=$2
234+
local type=$1 # w = wireless , e = ethernet
235+
local getip=$2 # true = also ask for new IP address
236+
local hide_all=$3 # true = hides selection for all-eth-interfaces
236237

237238
LIST=()
239+
# this functionality is exposed only on wired network
240+
[[ $hide_all != true && ${type} == e && -f /etc/netplan/10-dhcp-all-interfaces.yaml ]] && LIST=("all-eth-interfaces" "")
238241
HIDE_IP_PATTERN="^dummy0|^lo|^docker"
239242
for f in /sys/class/net/*; do
240243
interface=$(basename $f)
@@ -246,29 +249,41 @@ function choose_adapter() {
246249
fi
247250
done
248251
LIST_LENGTH=$((${#LIST[@]}/2));
249-
SELECTED_ADAPTER=$(whiptail --title "Select interface" --menu "" $((${LIST_LENGTH} + 8)) 40 $((${LIST_LENGTH})) "${LIST[@]}" 3>&1 1>&2 2>&3)
250-
if [[ -n $SELECTED_ADAPTER && "${getip}" != false ]]; then
251-
IP_ADDRESS=$(whiptail --title "Enter new IP for $SELECTED_ADAPTER" --inputbox "\nValid format: 1.2.3.4/5" 9 40 3>&1 1>&2 2>&3)
252+
adapter=$(whiptail --title "Select interface" --menu "" $((${LIST_LENGTH} + 8)) 40 $((${LIST_LENGTH})) "${LIST[@]}" 3>&1 1>&2 2>&3)
253+
if [[ -n $adapter && adapter != "all-eth-interfaces" && "${getip}" != false ]]; then
254+
address=$(whiptail --title "Enter new IP for $SELECTED_ADAPTER" --inputbox "\nValid format: 1.2.3.4/5" 9 40 3>&1 1>&2 2>&3)
252255
fi
253256

254257
}
255258

259+
module_options+=(
260+
["wifi_connect,author"]="Igor Pecovnik"
261+
["wifi_connect,ref_link"]=""
262+
["wifi_connect,feature"]="wifi_connect"
263+
["wifi_connect,desc"]="List and connect to wireless network"
264+
["wifi_connect,example"]="wifi_connect"
265+
["wifi_connect,doc_link"]=""
266+
["wifi_connect,status"]="review"
267+
)
268+
#
269+
# Function to list and connect to wireless network
270+
#
256271
function wifi_connect() {
257272

258-
choose_adapter "w" "false"
273+
choose_adapter "w" "false" "true"
259274

260275
LIST=()
261-
LIST=($(sudo iw dev ${SELECTED_ADAPTER} scan 2> /dev/null | grep 'SSID\|^BSS' | cut -d" " -f2 | sed "s/(.*//g" | xargs -n2 -d'\n' | awk '{print $2,$1}'))
276+
LIST=($(sudo iw dev ${adapter} scan 2> /dev/null | grep 'SSID\|^BSS' | cut -d" " -f2 | sed "s/(.*//g" | xargs -n2 -d'\n' | awk '{print $2,$1}'))
262277
LIST_LENGTH=$((${#LIST[@]}/2));
263278
SELECTED_SSID=$(whiptail --title "Select SSID" --menu "rf" $((${LIST_LENGTH} + 6)) 50 $((${LIST_LENGTH})) "${LIST[@]}" 3>&1 1>&2 2>&3)
264279
if [[ -n $SELECTED_SSID ]]; then
265280
SELECTED_PASSWORD=$(whiptail --title "Enter new password for $SELECTED_SSID" --passwordbox "" 7 50 3>&1 1>&2 2>&3)
266281
if [[ -n $SELECTED_PASSWORD ]]; then
267282
rm -f /etc/netplan/20-dhcp-wlan-interface
268283
netplan set --origin-hint 20-dhcp-wlan-interface renderer=networkd
269-
netplan set --origin-hint 20-dhcp-wlan-interface wifis.$SELECTED_ADAPTER.access-points."${SELECTED_SSID}".password=${SELECTED_PASSWORD}
270-
netplan set --origin-hint 20-dhcp-wlan-interface wifis.$SELECTED_ADAPTER.dhcp4=true
271-
netplan set --origin-hint 20-dhcp-wlan-interface wifis.$SELECTED_ADAPTER.dhcp6=true
284+
netplan set --origin-hint 20-dhcp-wlan-interface wifis.$adapter.access-points."${SELECTED_SSID}".password=${SELECTED_PASSWORD}
285+
netplan set --origin-hint 20-dhcp-wlan-interface wifis.$adapter.dhcp4=true
286+
netplan set --origin-hint 20-dhcp-wlan-interface wifis.$adapter.dhcp6=true
272287
fi
273288
fi
274289
}
@@ -277,25 +292,65 @@ module_options+=(
277292
["netplan_wrapper,author"]="Igor Pecovnik"
278293
["netplan_wrapper,ref_link"]=""
279294
["netplan_wrapper,feature"]="netplan_wrapper"
280-
["netplan_wrapper,desc"]="Displays available adapters"
295+
["netplan_wrapper,desc"]="Wrapping Netplan commands"
281296
["netplan_wrapper,example"]="netplan_wrapper"
282297
["netplan_wrapper,doc_link"]=""
283298
["netplan_wrapper,status"]="review"
284299
)
285300
#
286-
# Function to check the internet connection
301+
# Function to feed netplan CLI
287302
#
288303
function netplan_wrapper() {
289-
290-
local config=$1
291-
local type=$2
292-
local renderer=$3
293-
local adapter=$4
294-
local address=$5
295-
296-
#rm -f /etc/netplan/${config}.yaml
297-
netplan set --origin-hint ${config} renderer=${renderer}
298-
netplan set --origin-hint ${config} ethernets.${adapter}.addresses=[$address]
299-
show_message <<< "$(sudo netplan get ${type})"
300-
304+
local what=$1
305+
local get_ip=$2
306+
local config=$3
307+
local type=$4
308+
local renderer=$5
309+
local adapter=$6
310+
local address=$7
311+
312+
case "$1" in
313+
314+
show_message)
315+
show_message <<< $(sudo netplan get ${type})
316+
;;
317+
318+
dhcp_all_wired_interfaces)
319+
rm -f /etc/netplan/30-*-static-interfaces.yaml
320+
netplan set --origin-hint ${config} renderer=${renderer}
321+
netplan set --origin-hint ${config} ethernets.all-eth-interfaces.dhcp4=true
322+
netplan set --origin-hint ${config} ethernets.all-eth-interfaces.dhcp6=true
323+
netplan set --origin-hint ${config} ethernets.all-eth-interfaces.match.name=e*
324+
show_message <<< "$(sudo netplan get ${type})"
325+
;;
326+
327+
set_ip)
328+
choose_adapter "${type:0:1}" "${get_ip}" "true"
329+
if [[ "${address}" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\/[0-9]+$ ]]; then
330+
rm -f /etc/netplan/10-dhcp-all-interfaces.yaml
331+
netplan set --origin-hint ${config} renderer=${renderer}
332+
netplan set --origin-hint ${config} ethernets.${adapter}.addresses=[$address]
333+
netplan set --origin-hint ${config} ${type}.${adapter}.dhcp6=false
334+
netplan set --origin-hint ${config} ${type}.${adapter}.dhcp4=false
335+
show_message <<< "$(sudo netplan get ${type})"
336+
else
337+
[[ -n "${address}" ]] && show_message <<< "IP address is wrong. Try 1.2.3.4/5"
338+
fi
339+
;;
340+
341+
disable_ipv6)
342+
choose_adapter "${type:0:1}" "${get_ip}" "false"
343+
netplan set --origin-hint ${config} renderer=${renderer}
344+
netplan set --origin-hint ${config} ${type}.${adapter}.dhcp6=false
345+
show_message <<< "$(sudo netplan get ${type})"
346+
;;
347+
348+
enable_ipv6)
349+
choose_adapter "${type:0:1}" "${get_ip}" "true"
350+
netplan set --origin-hint ${config} renderer=${renderer}
351+
netplan set --origin-hint ${config} ${type}.${adapter}.dhcp6=true
352+
show_message <<< "$(sudo netplan get ${type})"
353+
;;
354+
*)
355+
esac
301356
}

0 commit comments

Comments
 (0)