@@ -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#
232232function 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+ #
256271function 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#
288303function 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