Total easymesh.sh cleanup!

main
Jason Hawks 10 months ago
parent ed801e2a37
commit 8d9ef1fbfb

@ -1,38 +1,76 @@
#!/bin/bash #!/bin/bash
clear_old_networks() { clear_mesh_radio() {
# Get the mesh name from UCI config # Get the radio to be used for mesh from the config
AP_RADIO=$(uci -q get easymesh.config.apRadio)
# Get the mesh name from the UCI configuration
MESH_NAME=$(uci -q get easymesh.config.mesh_id) MESH_NAME=$(uci -q get easymesh.config.mesh_id)
# Get all the radios and go through them one by one to remove old networks and interfaces with the same mesh name # Check if MESH_NAME is not empty
for CURRENT_RADIO in $(uci -X show wireless | grep wifi-device | awk -F'[.=]' '{print $2}'); do if [ -z "$MESH_NAME" ]; then
echo "Clearing old networks for radio: $CURRENT_RADIO" echo "Mesh name is not set. Please check the easymesh configuration."
exit 1
fi
# Use awk to parse the 'uci show wireless' output and find the matching section # Get the output from uci show wireless
matched_section=$(uci show wireless | awk -F. -v radio="$CURRENT_RADIO" -v ssid="$MESH_NAME" ' uci_output=$(uci show wireless)
$0 ~ /^wireless\.wifinet[0-9]+\.device=/ && $3 ~ radio { device_section=$2 }
$0 ~ /^wireless\.wifinet[0-9]+\.ssid=/ && $3 ~ ssid && device_section == $2 { print $2; exit }
')
# Check if a matching section was found # Find the mesh network with the matching mesh_id and delete it
if [ -n "$matched_section" ]; then mesh_id=$(echo "$uci_output" | grep -o "wireless\.mesh_radio[0-9]*\.mesh_id='$MESH_NAME'")
echo "The matching wireless interface section is: $matched_section - deleting it" if [ ! -z "$mesh_id" ]; then
uci delete wireless.$matched_section # Extract the number from the interface name
mesh_num=$(echo "$mesh_id" | grep -o "[0-9]*")
if [ "$AP_RADIO" == "all" ]; then
# Loop through all of the radios and delete the mesh networks
for radio in $(uci -X show wireless | grep wifi-device | awk -F'[.=]' '{print $2}'); do
# Delete the network
uci del wireless.mesh_$radio
echo "Deleted mesh network with mesh_id '$MESH_NAME' on radio$radio"
done
## Delete the network
#uci del wireless.mesh_radio$mesh_num
#echo "Deleted mesh network with mesh_id '$MESH_NAME' on radio$mesh_num"
elif [ "$mesh_num" == `echo "$AP_RADIO" | grep -o "[0-9]*"` ]; then
# Delete the network
uci del wireless.mesh_$mesh_num
echo "Deleted mesh network with mesh_id '$MESH_NAME' on radio$mesh_num"
else
echo "Skipping mesh network with mesh_id '$MESH_NAME' on radio$mesh_num"
fi
fi fi
# Find and delete existing mesh network interfaces with the specified MESH_NAME on the current radio # Find the wireless network with the matching ssid and delete it
EXISTING_MESH=$(uci show wireless | grep -w "mesh_id='$MESH_NAME'" | grep ".$CURRENT_RADIO." | cut -d'.' -f1-2) ssid=$(echo "$uci_output" | grep -o "wireless\.wifinet[0-9]*\.ssid='$MESH_NAME'")
for section in $EXISTING_MESH; do if [ ! -z "$ssid" ]; then
uci delete $section # Extract the number from the interface name
echo "The matching wireless interface section is: $section - deleting it" ssid_num=$(echo "$ssid" | grep -o "[0-9]*")
if [ "$AP_RADIO" == "all" ]; then
# Loop through all of the radios and delete the wireless networks
for radio in $(uci -X show wireless | grep wifi-device | awk -F'[.=]' '{print $2}'); do
# Get the number from the radio name
radio_num="${radio#radio}"
# Delete the network
uci del wireless.wifinet$radio_num
echo "Deleted wireless network with ssid '$MESH_NAME' on radio$radio_num"
done done
elif [ "$ssid_num" == "`echo "$AP_RADIO" | grep -o "[0-9]*"`" ]; then
# Delete the network
uci del wireless.wifinet$ssid_num
echo "Deleted wireless network with ssid '$MESH_NAME' on radio$ssid_num"
else
echo "Skipping wireless network with ssid '$MESH_NAME' on radio$ssid_num"
fi
fi
# Commit changes to wireless # Commit changes to make sure the wireless configuration is updated
uci commit uci commit wireless
done # Restart wireless to apply changes
wifi reload
wifi up
echo "Wireless interfaces reloaded."
} }
create_bat0() { create_batman_network() {
# Check if bat0 already exists # Check if bat0 already exists
if uci -q get network.bat0 >/dev/null; then if uci -q get network.bat0 >/dev/null; then
echo "bat0 interface already exists." echo "bat0 interface already exists."
@ -73,10 +111,13 @@ create_bat0() {
echo "Old bat0 interface deleted from lan network." echo "Old bat0 interface deleted from lan network."
fi fi
# # Add 'bat0' to the list of ports for 'br-lan' uci commit network
# uci add_list network.@device[0].ports='bat0' fi
# echo "bat0 has been added to the lan network."
# Check if network.private_router_batman already exists
if uci -q get network.private_router_batman >/dev/null; then
echo "network.private_router_batman interface already exists."
else
# Get the LAN bridge name # Get the LAN bridge name
LAN_NAME=$(uci -q get network.@device[0].name) LAN_NAME=$(uci -q get network.@device[0].name)
@ -91,7 +132,7 @@ create_bat0() {
fi fi
} }
find_radios() { process_radios() {
# Get the radio to be used for mesh from the config # Get the radio to be used for mesh from the config
AP_RADIO=$(uci -q get easymesh.config.apRadio) AP_RADIO=$(uci -q get easymesh.config.apRadio)
@ -101,28 +142,20 @@ find_radios() {
exit 1 exit 1
fi fi
# Check to make sure if "all" is passed or a single radio # Determine which radios to process
if [ "$AP_RADIO" == "all" ]; then if [ "$AP_RADIO" == "all" ]; then
# Get the list of wifi devices # If 'all' is specified, get all the radios
WIFI_RADIOS=$(uci -X show wireless | awk -F'[.=]' '/wifi-device/ {print $2}' | tr '\n' ' ') RADIO_LIST=$(uci -X show wireless | grep wifi-device | awk -F'[.=]' '{print $2}')
else else
# Set the radio to the one passed in the config # If a specific radio is specified, use only that radio
WIFI_RADIOS=$AP_RADIO RADIO_LIST=$AP_RADIO
fi fi
}
process_radios() { # Loop through the selected radios
# Check if WIFI_RADIOS contains more than one radio for CURRENT_RADIO in $RADIO_LIST; do
if [[ $WIFI_RADIOS =~ [[:space:]] ]]; then echo "Setting up mesh networks for: $CURRENT_RADIO"
# Loop through all the radios and set them up setup_mesh_radio $CURRENT_RADIO
for radio in $WIFI_RADIOS; do
echo "Multiple Radio Setup, Current Radio: $radio"
setup_mesh_radio $radio
done done
else
echo "Calling Single Radio Setup: $WIFI_RADIOS"
setup_mesh_radio $WIFI_RADIOS
fi
} }
# This is called from the process_radios function and is passed the radio to be used for mesh # This is called from the process_radios function and is passed the radio to be used for mesh
@ -136,12 +169,6 @@ setup_mesh_radio() {
# Commit changes to wireless # Commit changes to wireless
uci commit wireless uci commit wireless
# uci set network.nwi_mesh_$CURRENT_RADIO=interface
# uci set network.nwi_mesh_$CURRENT_RADIO.proto='batadv_hardif'
# uci set network.nwi_mesh_$CURRENT_RADIO.master='bat0'
# uci set network.nwi_mesh_$CURRENT_RADIO.device='bat0'
# uci set network.nwi_mesh_$CURRENT_RADIO.mtu='1536'
uci set wireless.mesh_$CURRENT_RADIO='wifi-iface' uci set wireless.mesh_$CURRENT_RADIO='wifi-iface'
uci set wireless.mesh_$CURRENT_RADIO.device="$CURRENT_RADIO" uci set wireless.mesh_$CURRENT_RADIO.device="$CURRENT_RADIO"
uci set wireless.mesh_$CURRENT_RADIO.ifname="mesh_$CURRENT_RADIO" uci set wireless.mesh_$CURRENT_RADIO.ifname="mesh_$CURRENT_RADIO"
@ -223,22 +250,6 @@ setup_mesh_radio() {
uci commit wireless uci commit wireless
fi fi
# Check if we need to setup iapp
ENABLE_IAPP=$(uci -q get easymesh.config.iapp)
if [ "$ENABLE_IAPP" = 1 ]; then
# Get the LAN interface name
LAN_NAME=$(uci -q get network.@device[0].name)
# If LAN_NAME is not empty, set the iapp interface
if [ ! -z "$LAN_NAME" ]; then
uci set wireless.wifinet$RADIO_NUM.iapp_interface="$LAN_NAME"
uci commit wireless
fi
else
uci -q delete wireless.wifinet$RADIO_NUM.iapp_interface
uci commit wireless
fi
# If KVR is enabled, setup DAWN # If KVR is enabled, setup DAWN
if [ "$ENABLE_KVR" = 1 ]; then if [ "$ENABLE_KVR" = 1 ]; then
RSSI=$(uci -q get easymesh.config.rssi_val) RSSI=$(uci -q get easymesh.config.rssi_val)
@ -256,61 +267,58 @@ setup_mesh_radio() {
} }
restart_and_reload() { restart_and_reload() {
# Enable all the radios and commit the changes at once # Get the radio to be used for mesh from the config
AP_RADIO=$(uci -q get easymesh.config.apRadio)
# Check if AP_RADIO is empty, if so exit
if [ -z "$AP_RADIO" ]; then
echo "No radio specified in the config, exiting."
exit 1
fi
# Check to make sure if "all" is passed or a single radio
if [ "$AP_RADIO" == "all" ]; then
# If 'all' is specified, enable all radios
for radio in $(uci -X show wireless | grep wifi-device | awk -F'[.=]' '{print $2}'); do for radio in $(uci -X show wireless | grep wifi-device | awk -F'[.=]' '{print $2}'); do
echo "Enabling $radio..." echo "Enabling $radio..."
uci set wireless.$radio.disabled=0 uci set wireless.$radio.disabled=0
done done
uci commit else
echo "All radios enabled." # If a specific radio is specified, enable only that radio
echo "Enabling $AP_RADIO..."
uci set wireless.$AP_RADIO.disabled=0
fi
# Apply the wireless configuration changes uci commit wireless
# Reload wifi to apply changes without restarting all interfaces
wifi reload wifi reload
wifi up
echo "Wireless interfaces reloaded." echo "Wireless interfaces reloaded."
# Apply network configuration changes # Apply network configuration changes
# Note: Only use reload_config if you have made changes to the network config
# and need to apply them. Otherwise, you can skip this step.
reload_config reload_config
echo "Network configuration reloaded." echo "Network configuration reloaded."
/etc/init.d/network reload /etc/init.d/network reload
/etc/init.d/network restart
} }
disable_easymesh() { disable_batman_interfaces() {
# Delete the bat0 interface # Delete the bat0 interface
if [ "$(uci -q get network.bat0)" = "interface" ]; then if [ "$(uci -q get network.bat0)" = "interface" ]; then
uci del network.bat0 uci del network.bat0
fi fi
# Get the current list of ports for the 'br-lan' interface # Delete the private_router_batman network interface
LAN_PORTS=$(uci -q get network.@device[0].ports)
# Check if 'bat0' is already in the list of ports for 'br-lan'
if echo "$LAN_PORTS" | grep -q 'bat0'; then
uci del_list network.@device[0].ports='bat0'
echo "Old bat0 interface deleted from lan network."
fi
# Get the list of wifi devices
WIFI_RADIOS=$(uci -X show wireless | awk -F'[.=]' '/wifi-device/ {print $2}' | tr '\n' ' ')
# Loop through all the radios and delete the network interfaces
for CURRENT_RADIO in $WIFI_RADIOS; do
echo "Handling removal of mesh networks for: $CURRENT_RADIO"
if [ "$(uci -q get network.private_router_batman)" = "interface" ]; then if [ "$(uci -q get network.private_router_batman)" = "interface" ]; then
uci del network.private_router_batman uci del network.private_router_batman
uci commit network
fi fi
done
uci commit uci commit network
reload_config reload_config
echo "Network configuration reloaded."
/etc/init.d/network reload /etc/init.d/network reload
/etc/init.d/network restart echo "Network configuration reloaded."
} }
set_apmode() { set_apmode() {
@ -326,11 +334,6 @@ set_apmode() {
cp /etc/config/network /etc/config/network.meshbak cp /etc/config/network /etc/config/network.meshbak
cp /etc/config/dhcp /etc/config/dhcp.meshbak cp /etc/config/dhcp /etc/config/dhcp.meshbak
# Generate a random IP in the same subnet for fun
# ip_base=$(echo "$dns" | cut -d'.' -f1-3) # Get the first three octets of the DNS IP
# last_octet=$((RANDOM % 254 + 1)) # Generate a random value for the last octet between 1 and 254
# nodeip="${ip_base}.${last_octet}" # Concatenate the base IP with the new last octet
# Disabling and stopping services not needed # Disabling and stopping services not needed
for service in firewall dnsmasq odhcpd; do for service in firewall dnsmasq odhcpd; do
if /etc/init.d/$service enabled; then if /etc/init.d/$service enabled; then
@ -373,29 +376,30 @@ set_apmode() {
uci add_list network.@device[0].ports='wan' uci add_list network.@device[0].ports='wan'
fi fi
# Find all radios we are using in our setup # Get the radio to be used for mesh from the config
find_radios AP_RADIO=$(uci -q get easymesh.config.apRadio)
#### COME BACK TO THIS #### # Determine which radios to process
# # Check if WIFI_RADIOS contains more than one radio if [ "$AP_RADIO" == "all" ]; then
# if [[ $WIFI_RADIOS =~ [[:space:]] ]]; then # If 'all' is specified, get all the radios
# # Loop through all the radios add nwimesh to the that network RADIO_LIST=$(uci -X show wireless | grep wifi-device | awk -F'[.=]' '{print $2}')
# for cur_radio in $WIFI_RADIOS; do else
# echo "Multiple Radio Setup, Current Radio: $radio" # If a specific radio is specified, use only that radio
# # Extact the radio number from the radio name RADIO_LIST=$AP_RADIO
# wifi_num="${cur_radio#radio}" fi
# uci set wireless.wifinet${wifi_num}.network="lan nwi_mesh_${cur_radio}"
# done # Loop through the selected radios
# else for CURRENT_RADIO in $RADIO_LIST; do
# echo "Calling Single Radio Setup: $WIFI_RADIOS" echo "Multiple Radio Setup, Current Radio: $CURRENT_RADIO"
# # Extact the radio number from the radio name # Extact the radio number from the radio name
# wifi_num="${WIFI_RADIOS#radio}" wifi_num="${cur_radio#CURRENT_RADIO}"
# uci set wireless.wifinet${wifi_num}.network="lan nwi_mesh_${WIFI_RADIOS}" uci set wireless.wifinet${wifi_num}.network="lan nwi_mesh_${cur_radio}"
# fi done
uci commit uci commit
# Tell openwrt to reload the configs # Tell openwrt to reload the configs
reload_config reload_config
/etc/init.d/network reload
else else
# Set a flag to indicate that we are in AP mode # Set a flag to indicate that we are in AP mode
uci set easymesh.config.ap_mode_enabled=0 uci set easymesh.config.ap_mode_enabled=0
@ -430,31 +434,18 @@ set_apmode() {
fi fi
} }
# ENABLED=$(uci -q get easymesh.config.enabled)
# if [ "$ENABLED" = 1 ]; then
# clear_old_networks
# create_bat0
# find_radios
# process_radios
# restart_and_reload
# else
# clear_old_networks
# disable_easymesh
# fi
# We accept the parameters: start, stop and apmode # We accept the parameters: start, stop and apmode
# start: Enables easymesh # start: Enables easymesh
# stop: Disables easymesh # stop: Disables easymesh
# apmode: Sets the device to AP mode # apmode: Sets the device to AP mode
if [ "$1" = "start" ]; then if [ "$1" = "start" ]; then
clear_old_networks clear_mesh_radio
create_bat0 create_batman_network
find_radios
process_radios process_radios
restart_and_reload restart_and_reload
elif [ "$1" = "stop" ]; then elif [ "$1" = "stop" ]; then
clear_old_networks clear_mesh_radio
disable_easymesh disable_batman_interfaces
# elif [ "$1" = "apmode" ]; then # elif [ "$1" = "apmode" ]; then
# set_apmode # set_apmode
else else

Loading…
Cancel
Save