#!/bin/sh # Configure a "spare router" to a known-good state. # This script configures the factory default settings of OpenWrt # to make it easy to swap it in when a new router is needed. # It also creates a label showing the configuration and credentials. # You can print the label and tape it to the router so # the next person will know how to access the router. # The label format is: # # ======= Printed with: print-router-label.sh ======= # Device: Linksys E8450 (UBI) # OpenWrt: OpenWrt 23.05.5 r24106-10cc5fcd00 # Connect to: http://Belkin-RT3200.local # or: ssh root@Belkin-RT3200.local # LAN: 192.168.253.1 # User: root # Login PW: root-password # Wifi SSID: My Wifi SSID # Wifi PW: abcd9876 # Configured: 2024-11-28 # === See github.com/richb-hanover/OpenWrtScripts === # # Label for Power Brick: Linksys E8450 (UBI) # ***** To run this script ***** # # 0. (Optional) Make a backup of the current router config. # It'll be easy to restore if necessary. # 1. Connect your laptop on a wired LAN port (Ethernet): # some of these changes can reset the wireless network. # 2. Connect the router's WAN port to the internet: this # script needs to install certain packages. (Perhaps # plug its WAN port into your new router's LAN port # while running this script.) # 3. Flash the router with factory firmware. # Do NOT keep the settings. # 4. SSH in and execute the statements below. # # ssh root@192.168.1.1 # the default OpenWrt LAN address # cd /tmp # cat > config.sh # [paste in the entire contents of this file, then hit ^D] # sh config.sh # Presto! (The router reboots when the script completes.) # # The script sets generic settings and credentials. # You could make a copy of this script, customize it to your needs, # then use the "To run this script" procedure (below). # # === print_router_label() === # This function is copy/pasted from "print-router-label.sh" # to keep the "config-spare-router.sh" script a single file. # THIS IS A MAINTENANCE HASSLE: # Changes to the printing must be updated in both places print_router_label() { local ROOTPASSWD="${1:-"?"}" TODAY=$(date +"%Y-%m-%d") DEVICE=$(cat /tmp/sysinfo/model) OPENWRTVERSION=$(grep "DISTRIB_DESCRIPTION" /etc/openwrt_release | cut -d"=" -f2 | tr -d '"' | tr -d "'") HOSTNAME=$(uci get system.@system[0].hostname) LANIPADDRESS=$(uci get network.lan.ipaddr) LOCALDNSTLD=$(uci get dhcp.@dnsmasq[0].domain) # top level domain for local names # Create temporary file for both SSID and password TMPFILE=$(mktemp /tmp/wifi_creds.XXXXXX) # Get wifi credentials uci show wireless |\ egrep =wifi-iface$ |\ cut -d= -f1 |\ while read s; do uci -q get $s.disabled |\ grep -q 1 && continue; id=$(uci -q get $s.ssid); key=$(uci -q get $s.key); # Write both SSID and password to temporary file echo "$id:$key" > "$TMPFILE" break done # Read both values from temporary file if [ -f "$TMPFILE" ]; then WIFISSID=$(cut -d: -f1 "$TMPFILE") WIFIPASSWD=$(cut -d: -f2 "$TMPFILE") # Check if password is empty and replace with "" if [ -z "$WIFIPASSWD" ]; then WIFIPASSWD="" fi else WIFISSID="unknown" WIFIPASSWD="unknown" fi # Clean up temporary file rm -f "$TMPFILE" echo "" echo "Print the following label and tape it to the router..." echo "" echo "======= Printed with: print-router-label.sh =======" echo " Device: $DEVICE" echo " OpenWrt: $OPENWRTVERSION" echo " Connect to: http://$HOSTNAME.$LOCALDNSTLD" echo " or: ssh root@$HOSTNAME.$LOCALDNSTLD" echo " LAN: $LANIPADDRESS" echo " User: root" echo " Login PW: $ROOTPASSWD" echo " Wifi SSID: $WIFISSID" echo " Wifi PW: $WIFIPASSWD" echo " Configured: $TODAY" echo "=== See github.com/richb-hanover/OpenWrtScripts ===" echo "" echo "Label for Power Brick: $DEVICE" echo "" } # === CONFIGURATION PARAMETERS === # Set the variables to be used for configuration HOSTNAME="SpareRouter" ROOTPASSWD="SpareRouter" TIMEZONE='EST5EDT,M3.2.0,M11.1.0' # see link below for other time zones ZONENAME='America/New York' LANIPADDRESS="172.30.42.1" # 172.30.42.1 minimizes chance of conflict LANSUBNET="255.255.255.0" SNMP_COMMUNITYSTRING=public WIFISSID="SpareRouter" WIFIPASSWD='' ENCRMODE='none' # === Update root password ===================== # Update the root password. # echo '*** Updating root password' passwd <