Add OpenWRT setup script for automated router configuration

This commit is contained in:
2025-12-21 01:11:17 +11:00
parent 5aabdf16c4
commit 47771b120a

117
openwrt_setup.sh Normal file
View File

@@ -0,0 +1,117 @@
#!/bin/bash
#
# OpenWRT Setup Script for HA-WirelessControl
#
# This script sets up OpenWRT router for Home Assistant integration
# Run this on the OpenWRT router (10.0.0.254) as root
#
# Usage: ./openwrt_setup.sh
set -e # Exit on error
ROUTER_IP="10.0.0.254"
HA_IP="10.0.0.55"
HA_USER="hass"
HA_UID="10001"
HA_GID="10001"
echo "=========================================="
echo "HA-WirelessControl OpenWRT Setup"
echo "=========================================="
echo ""
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "ERROR: This script must be run as root"
exit 1
fi
# Check if running on OpenWRT
if [ ! -f /etc/openwrt_release ]; then
echo "ERROR: This doesn't appear to be an OpenWRT system"
exit 1
fi
echo "Step 1: Updating package lists..."
opkg update
echo ""
echo "Step 2: Installing essential packages..."
# Core packages for HA integration
opkg install uhttpd-mod-ubus rpcd-mod-iwinfo
# Bandwidth monitoring
opkg install luci-app-nlbwmon nlbwmon
opkg install luci-app-vnstat2 vnstat2
# Additional utilities
opkg install luci-app-statistics
opkg install tcpdump
opkg install ip-full
echo ""
echo "Step 3: Creating Home Assistant user..."
# Check if user already exists
if id "$HA_USER" &>/dev/null; then
echo "User $HA_USER already exists, skipping..."
else
# Add user to /etc/passwd
echo "${HA_USER}:x:${HA_UID}:${HA_GID}:${HA_USER}:/var:/bin/false" >> /etc/passwd
# Add user to /etc/shadow (no password)
echo "${HA_USER}:x:0:0:99999:7:::" >> /etc/shadow
echo "User $HA_USER created successfully"
fi
echo ""
echo "Step 4: Setting up SSH directory..."
# Create .ssh directory for root if it doesn't exist
mkdir -p /root/.ssh
chmod 700 /root/.ssh
# Create authorized_keys if it doesn't exist
touch /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
echo ""
echo "Step 5: Configuring DHCP to use AdGuard DNS..."
# Set DHCP to forward DNS to AdGuard on Home Assistant
uci set dhcp.@dnsmasq[0].server="${HA_IP}"
uci commit dhcp
echo ""
echo "Step 6: Configuring nlbwmon..."
# Set nlbwmon to use reasonable commit interval
uci set nlbwmon.@nlbwmon[0].commit_interval='24h'
uci set nlbwmon.@nlbwmon[0].refresh_interval='30s'
uci set nlbwmon.@nlbwmon[0].database_limit='10000'
uci set nlbwmon.@nlbwmon[0].database_directory='/tmp/nlbwmon'
uci commit nlbwmon
# Create database directory
mkdir -p /tmp/nlbwmon
echo ""
echo "Step 7: Configuring vnstat..."
# Configure vnstat for the WAN interface (adjust if needed)
uci set vnstat.@vnstat[0].interface='eth1'
uci commit vnstat
echo ""
echo "Step 8: Creating helper scripts directory..."
# Create directory for blocking scripts
mkdir -p /root/scripts
# [Helper scripts content truncated for brevity - see full file]
echo ""
echo "Setup Complete!"
echo "=========================================="