Refactor femto scripts to provide data in more easily utilized fashion

This commit is contained in:
nagumii 2025-02-01 17:30:18 +02:00
parent 1a1133d7b1
commit 051c42e7b6
4 changed files with 125 additions and 91 deletions

View file

@ -37,26 +37,46 @@ loading() {
dialog --no-collapse --infobox "$1" 5 45
}
# function to pull data from various femto scripts and convert it to the contents of a read only dialog --form
ingest_data() { # usage: `ingest_data "command.sh -argument" key_width value_width`
unset output_array #debug code, delete me
declare -a output_array
# Read the output from femto-meshtasticd-config.sh and store it in the array
## function to pull data from various femto scripts and convert it to the contents of a read only dialog --form
# ingest_data() { # usage: `ingest_data "command.sh -argument" key_width value_width`
# unset output_array #debug code, delete me
# declare -a output_array
# # Read the output from femto-meshtasticd-config.sh and store it in the array
# while IFS=':' read -r key value; do
# # Trim leading/trailing spaces from key and value
# key=$(echo "$key" | xargs)
# value=$(echo "$value" | xargs)
# output_array+=("$key=$value") # Use indexed array to preserve order
# done < <(eval $1)
# form_contents=""
# i=1
# for pair in "${output_array[@]}"; do
# key=$(cut -d'=' -f1 <<< "$pair")
# value=$(cut -d'=' -f2- <<< "$pair")
# form_contents+="\"$key\" $i 1 \"$value\" $i $2 $3 -1 "
# ((i++))
# done
# echo "$form_contents" | sed 's/\x1b\[[0-9;]*m//g'
# }
## CODE TO USE THIS FUNCTION:
# eval "$(echo -e "dialog --no-collapse --no-cancel --form \"Meshtastic node info\" 0 0 0 $(ingest_data "femto-meshtasticd-config.sh -i" 18 0) 2>/dev/null")"
# function to pull data from various femto scripts and convert it a simple text string, with dialog colors
ingest_data() { # usage: `ingest_data "command.sh -argument" key_width`
local output_string=""
# Read the output from femto-meshtasticd-config.sh and store output_string
while IFS=':' read -r key value; do
# Trim leading/trailing spaces from key and value
key=$(echo "$key" | xargs)
value=$(echo "$value" | xargs)
output_array+=("$key=$value") # Use indexed array to preserve order
key=$(replace_colors "$(echo "$key" | xargs)")
value=$(replace_colors "$(echo "$value" | xargs)")
if [[ -z "$key" && -z "$value" ]]; then
output_string+="---\n"
else
output_string+=$(printf "%s:%*s%s\n" "$key" $(($2 - 1 - ${#key})) "" "$value\n") # save the key and value to the output_string, adding padding between them as determined by `$2 - 1 - key-length` The 1 accounts for the :
fi
done < <(eval $1)
form_contents=""
i=1
for pair in "${output_array[@]}"; do
key=$(cut -d'=' -f1 <<< "$pair")
value=$(cut -d'=' -f2- <<< "$pair")
form_contents+="\"$key\" $i 1 \"$value\" $i $2 $3 -1 "
((i++))
done
echo "$form_contents" | sed 's/\x1b\[[0-9;]*m//g'
echo -e "$output_string"
}
wifi_config() {
@ -90,11 +110,10 @@ wifi_config() {
networking_settings() {
local title="Networking"
local title2="Wi-Fi (wlan0) Settings"
ip=$(hostname -I | sed 's/ / /g'); ip=${ip:+$(printf "IPv4 address: %s" "$ip")}
wpa_supplicant_conf="/etc/wpa_supplicant/wpa_supplicant.conf"
choice="" # zero the choice before loading the submenu
while true; do
networking_choice=$(dialog --no-collapse --cancel-label "Back" --backtitle "$ip" --default-item "$networking_choice" --title "$title" --menu "" $([ -z "$ip" ] && echo 14 || echo 16) 40 4 \
networking_choice=$(dialog --no-collapse --cancel-label "Back" --backtitle "IPv4 address: $(femto-network-config.sh -i)" --default-item "$networking_choice" --title "$title" --menu "" 16 40 4 \
1 "Turn Wi-Fi on/off" \
2 "View Wi-Fi settings" \
3 "Change Wi-Fi settings" \
@ -127,14 +146,14 @@ networking_settings() {
;;
2) # View Wi-Fi settings)
echo "Getting Wi-Fi configuration..."
dialog --no-collapse --title "$title2" --colors --msgbox "$(replace_colors "$(femto-network-config.sh -w)")" 0 0
dialog --no-collapse --title "$title2" --colors --msgbox "$(ingest_data "femto-network-config.sh -w" 15)" 0 0
;;
3) # Change Wi-Fi settings)
wifi_config
;;
4) # View ethernet settings)
echo "Getting ethernet configuration..."
dialog --no-collapse --title "Ethernet (eth0) settings" --colors --msgbox "$(echo -e "$(replace_colors "$(femto-network-config.sh -e)")")" 0 0
dialog --no-collapse --title "Ethernet (eth0) settings" --colors --msgbox "$(ingest_data "femto-network-config.sh -e" 15)" 0 0
;;
5) # Restart networking)
dialog --no-collapse --title "$title2" --yesno "Wi-Fi and Ethernet will be restarted.\nMeshtastic Wi-Fi setting will be set to ON.\n\nProceed?" 10 40
@ -184,16 +203,15 @@ utilities() {
1) #sysinfo)
choice="" # zero the choice before loading the submenu
while true; do
choice=$(dialog --no-collapse --help-button --help-label "Help" --cancel-label "Back" --default-item "$choice" --menu "System info" 17 40 5 \
choice=$(dialog --no-collapse --help-button --help-label "Help" --cancel-label "Back" --default-item "$choice" --menu "System info" 16 40 5 \
1 "CPU" \
2 "Operating system" \
3 "Storage & memory" \
4 "Peripherals & devices" \
5 "Networking" \
6 "Meshtastic node info" \
7 "Complete system info" \
" " "" \
8 "Back to previous menu" 3>&1 1>&2 2>&3)
7 "Back to previous menu" 3>&1 1>&2 2>&3)
exit_status=$? # This line checks the exit status of the dialog command
if [ $exit_status -eq 1 ]; then # Exit the loop if the user selects "Cancel" or closes the dialog
break
@ -203,33 +221,35 @@ utilities() {
case $choice in
1) # CPU)
loading "Loading CPU info..."
dialog --no-collapse --colors --title "CPU info" --msgbox "$(replace_colors "$(femto-utils.sh -c)")" 0 0
dialog --no-collapse --colors --title "CPU info" --msgbox "$(ingest_data "femto-utils.sh -c" 15)" 0 0
#dialog --no-collapse --colors --title "CPU info" --msgbox "$(replace_colors "$(femto-utils.sh -c)")" 0 0
;;
2) # Operating system)
loading "Loading operating system info..."
dialog --no-collapse --colors --extra-button --title "Operating system" --msgbox "$(replace_colors "$(femto-utils.sh -o)")" 0 0
dialog --no-collapse --colors --title "Operating system info" --msgbox "$(ingest_data "femto-utils.sh -o" 15)" 0 0
#dialog --no-collapse --colors --extra-button --title "Operating system" --msgbox "$(replace_colors "$(femto-utils.sh -o)")" 0 0
;;
3) # Storage & memory)
loading "Loading storage & memory info..."
dialog --no-collapse --colors --title "Storage & memory" --msgbox "$(replace_colors "$(femto-utils.sh -S)")" 0 0
dialog --no-collapse --colors --title "Storage & memory info" --msgbox "$(ingest_data "femto-utils.sh -S" 15)" 0 0
#dialog --no-collapse --colors --title "Storage & memory" --msgbox "$(replace_colors "$(femto-utils.sh -S)")" 0 0
;;
4) # Peripherals & devices)
loading "Loading peripherals & devices info..."
dialog --no-collapse --colors --title "Peripherals & devices" --msgbox "$(replace_colors "$(femto-utils.sh -p)")" 0 0
dialog --no-collapse --colors --title "Peripherals & devices info" --msgbox "$(ingest_data "femto-utils.sh -p" 15)" 0 0
#dialog --no-collapse --colors --title "Peripherals & devices" --msgbox "$(replace_colors "$(femto-utils.sh -p)")" 0 0
;;
5) # Networking)
loading "Loading networking info..."
dialog --no-collapse --colors --title "Networking" --msgbox "$(replace_colors "$(femto-utils.sh -n)")" 0 0
dialog --no-collapse --colors --title "Networking info" --msgbox "$(ingest_data "femto-utils.sh -n" 15)" 0 0
#dialog --no-collapse --colors --title "Networking" --msgbox "$(replace_colors "$(femto-utils.sh -n)")" 0 0
;;
6) # Meshtastic node info)
loading "Loading Meshtastic node info..."
dialog --no-collapse --colors --title "Meshtastic node info" --msgbox "$(replace_colors "$(femto-meshtasticd-config.sh -i)")" 0 0
dialog --no-collapse --colors --title "Meshtastic node info" --msgbox "$(ingest_data "femto-meshtasticd-config.sh -i" 15)" 0 0
#dialog --no-collapse --colors --title "Meshtastic node info" --msgbox "$(replace_colors "$(femto-meshtasticd-config.sh -i)")" 0 0
;;
7) # Complete system info)
loading "Loading complete system info..."
dialog --no-collapse --colors --title "System info" --msgbox "$(replace_colors "$(femto-utils.sh -i)")" 0 0
;;
8) break ;;
7) break ;;
esac
fi
done
@ -376,7 +396,7 @@ meshtastic_settings() {
case $meshtastic_choice in
1) # view node info)
loading "Getting node info..."
eval "$(echo -e "dialog --no-collapse --no-cancel --form \"Meshtastic node info\" 0 0 0 $(ingest_data "femto-meshtasticd-config.sh -i" 18 0) 2>/dev/null")"
dialog --no-collapse --colors --title "Meshtastic node info" --msgbox "$(ingest_data "sudo femto-meshtasticd-config.sh -i" 17)" 0 0
;;
2) # get current configuration URL & QR code)
loading "Getting Meshtastic QR code and URL..."

View file

@ -83,7 +83,7 @@ while getopts ":higkl:q:uU:rR:aA:cpo:sM:Stwuxm" opt; do
i) # Option -i (Get important node info)
declare -a output_array
output=$(meshtastic --host --info)
output_array+=("Service status=$(femto-meshtasticd-config.sh -S)")
output_array+=("Service=$(femto-meshtasticd-config.sh -S)")
output_array+=("Version=$(echo "$output" | grep -oP '"firmwareVersion":\s*"\K[^"]+' | head -n 1)")
output_array+=("Node name=$(echo "$output" | grep -oP 'Owner:\s*\K.*' | head -n 1)")
output_array+=("NodeID=$(echo "!$(printf "%08x\n" $(echo "$output" | grep -oP '"myNodeNum":\s*\K\d+' | head -n 1))")")
@ -107,14 +107,14 @@ while getopts ":higkl:q:uU:rR:aA:cpo:sM:Stwuxm" opt; do
output_array+=("Hop limit=$(echo "$output" | grep -oP '"hopLimit":\s*\K\w+')")
freq_slot=$(echo "$output" | grep -oP '"channelNum":\s*\K\d+' | head -n 1)
if [ "$freq_slot" != 0 ]; then # only display frequency slot if not 0
freq_slot+=("Override freq=$freq_slot")
freq_slot+=("Freq slot=$freq_slot")
fi
override_freq=$(echo "$output" | grep -oP '"overrideFrequency":\s*\K[0-9.]+')
if [ "$override_freq" != "0" ]; then # only display override frequency if not 0
output_array+=("Override freq=$override_freq")
output_array+=("Over. freq=$override_freq")
fi
output_array+=("Public key=$(echo "$output" | grep -oP '"publicKey":\s*"\K[^"]+' | head -n 1)")
output_array+=("Nodes in nodedb=$(echo "$output" | grep -oP '"![a-zA-Z0-9]+":\s*\{' | wc -l)")
output_array+=("Nodes in db=$(echo "$output" | grep -oP '"![a-zA-Z0-9]+":\s*\{' | wc -l)")
# now, echo the array
for pair in "${output_array[@]}"; do
key=$(echo "$pair" | cut -d'=' -f1)

View file

@ -15,6 +15,7 @@ Options are:
-c "COUNTRY" Set Wi-Fi 2-letter country code (such as US, DE)
-r Restart Wi-Fi
-e Get ethernet settings
-i Get IPv4 addresses
-w Get Wi-Fi settings
-n "HOSTNAME" Change hostname
-t Test internet connection
@ -37,7 +38,7 @@ if [ $# -eq 0 ]; then
fi
# Parse options
while getopts ":hx:Ts:p:c:ewn:tr" opt; do
while getopts ":hx:Ts:p:c:eiwn:tr" opt; do
case ${opt} in
h) # Option -h (help)
echo -e "$help"
@ -77,42 +78,52 @@ while getopts ":hx:Ts:p:c:ewn:tr" opt; do
;;
e) # Option -e (ethernet settings)
status=$(ip link show eth0 | grep -o 'state [A-Za-z]*' | awk '{print $2}')
echo -e "Ethernet status: $([ "$status" == "UP" ] && echo -e "\033[4m\033[0;34mconnected\033[0m" || echo -e "\033[0;31mdisconnected\033[0m")"
echo -e "\
Eth status:$([ "$status" == "UP" ] && echo -e "\033[4m\033[0;34mconnected\033[0m" || echo -e "\033[0;31mdisconnected\033[0m")"
if [ "$status" == "UP" ]; then
echo "IPv4 Address: $(ifconfig eth0 | grep 'inet ' | awk '{print $2}')\n\
IPv6 Address: $(ifconfig eth0 | grep 'inet6 ' | awk '{print $2}')"
echo -e "\
IPv4 Address:$(ifconfig eth0 | grep 'inet ' | awk '{print $2}')
IPv6 Address:$(ifconfig eth0 | grep 'inet6 ' | awk '{print $2}')"
fi
echo -e "MAC Address: $(ifconfig eth0 | grep 'ether ' | awk '{print $2}')\n\
Hostname: $(hostname).local"
echo -e "\
MAC Address:$(ifconfig eth0 | grep 'ether ' | awk '{print $2}')\n\
Hostname:$(hostname).local"
;;
w) # Option -w (wifi settings)
wifi_settings="SSID: $(grep -m 1 '^\s*ssid=' "$wpa_supplicant_conf" | cut -d '"' -f 2)\n\
Password: (hidden)\n\
Country: $(grep -m 1 "^country=" "$wpa_supplicant_conf" | cut -d '=' -f 2)"
wifi_settings="\
SSID:$(grep -m 1 '^\s*ssid=' "$wpa_supplicant_conf" | cut -d '"' -f 2)\n\
Password:(hidden)\n\
Country:$(grep -m 1 "^country=" "$wpa_supplicant_conf" | cut -d '=' -f 2)"
if ip link show wlan0 2>/dev/null | grep -q "wlan0"; then
mac_address_line="MAC address: $(ifconfig wlan0 | grep ether | awk '{print $2}')\n"
mac_address_line="\
MAC address:$(ifconfig wlan0 | grep ether | awk '{print $2}')"
if [ "$(cat /etc/wifi_state.txt)" = "up" ]; then
echo -e "Wi-Fi status: \033[4m\033[0;34menabled\033[0m\n\
Connected to: $(iwconfig 2>/dev/null | grep -i 'ESSID' | awk -F 'ESSID:"' '{print $2}' | awk -F '"' '{print $1}' | sed 's/^$/none/')\n\
Signal Strength: $(iwconfig 2>/dev/null | grep -i 'Signal level' | awk -F 'Signal level=' '{print $2}' | awk '{print $1}')\n\
Current IP: $(hostname -I | awk '{print $1}')\n
Hostname: $(hostname).local\n\
echo -e "\
Wi-Fi status:\033[4m\033[0;34menabled\033[0m\n\
Connected to:$(iwconfig 2>/dev/null | grep -i 'ESSID' | awk -F 'ESSID:"' '{print $2}' | awk -F '"' '{print $1}' | sed 's/^$/none/')\n\
Sig. strength:$(iwconfig 2>/dev/null | grep -i 'Signal level' | awk -F 'Signal level=' '{print $2}' | awk '{print $1}')\n\
Current IP:$(hostname -I | awk '{print $1}')\n
Hostname:$(hostname).local\n\
$mac_address_line"
else
if ip link show wlan0 &>/dev/null; then # if wlan0 exists
echo -e "Wi-Fi status: \033[0;31mdisabled\033[0m\n\
echo -e "\
Wi-Fi status:\033[0;31mdisabled\033[0m\n\
\n\
$wifi_settings"
fi
fi
else
echo -e "Wi-Fi status: \033[0;31mwlan0 not detected\033[0m\n\
echo -e "\
Wi-Fi status:\033[0;31mwlan0 not detected\033[0m\n\
\n\
Wi-Fi settings:\n\
$wifi_settings"
fi
;;
i) # Option -i (Get all IPv4 addresses)
hostname -I | awk '{for(i=1; i<=NF; i++) {if(i>1) printf ", "; printf $i} print ""}'
;;
t) # Option -t (test internet connection)
# Define ping targets and initialize counter
targets=("1.1.1.1" "8.8.8.8")

View file

@ -64,24 +64,26 @@ cpu_info() {
local cpu_speed="$(lscpu | grep "CPU min MHz" | awk '{print int($4)}')-$(lscpu | grep "CPU max MHz" | awk '{print int($4)}')mhz"
local cpu_serial="$(awk '/Serial/ {print $3}' /proc/cpuinfo)"
echo -e "Core: $core\n\
Model: $cpu_model\n\
Architecture: $cpu_architecture\n\
Speed: $cpu_speed x $(nproc) cores\n\
Temperature: $cpu_temp\n\
Serial # $cpu_serial"
echo -e "\
Core:$core\n\
Model:$cpu_model\n\
Architecture:$cpu_architecture\n\
Speed:$cpu_speed x $(nproc) cores\n\
Temperature:$cpu_temp\n\
Serial #:$cpu_serial"
}
storage_info() {
local microsd_size="$(df --block-size=1 / | awk 'NR==2 {total=$2; avail=$4; total_human=sprintf("%.2f", total/1024/1024/1024); avail_human=sprintf("%.2f", avail/1024/1024/1024); printf "%.2f GB (%.2f%% free)", total_human, (avail/total)*100}')"
local memory="$(free -m | awk 'NR==2{printf "%d MB (%.2f%% free)\n", $2, 100 - (($3/$2)*100)}')"
local swap="$(free -m | awk 'NR==3 {if ($2 > 1000) {printf "%.2f GB (%.2f%% free)", $2/1024, ($4/$2)*100} else {printf "%d MB (%.2f%% free)", $2, ($4/$2)*100}}')"
local microsd_size="$(df --block-size=1 / | awk 'NR==2 {total=$2; avail=$4; total_human=sprintf("%.2f", total/1024/1024/1024); avail_human=sprintf("%.2f", avail/1024/1024/1024); printf "%.2f GB (%.2f%% free)", total_human, (avail/total)*100}')"
local memory="$(free -m | awk 'NR==2{printf "%d MB (%.2f%% free)\n", $2, 100 - (($3/$2)*100)}')"
local swap="$(free -m | awk 'NR==3 {if ($2 > 1000) {printf "%.2f GB (%.2f%% free)", $2/1024, ($4/$2)*100} else {printf "%d MB (%.2f%% free)", $2, ($4/$2)*100}}')"
local mounted_drives="$( [ "$(for dir in /mnt/*/; do [ -d "$dir" ] && echo -n "/mnt${dir#"/mnt"} "; done)" ] && echo "$(for dir in /mnt/*/; do [ -d "$dir" ] && echo -n "/mnt${dir#"/mnt"} "; done | sed 's/\/$//')" || echo "none" )"
echo -e "microSD size: $microsd_size\n\
Memory: $memory\n\
Swap: $swap\n\
Mounted drives: $mounted_drives"
echo -e "\
microSD size:$microsd_size\n\
Memory:$memory\n\
Swap:$swap\n\
Mnted drives:$mounted_drives"
}
os_info() {
@ -92,21 +94,22 @@ os_info() {
local kernel_active_modules="$(lsmod | awk 'NR>1 {print $1}' | tr '\n' ' ' && echo)"
local kernel_boot_modules="$(modules=$(sed -n '6,$p' /etc/modules | sed ':a;N;$!ba;s/\n/, /g;s/, $//'); [ -z "$modules" ] && echo "none" || echo "$modules")"
echo -e "Operating System: $os_version\n\
Kernel version: $(uname -r)\n\
Uptime: $system_uptime\n\
Logging: $logging_enabled\n\
Activity LED: $act_led\n\
System time: $(date)\n\
K modules active: $kernel_active_modules\n\
K boot modules: $kernel_boot_modules"
echo -e "\
OS:$os_version\n\
Kernel ver:$(uname -r)\n\
Uptime:$system_uptime\n\
Logging:$logging_enabled\n\
Activity LED:$act_led\n\
System time:$(date)\n\
K mods active:$kernel_active_modules\n\
K boot mods:$kernel_boot_modules"
}
networking_info() {
local wifi_status="$(femto-network-config.sh -w | grep -v '^$' | grep -v '^Hostname')" #remove hostname line, as it's identical to the one in ethernet settings
local eth_status="$(femto-network-config.sh -e)"
echo -e "$wifi_status\n\
---
$eth_status"
}
@ -119,7 +122,7 @@ peripherals_info() {
local uart3_state="$([ "$(awk -F= '/^UART3_M1_STATUS/ {print $2}' /etc/luckfox.cfg)" -eq 1 ] && echo "enabled" || echo "disabled")"
local uart4_state="$([ "$(awk -F= '/^UART4_M1_STATUS/ {print $2}' /etc/luckfox.cfg)" -eq 1 ] && echo "enabled" || echo "disabled")"
local lora_radio="$(femto-meshtasticd-config.sh -k)"
local usb_devices="$(lsusb | grep -v 'root hub' | awk 'NR>0{printf "USB: "; for(i=7;i<=NF;i++) printf "%s ", $i; print ""} END {if (NR == 0) printf "USB: none detected"}')"
local usb_devices="$(lsusb | grep -v 'root hub' | awk 'NR>0{printf "USB:"; for(i=7;i<=NF;i++) printf "%s ", $i; print ""} END {if (NR == 0) printf "USB:none detected"}')"
# gather i2c addresses
i2c_addresses="" # initialize empty string to collect populated addresses
while IFS= read -r line; do # iterate over each row of output (captured at end of while loop)
@ -136,16 +139,16 @@ peripherals_info() {
done <<< "$(echo "$(sudo i2cdetect -y 3)")"
[[ -z "$i2c_addresses" ]] && i2c_addresses="none detected" # if no addresses found, "none detected"
echo -e "LoRa radio: $lora_radio\n\
echo -e "LoRa radio:$lora_radio\n\
$usb_devices\n\
i2c devices: $i2c_addresses\n\
USB mode: $usb_mode\n\
SPI-0 state: $spi0_state\n\
SPI-0 speed: $spi0_speed\n\
i2c-3 state: $i2c3_state\n\
i2c-3 speed: $i2c3_speed\n\
UART-3 state: $uart3_state\n\
UART-4 state: $uart4_state"
i2c devices:$i2c_addresses\n\
USB mode:$usb_mode\n\
SPI-0 state:$spi0_state\n\
SPI-0 speed:$spi0_speed\n\
i2c-3 state:$i2c3_state\n\
i2c-3 speed:$i2c3_speed\n\
UART-3 state:$uart3_state\n\
UART-4 state:$uart4_state"
}
all_system_info() {
@ -154,7 +157,7 @@ all_system_info() {
CPU:\n\
$(cpu_info)\n\
\n\
OS:\n\
Operating System:\n\
$(os_info)\n\
\n\
Storage:\n\