From 3057150744351b10dc9cb5b5551f58b90a4deda1 Mon Sep 17 00:00:00 2001 From: Igor Date: Sun, 15 Sep 2024 01:52:11 +0200 Subject: [PATCH 1/2] Add support for editing default route and DNS (#75) * Add support for editing default route and DNS * Update config.ng.docs.sh fixed missed typo --------- Co-authored-by: Joey Turner --- lib/armbian-configng/config.ng.docs.sh | 2 +- lib/armbian-configng/config.ng.jobs.json | 10 +++++++++ lib/armbian-configng/config.ng.network.sh | 26 +++++++++++++++-------- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/lib/armbian-configng/config.ng.docs.sh b/lib/armbian-configng/config.ng.docs.sh index c3c0fda1..81e05871 100644 --- a/lib/armbian-configng/config.ng.docs.sh +++ b/lib/armbian-configng/config.ng.docs.sh @@ -391,7 +391,7 @@ function see_full_list() { doc_link=$(jq -r ".menu[$i].sub[$j].doc_link" "$json_file") # Check if src_reference and doc_link are null - [ -z "$doc_link" == "" ] && doc_link="#$id_link" || doc_link="$doc_link" + [ -z "$doc_link" ] && doc_link="#$id_link" || doc_link="$doc_link" echo -e " - **$id** - $description" # echo -e " - Status: [$status]($doc_link)" diff --git a/lib/armbian-configng/config.ng.jobs.json b/lib/armbian-configng/config.ng.jobs.json index 1451729f..533a9c48 100644 --- a/lib/armbian-configng/config.ng.jobs.json +++ b/lib/armbian-configng/config.ng.jobs.json @@ -435,6 +435,16 @@ "src_reference": "", "author": "Igor Pecovnik", "condition": "" + }, + { + "id": "N90", + "description": "Display status", + "command": [ "show_message <<< \"$(sudo netplan status)\"" ], + "status": "Active", + "doc_link": "", + "src_reference": "", + "author": "Igor Pecovnik", + "condition": "" } ] }, diff --git a/lib/armbian-configng/config.ng.network.sh b/lib/armbian-configng/config.ng.network.sh index 2fa47ab2..15014d23 100644 --- a/lib/armbian-configng/config.ng.network.sh +++ b/lib/armbian-configng/config.ng.network.sh @@ -244,16 +244,23 @@ function choose_adapter() { if [[ $intf =~ $HIDE_IP_PATTERN ]]; then continue else - QUERY=$(ip -br addr show dev $interface | grep "^$type" | awk '{print $1" " $3}') + QUERY=$(ip -br addr show dev $interface | grep "^$type" | awk '{ print $1, " " , ($3==""?"unassigned":$3) }') [[ -n $QUERY ]] && LIST+=($QUERY) fi done LIST_LENGTH=$((${#LIST[@]}/2)); - adapter=$(whiptail --title "Select interface" --menu "" $((${LIST_LENGTH} + 8)) 40 $((${LIST_LENGTH})) "${LIST[@]}" 3>&1 1>&2 2>&3) - if [[ -n $adapter && adapter != "all-eth-interfaces" && "${getip}" != false ]]; then - address=$(ip -br addr show dev $adapter | awk '{print $3}') - address=$(whiptail --title "Enter new IP for $adapter" --inputbox "\nValid format: $address" 9 40 "$address" 3>&1 1>&2 2>&3) + if [[ -n $adapter && adapter != "all-eth-interfaces" && "${getip}" != false && $? == 0 ]]; then + address=$(ip -br addr show dev $adapter | awk '{print $3}') + address=$(whiptail --title "Enter IP for $adapter" --inputbox "\nValid format: $address" 9 40 "$address" 3>&1 1>&2 2>&3) + if [[ -n $address && $? == 0 ]]; then + defaultroute=$(ip route show default | grep "$adapter" | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]") + defaultroute=$(whiptail --title "Enter IP for default route" --inputbox "\nValid format: $defaultroute" 9 40 "$defaultroute" 3>&1 1>&2 2>&3) + if [[ -n $defaultroute && $? == 0 ]]; then + nameservers="9.9.9.9,1.1.1.1" + nameservers=$(whiptail --title "Enter DNS server" --inputbox "\nValid format: $nameservers" 9 40 "$nameservers" 3>&1 1>&2 2>&3) + fi + fi fi } @@ -273,9 +280,10 @@ module_options+=( function wifi_connect() { choose_adapter "w" "false" "true" - + # enable adapter + ip link set ${adapter} up LIST=() - 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}')) + LIST=($(iw dev ${adapter} scan 2> /dev/null | grep 'SSID\|^BSS' | cut -d" " -f2 | sed "s/(.*//g" | xargs -n2 -d'\n' | awk '{print $2,$1}')) LIST_LENGTH=$((${#LIST[@]}/2)); SELECTED_SSID=$(whiptail --title "Select SSID" --menu "rf" $((${LIST_LENGTH} + 6)) 50 $((${LIST_LENGTH})) "${LIST[@]}" 3>&1 1>&2 2>&3) if [[ -n $SELECTED_SSID ]]; then @@ -332,8 +340,8 @@ function netplan_wrapper() { rm -f /etc/netplan/10-dhcp-all-interfaces.yaml netplan set --origin-hint ${config} renderer=${renderer} netplan set --origin-hint ${config} ethernets.${adapter}.addresses=[$address] - netplan set --origin-hint ${config} ${type}.${adapter}.dhcp6=false - netplan set --origin-hint ${config} ${type}.${adapter}.dhcp4=false + netplan set --origin-hint ${config} ${type}.${adapter}.routes='[{"to":"0.0.0.0/0", "via": "'$defaultroute'","metric":200}]' + netplan set --origin-hint ${config} ${type}.${adapter}.nameservers.addresses=[$nameservers] show_message <<< "$(sudo netplan get ${type})" else [[ -n "${address}" ]] && show_message <<< "IP address is wrong. Try 1.2.3.4/5" From 11c3c88c0505b1ee0a8860d80674535143df481b Mon Sep 17 00:00:00 2001 From: Joey Turner Date: Sun, 15 Sep 2024 11:33:07 -0500 Subject: [PATCH 2/2] 67 feature request tree structure when listing commands (#77) * Refactor see_cmd_list to show "sub menus" properly * Renumber json for readbility nested menu represented with "." for each nesting. * Restored mistakenly deleted menu item --------- Co-authored-by: Tearran --- lib/armbian-configng/config.ng.docs.sh | 103 ++++++++++++++--------- lib/armbian-configng/config.ng.jobs.json | 82 +++++++++--------- 2 files changed, 102 insertions(+), 83 deletions(-) diff --git a/lib/armbian-configng/config.ng.docs.sh b/lib/armbian-configng/config.ng.docs.sh index 81e05871..71b92ac8 100644 --- a/lib/armbian-configng/config.ng.docs.sh +++ b/lib/armbian-configng/config.ng.docs.sh @@ -462,6 +462,7 @@ jq -r ' ' $json_file } + module_options+=( ["see_cmd_list,author"]="Joey Turner" ["see_cmd_list,ref_link"]="" @@ -472,52 +473,70 @@ module_options+=( ["see_cmd_list,doc_link"]="" ) # -# See command line options +# See command options # -function see_cmd_list() { - local script_name="${0##*/}" - local help_menu="$1" # Capture the first argument passed to --help - - cat << EOF -Usage: $script_name [option] [arguments] - - --help [category] - Use [category] to filter specific menu options. -EOF - - # Check if a specific menu was provided - if [[ -z "$help_menu" || "$help_menu" == "cmd" ]]; then - jq -r --arg script_name "$script_name" ' - # Define a function to process each menu item and include its parent menu id - def process_item(item; menu_id): - "\t --cmd " + item.id + " - " + (item.description // "No description available"), - # Recursively process sub-items, passing the parent menu_id - (item.sub[]? | process_item(. ; menu_id)); - - # Start by iterating through the main menu - .menu[] | - "\n " + .id + " - " + (.description // "No description available") , - .id as $menu_id | - # Process both the main menu and its sub-items - .sub[]? | process_item(.; $menu_id) - ' "$json_file" - elif [[ "$help_menu" == "api" ]]; then - see_use - else - # Parse the JSON file and display the filtered commands based on the menu - jq -r --arg menu "$help_menu" ' - # Function to process items and print commands - def process_item(item; menu_id): - " --cmd " + item.id + " - " + (item.description // "No description available"), - (item.sub[]? | process_item(. ; menu_id)); - - # Filter by the provided menu name and display only the matching menu and sub-commands - .menu[] | select(.id | ascii_downcase == ($menu | ascii_downcase)) | - (.sub[]? | process_item(.; .id)) - ' "$json_file" - fi +see_cmd_list() { + local help_menu="$1" + + if [[ -n "$help_menu" && "$help_menu" != "cmd" ]] ; then + echo "$json_data" | jq -r --arg menu "$help_menu" ' + def recurse_menu(menu; level): + menu | .id as $id | .description as $desc | + if has("sub") then + if level == 0 then + "\n \($id) - \($desc)\n" + (.sub | map(recurse_menu(. ; level + 1)) | join("\n")) + elif level == 1 then + " \($id) - \($desc)\n" + (.sub | map(recurse_menu(. ; level + 1)) | join("\n")) + else + " \($id) - \($desc)\n" + (.sub | map(recurse_menu(. ; level + 1)) | join("\n")) + end + else + if level == 0 then + " --cmd \($id) - \($desc)" + elif level == 1 then + " --cmd \($id) - \($desc)" + else + "\t--cmd \($id) - \($desc)" + end + end; + + # Find the correct menu if $menu is passed, otherwise show all + if $menu == "" then + .menu | map(recurse_menu(. ; 0)) | join("\n") + else + .menu | map(select(.id == $menu) | recurse_menu(. ; 0)) | join("\n") + end + ' + elif [[ -z "$1" || "$1" == "cmd" ]]; then + echo "$json_data" | jq -r --arg menu "$help_menu" ' + def recurse_menu(menu; level): + menu | .id as $id | .description as $desc | + if has("sub") then + if level == 0 then + "\n \($id) - \($desc)\n" + (.sub | map(recurse_menu(. ; level + 1)) | join("\n")) + elif level == 1 then + " \($id) - \($desc)\n" + (.sub | map(recurse_menu(. ; level + 1)) | join("\n")) + else + " \($id) - \($desc)\n" + (.sub | map(recurse_menu(. ; level + 1)) | join("\n")) + end + else + if level == 0 then + " --cmd \($id) - \($desc)" + elif level == 1 then + " --cmd \($id) - \($desc)" + else + "\t--cmd \($id) - \($desc)" + end + end; + .menu | map(recurse_menu(. ; 0)) | join("\n") + ' + + else echo "nope" + fi } + module_options+=( ["see_cli_legacy,author"]="Joey Turner" ["see_cli_legacy,ref_link"]="" diff --git a/lib/armbian-configng/config.ng.jobs.json b/lib/armbian-configng/config.ng.jobs.json index 533a9c48..2fb046c2 100644 --- a/lib/armbian-configng/config.ng.jobs.json +++ b/lib/armbian-configng/config.ng.jobs.json @@ -79,11 +79,11 @@ "condition": "[[ -n $(ls /sbin/armbian-install) ]]" }, { - "id": "S07", + "id": "S07.1", "description": "Manage SSH login options", "sub": [ { - "id": "SS01", + "id": "S07", "description": "Disable root login", "command": [ "sed -i \"s|^#\\?PermitRootLogin.*|PermitRootLogin no|\" /etc/ssh/sshd_config", @@ -96,7 +96,7 @@ "condition": "grep -q '^PermitRootLogin yes' /etc/ssh/sshd_config" }, { - "id": "SS02", + "id": "S08", "description": "Enable root login", "command": [ "sed -i \"s/^#\\?PermitRootLogin.*/PermitRootLogin yes/\" /etc/ssh/sshd_config" , @@ -109,7 +109,7 @@ "condition": "grep -q '^PermitRootLogin no' /etc/ssh/sshd_config" }, { - "id": "SS03", + "id": "S09", "description": "Disable password login", "command": [ "sed -i \"s/^#\\?PasswordAuthentication.*/PasswordAuthentication no/\" /etc/ssh/sshd_config" , @@ -122,7 +122,7 @@ "condition": "grep -q 'PasswordAuthentication yes' /etc/ssh/sshd_config" }, { - "id": "SS04", + "id": "S10", "description": "Enable password login", "command": [ "sed -i \"s/^#\\?PasswordAuthentication.*/PasswordAuthentication yes/\" /etc/ssh/sshd_config" , @@ -135,7 +135,7 @@ "condition": "grep -q 'PasswordAuthentication no' /etc/ssh/sshd_config" }, { - "id": "SS05", + "id": "S11", "description": "Disable Public key authentication login", "command": [ "sed -i \"s/^#\\?PubkeyAuthentication.*/PubkeyAuthentication no/\" /etc/ssh/sshd_config" , @@ -148,7 +148,7 @@ "condition": "grep -q 'PubkeyAuthentication yes' /etc/ssh/sshd_config" }, { - "id": "SS06", + "id": "S12", "description": "Enable Public key authentication login", "command": [ "sed -i \"s/^#\\?PubkeyAuthentication.*/PubkeyAuthentication yes/\" /etc/ssh/sshd_config" , @@ -161,7 +161,7 @@ "condition": "grep -q 'PubkeyAuthentication no' /etc/ssh/sshd_config" }, { - "id": "SS07", + "id": "S13", "description": "Disable OTP authentication", "command": [ "clear", @@ -177,7 +177,7 @@ "condition": "grep -q 'ChallengeResponseAuthentication yes' /etc/ssh/sshd_config" }, { - "id": "SS08", + "id": "S14", "description": "Enable OTP authentication", "command": [ "check_if_installed libpam-google-authenticator || debconf-apt-progress -- apt-get -y install libpam-google-authenticator", @@ -194,7 +194,7 @@ "condition": "! check_if_installed libpam-google-authenticator || ! check_if_installed qrencode || grep -q '^ChallengeResponseAuthentication no' /etc/ssh/sshd_config || ! grep -q 'ChallengeResponseAuthentication' /etc/ssh/sshd_config" }, { - "id": "SS09", + "id": "S15", "description": "Generate new OTP authentication QR code", "command": [ "qr_code generate" @@ -206,7 +206,7 @@ "condition": "grep -q '^ChallengeResponseAuthentication yes' /etc/ssh/sshd_config" }, { - "id": "SS10", + "id": "S16", "description": "Show OTP authentication QR code", "command": ["qr_code"], "status": "Active", @@ -218,7 +218,7 @@ ] }, { - "id": "S08", + "id": "S17", "description": "Change shell system wide to BASH", "command": [ "export BASHLOCATION=$(grep /bash$ /etc/shells | tail -1)", @@ -235,7 +235,7 @@ "condition": "[[ $(cat /etc/passwd | grep \"^root:\" | rev | cut -d\":\" -f1 | cut -d\"/\" -f1| rev) == \"zsh\" ]]" }, { - "id": "S09", + "id": "S18", "description": "Change shell system wide to ZSH", "command": [ "export ZSHLOCATION=$(grep /zsh$ /etc/shells | tail -1)", @@ -252,7 +252,7 @@ "condition": "[[ $(cat /etc/passwd | grep \"^root:\" | rev | cut -d\":\" -f1 | cut -d\"/\" -f1| rev) == \"bash\" ]]" }, { - "id": "S10", + "id": "S19", "description": "Switch to rolling release", "prompt": "This will switch to rolling releases\n\nwould you like to continue?", "command": [ "set_rolling" ], @@ -263,7 +263,7 @@ "condition": "grep -q 'apt.armbian.com' /etc/apt/sources.list.d/armbian.list && [[ -z \"$(apt-mark showhold)\" ]]" }, { - "id": "S11", + "id": "S20", "description": "Switch to stable release", "prompt": "This will switch to stable releases\n\nwould you like to continue?", "command": [ "set_stable" ], @@ -274,7 +274,7 @@ "condition": "grep -q 'beta.armbian.com' /etc/apt/sources.list.d/armbian.list && [[ -z \"$(apt-mark showhold)\" ]]" }, { - "id": "S12", + "id": "S21", "description": "Enable read only filesystem", "prompt": "This will enable Armbian read-only filesystem. Reboot is mandatory?\n\nWould you like to continue?", "command": [ "manage_overlayfs enable" ], @@ -285,7 +285,7 @@ "condition": "modinfo overlay > /dev/null 2>&1 && [[ -z $(findmnt -k /media/root-ro | tail -1) ]] && [[ \"${DISTRO}\"=Ubuntu ]]" }, { - "id": "S13", + "id": "S22", "description": "Disable read only filesystem", "prompt": "This will disable Armbian read-only filesystem. Reboot is mandatory?\n\nWould you like to continue?", "command": [ "manage_overlayfs disable" ], @@ -302,15 +302,15 @@ "description": "Fixed and wireless network settings", "sub": [ { - "id": "N01", + "id": "N01.1", "description": "Configure network interfaces", "sub": [ { - "id": "N02", + "id": "N01.1.1", "description": "Wired", "sub": [ { - "id": "N06", + "id": "N01", "description": "Show configuration", "command": [ "netplan_wrapper \"show_message\" \"\" \"\" \"ethernets\"" ], "status": "Active", @@ -320,7 +320,7 @@ "condition": "[ -f /etc/netplan/30-*static-interfaces.yaml ] || [ -f /etc/netplan/10-dhcp-all-interfaces.yaml ]" }, { - "id": "N07", + "id": "N02", "description": "Enable DHCP on all interfaces", "command": [ "netplan_wrapper \"dhcp_all_wired_interfaces\" \"false\" \"10-dhcp-all-interfaces\" \"ethernets\" \"networkd\"" ], "status": "Active", @@ -328,7 +328,7 @@ "condition": "[ ! -f /etc/netplan/10-dhcp-all-interfaces.yaml ]" }, { - "id": "N08", + "id": "N03", "description": "Set fixed IP address", "command": [" netplan_wrapper \"set_ip\" \"true\" \"10-dhcp-all-interfaces\" \"ethernets\" \"networkd\""], "status": "Active", @@ -338,7 +338,7 @@ "condition": "" }, { - "id": "N09", + "id": "N04", "description": "Disable IPV6", "command": [" netplan_wrapper \"disable_ipv6\" \"false\" \"10-dhcp-all-interfaces\" \"ethernets\" \"networkd\""], "status": "Pending Review", @@ -348,7 +348,7 @@ "condition": "[ -f /etc/netplan/30-*static-interfaces.yaml ] || [ -f /etc/netplan/10-dhcp-all-interfaces.yaml ]" }, { - "id": "N10", + "id": "N05", "description": "Enable IPV6", "command": [" netplan_wrapper \"enable_ipv6\" \"false\" \"10-dhcp-all-interfaces\" \"ethernets\" \"networkd\""], "status": "Pending Review", @@ -358,7 +358,7 @@ "condition": "[ -f /etc/netplan/30-*static-interfaces.yaml ] || [ -f /etc/netplan/10-dhcp-all-interfaces.yaml ]" }, { - "id": "N11", + "id": "N06", "description": "Disable wired networking", "command": [ "rm -f /etc/netplan/10-dhcp-all-interfaces.yaml /etc/netplan/30-*static-interfaces.yaml" ], "condition": "[ -f /etc/netplan/30-*static-interfaces.yaml ] || [ -f /etc/netplan/10-dhcp-all-interfaces.yaml ]", @@ -368,11 +368,11 @@ ] }, { - "id": "N03", + "id": "N01.1.2", "description": "Wireless", "sub": [ { - "id": "N25", + "id": "N07", "description": "Show configuration", "command": [ "netplan_wrapper \"show_message\" \"\" \"\" \"wifis\"" ], "condition": "[ -f /etc/netplan/20-dhcp-wlan-interface.yaml ]", @@ -380,7 +380,7 @@ "author": "Igor Pecovnik" }, { - "id": "N26", + "id": "N08", "description": "Disable wireless networking", "command": [ "rm -f /etc/netplan/20-dhcp-wlan-interface.yaml" ], "condition": "[ -f /etc/netplan/20-dhcp-wlan-interface.yaml ]", @@ -388,7 +388,7 @@ "author": "Igor Pecovnik" }, { - "id": "N27", + "id": "N09", "description": "Disable IPV6", "command": [" netplan_wrapper \"disable_ipv6\" \"false\" \"20-dhcp-wlan-interface\" \"wifis\" \"networkd\""], "condition": "[ -f /etc/netplan/20-dhcp-wlan-interface.yaml ]", @@ -396,7 +396,7 @@ "author": "Igor Pecovnik" }, { - "id": "N28", + "id": "N10", "description": "Enable IPV6", "command": [" netplan_wrapper \"enable_ipv6\" \"false\" \"20-dhcp-wlan-interface\" \"wifis\" \"networkd\""], "condition": "[ -f /etc/netplan/20-dhcp-wlan-interface.yaml ]", @@ -404,7 +404,7 @@ "author": "Igor Pecovnik" }, { - "id": "N29", + "id": "N11", "description": "Enable DHCP on wireless network interface", "command": [ "wifi_connect" @@ -416,7 +416,7 @@ ] }, { - "id": "N04", + "id": "N12", "description": "Show common configs", "command": [ "show_message <<< \"$(sudo netplan get all)\"" ], "status": "Active", @@ -426,7 +426,7 @@ "condition": "" }, { - "id": "N05", + "id": "N13", "description": "Apply common configs", "prompt": "This will apply new network configuration\n\nwould you like to continue?", "command": [ "netplan apply" ], @@ -437,7 +437,7 @@ "condition": "" }, { - "id": "N90", + "id": "N14", "description": "Display status", "command": [ "show_message <<< \"$(sudo netplan status)\"" ], "status": "Active", @@ -449,7 +449,7 @@ ] }, { - "id": "N13", + "id": "N15", "description": "Install Bluetooth support", "command": [ "see_current_apt ", @@ -463,7 +463,7 @@ "condition": "! check_if_installed bluetooth && ! check_if_installed bluez && ! check_if_installed bluez-tools" }, { - "id": "N14", + "id": "N16", "description": "Remove Bluetooth support", "command": [ "see_current_apt ", @@ -478,7 +478,7 @@ "condition": "check_if_installed bluetooth || check_if_installed bluez || check_if_installed bluez-tools" }, { - "id": "N15", + "id": "N17", "description": "Bluetooth Discover", "prompt": "This will enable bluetooth and discover devices\n\nWould you like to continue?", "command": [ "connect_bt_interface" ], @@ -489,7 +489,7 @@ "condition": "check_if_installed bluetooth || check_if_installed bluez || check_if_installed bluez-tools" }, { - "id": "N16", + "id": "N18", "description": "Toggle system IPv6/IPv4 internet protocol", "prompt": "This will toggle your internet protocol\nWould you like to continue?", "command": [ "toggle_ipv6 | show_infobox" ], @@ -558,7 +558,7 @@ "description": "Run/Install 3rd party applications", "sub": [ { - "id": "SW01", + "id": "SW01.1", "description": "Desktop Environments", "sub": [ { @@ -614,7 +614,7 @@ ] }, { - "id": "SW07", + "id": "SW01.2", "description": "Network tools", "sub": [ { @@ -730,7 +730,7 @@ ] }, { - "id": "SW16", + "id": "SW01.3", "description": "Development", "sub": [ {