Skip to content

Instantly share code, notes, and snippets.

@ahbanavi
Last active July 3, 2024 18:16
Show Gist options
  • Save ahbanavi/dd061cfdebc61edf758d1c00a1adfd69 to your computer and use it in GitHub Desktop.
Save ahbanavi/dd061cfdebc61edf758d1c00a1adfd69 to your computer and use it in GitHub Desktop.
swich / watch wan interfaces on router to swich between them based on cron or usage
#!/bin/bash
interface=$1
# Check if the interface is valid and set the other interface
if [[ $interface == "wan" ]]; then
other_interface="wanb"
elif [[ $interface == "wanb" ]]; then
other_interface="wan"
else
echo "Invalid interface: $interface"
echo "Please choose either 'wan' or 'wanb'"
exit 1
fi
# SSH into the OpenWrt router and switch the interface
echo "Switching to $interface..."
ssh [email protected] "ifup $interface && sleep 30 && ifdown $other_interface" &
#!/bin/bash
# you must install bash + bc + jq, opkg install bash bc
# and also install and setup vnstat
# https://openwrt.org/docs/guide-user/services/network_monitoring/vnstat
# usage in cron every 10 min:
# */10 * * * * /root/scripts/watch_interface.sh wan 2 wanb
# 2 means if ussage is greater than 3GiB
# remember to also swich back interface in cron, like 4 am:
# 0 4 * * * /bin/sh -c 'ifup wan && sleep 30 && ifdown wanb'
# Function to check network usage
check_usage() {
local interface=$1
local usage_limit=$2
local other_interface=$3
# Check if the interface is down
if ! ifconfig $interface | grep -q 'UP'; then
echo "Interface $interface is down. Exiting."
exit 1
fi
# Get the current network usage for received and transmitted data
local usage_data=$(vnstat -i $interface --json d | jq '.interfaces[0].traffic.days[0] | {rx: .rx, tx: .tx}')
local usage_rx=$(echo $usage_data | jq '.rx')
local usage_tx=$(echo $usage_data | jq '.tx')
# Add up the received and transmitted data
local total_usage=$(echo "$usage_rx + $usage_tx" | bc)
# Convert usage to GB
local usage_gb=$(echo "$total_usage / 1000 / 1000" | bc)
echo "Current usage: $usage_gb GB"
# Check if usage exceeds limit
if (( usage_gb > usage_limit )); then
# Switch interface
ifup $other_interface && sleep 30 && ifdown $interface
echo "Usage is above limit - switching interfaces to $other_interface"
logger -p notice -t switchnet "switch network to $other_interface with usage of $total_usage"
else
echo "Usage is below limit"
fi
}
if [ $# -ne 3 ]; then
echo "Usage: $0 <interface> <usage_limit> <other_interface>"
exit 1
fi
check_usage $1 $2 $3
#!/bin/bash
# Function to check network usage
check_usage() {
local interface=$1
local usage_limit=$2
local other_interface=$3
# Check if the interface is down
if ! ssh [email protected] "ifconfig $interface | grep -q 'UP'"; then
echo "Interface $interface is down. Exiting."
exit 1
fi
# Get the current network usage
local usage_data=$(ssh [email protected] "vnstat -i $interface --json d | jq '.interfaces[0].traffic.days[0] | {rx: .rx, tx: .tx}'")
local usage_rx=$(echo $usage_data | jq '.rx')
local usage_tx=$(echo $usage_data | jq '.tx')
# Add up the received and transmitted data
local total_usage=$(echo "$usage_rx + $usage_tx" | bc)
# Convert usage to GB
local usage_gb=$(echo "$total_usage / 1000 / 1000" | bc)
echo "Current usage: $usage_gb GB"
# Check if usage exceeds limit
if (( usage_gb > usage_limit )); then
# Switch interface
ssh [email protected] "ifup $other_interface && sleep 30 && ifdown $interface"
echo "Usage is above limit - switching interfaces to $other_interface"
else
echo "Usage is below limit"
fi
}
# Call the function with the desired interface, usage limit, and other interface
# check if arguments are passed
# if not, print usage
if [ $# -ne 3 ]; then
echo "Usage: $0 <interface> <usage_limit> <other_interface>"
exit 1
fi
check_usage $1 $2 $3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment