fix network switch script

This commit is contained in:
Torben Haack 2024-10-10 07:17:01 +02:00
parent 5bb55a0a7d
commit 81c1b98894

View File

@ -1,52 +1,37 @@
#!/bin/bash #!/bin/bash
# Define the state file # Function to show the current state
STATE_FILE="/tmp/network_toggle_state"
# Function to display current state
show_current_state() { show_current_state() {
if [ "$1" == "down" ]; then if ip link show eth0 | grep -q "UP"; then
echo "Current state: Ethernet and MACVLAN active, Wi-Fi inactive" echo "Current state: Ethernet and MACVLAN active, Wi-Fi inactive"
else else
echo "Current state: Wi-Fi active, Ethernet and MACVLAN inactive" echo "Current state: Wi-Fi active, Ethernet and MACVLAN inactive"
fi fi
} }
# Check if the state file exists # Show the current state
if [ -f "$STATE_FILE" ]; then show_current_state
# If the state file exists, read the state
STATE=$(cat "$STATE_FILE")
else
# If the state file does not exist, initialize it to "down"
STATE="down"
echo "$STATE" > "$STATE_FILE"
fi
# Show current state # Check the current state by examining the status of Ethernet (eth0)
show_current_state "$STATE" if ip link show eth0 | grep -q "UP"; then
# Toggle the state
if [ "$STATE" == "down" ]; then
echo "Switching to Wi-Fi mode..." echo "Switching to Wi-Fi mode..."
echo "Deactivating: Ethernet (eth0) and MACVLAN interfaces" echo "Deactivating: Ethernet (eth0) and MACVLAN interfaces"
sudo ip link set macvlan0 down ip link set macvlan0 down
sudo ip link set macvlan1 down ip link set macvlan1 down
sudo ip link set eth0 down ip link set eth0 down
echo "Activating: Wi-Fi (wlan0)" echo "Activating: Wi-Fi (wlan0)"
sudo ip link set wlan0 up ip link set wlan0 up
echo "up" > "$STATE_FILE" # Update the state to "up"
else else
echo "Switching to Ethernet and MACVLAN mode..." echo "Switching to Ethernet and MACVLAN mode..."
echo "Deactivating: Wi-Fi (wlan0)" echo "Deactivating: Wi-Fi (wlan0)"
sudo ip link set wlan0 down ip link set wlan0 down
echo "Activating: Ethernet (eth0) and MACVLAN interfaces" echo "Activating: Ethernet (eth0) and MACVLAN interfaces"
sudo ip link set eth0 up ip link set eth0 up
sudo ip link set macvlan1 up ip link set macvlan1 up
sudo ip link set macvlan0 up ip link set macvlan0 up
echo "down" > "$STATE_FILE" # Update the state to "down"
fi fi
# Show new state # Show the new state
echo "" echo ""
echo "Switch completed." echo "Switch completed."
show_current_state $(cat "$STATE_FILE") show_current_state