From 7ab35bbdef8881e1adca779dbc3d8f59e2dec94e Mon Sep 17 00:00:00 2001 From: Adolfo Delorenzo Date: Mon, 29 Jul 2024 13:27:56 -0600 Subject: [PATCH] 2024-07-29 - --- wifi_switch.sh | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100755 wifi_switch.sh diff --git a/wifi_switch.sh b/wifi_switch.sh new file mode 100755 index 0000000..d4c9ff9 --- /dev/null +++ b/wifi_switch.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +# Configuration +WIFI_INTERFACE="wlan0" +MODEM_INTERFACE="wwan0" +CHECK_INTERVAL=10 # Check every 60 seconds + +# Function to check Wi-Fi connection +check_wifi() { + if iwconfig "$WIFI_INTERFACE" | grep -q "ESSID:off/any"; then + return 1 # Wi-Fi is not connected + else + return 0 # Wi-Fi is connected + fi +} + +# Function to enable 4G modem +enable_4g() { + echo "Wi-Fi disconnected. Enabling 4G modem..." + sudo ifconfig "$MODEM_INTERFACE" up + sudo dhclient "$MODEM_INTERFACE" +} + +# Function to disable 4G modem +disable_4g() { + echo "Wi-Fi connected. Disabling 4G modem..." + sudo ifconfig "$MODEM_INTERFACE" down +} + +# Main loop +while true; do + if check_wifi; then + echo "Wi-Fi is connected." + if ifconfig "$MODEM_INTERFACE" | grep -q "UP"; then + disable_4g + fi + else + echo "Wi-Fi is disconnected." + if ! ifconfig "$MODEM_INTERFACE" | grep -q "UP"; then + enable_4g + fi + fi + sleep "$CHECK_INTERVAL" +done