1##!/bin/bash
2
3#
4# Automatic OpenWRT manteinance:
5# - Upgrades all packages
6# - Upgrades OpenWRT version to latest stable
7#
8
9# Find target and id from
10# https://downloads.openwrt.org/releases/23.05.5/.overview.json
11target=ath79/generic # CHANGEME
12id=tplink_archer-c7-v5 # CHANGEME
13router=[email protected] # CHANGEME
14
15# Get upstream version
16stable_version=$(curl 'https://downloads.openwrt.org/.versions.json' 2>/dev/null | jq '.stable_version' | cut -d '"' -f 2)
17
18# Get router version
19router_version=$(ssh ${router} cat /etc/os-release | grep "VERSION=" | cut -d "\"" -f 2)
20
21# CHeck if router version is latest stable
22if [ "${router_version}" == "${stable_version}" ]; then
23 echo "[+] Already running latest OpenWRT (${stable_version})"
24 echo "[+] Updating OpenWRT packages"
25 ssh ${router} "opkg update && opkg list-upgradable | cut -f 1 -d ' ' | xargs opkg upgrade"
26 exit 0
27else
28 echo "[+] Updating OpenWRT ${router_version} -> ${stable_version}"
29fi
30
31# If not, proceed to upgrade the router
32
33# Download the sysupgrade file
34
35file=$(ls openwrt-${stable_version}* | cut -d ' ' -f 1)
36
37if [ ! -f ${file} ]; then
38 wget -q https://downloads.openwrt.org/releases/${stable_version}/targets/${target}/openwrt-${stable_version}-${target//\//-}-${id}-squashfs-sysupgrade.bin
39 file=$(ls openwrt-${stable_version}* | cut -d ' ' -f 1)
40 echo "[+] Downloaded ${file}"
41fi
42
43# Check download integrity against OpenWRT listed one
44file=$(ls openwrt-${stable_version}* | cut -d ' ' -f 1)
45sha256=$(sha256sum ${file} | cut -d ' ' -f 1)
46check=$(curl 2>/dev/null "https://downloads.openwrt.org/releases/${stable_version}/targets/${target}/profiles.json" | grep -c ${sha256})
47if [ "${check}" -eq "0" ]; then
48 echo "[+] Corrupted download"
49 exit 1
50else
51
52 echo "[+] Integrity check passed (download)"
53fi
54
55# Copy sysupgrade file to the router
56echo "[+] Sending upgrade file to the router"
57scp -O ${file} ${router}:/tmp/${file}
58
59# Check if file got corrupted during send
60router_sha256=$(ssh ${router} "sha256sum /tmp/${file} | cut -d ' ' -f 1")
61
62echo ${router_sha256}
63
64if [ "${sha256}" != "${router_sha256}" ]; then
65 echo "[+] Corrupted file in the router"
66 exit 1
67else
68 echo "[+] Integrity check passed (router)"
69fi
70
71# Update all router packages before a sysupgrade
72echo "[+] Updating OpenWRT packages"
73ssh ${router} "opkg update && opkg list-upgradable | cut -f 1 -d ' ' | xargs opkg upgrade"
74
75# Perform sysupgrade on router
76echo "[+] Upgrading OpenWRT"
77ssh ${router} "sysupgrade /tmp/${file}"