[luci] OpenWrt new release/update available notification

as there are alot of posts here on this topic... i'm sharing this "quick-and-dirty" proof of concept... luci notifier...

you need curl and bootstrap and it's likely imperfect. but provides the kind of notice i've suggested... so may as well put my mouth where my lack of money is.

apologies to the makers of luci

/bin/updatecheck.sh
#!/bin/sh

###############################################

# install curl save this as /bin/updatecheck.sh
# run once ( to rewrite luci bootstrap header )
# i.e.;
# chmod +x /bin/updatecheck.sh; /bin/updatecheck.sh 
# beware writes to /etc... 
#( avoid using "hourly" on flash )

###############################################

OUTPUTOK=1			#tba -> show "uptodate" all the time if no updates
SNAPSHOTCHK=1 			#snapshot comment out to disable checks totally (just exit)
#RELEASESMAJORONLY=1 		#non-snapshot... report only same release updates
CHECKPERIOD="monthly"
CHECKPERIOD="weekly"
CHECKPERIOD="hourly"
CHECKPERIOD="daily"
###############################
D=$(date +%Y%m%d-%H%M)
releaseurl="https://downloads.openwrt.org/releases/" #curl $releaseurl ONLY for non-snapshots
updF="/etc/updatecheck"; mkdir -p $updF

RELEASETXT="$updF/releasetxt-`date +%Y%m%d`"
CHECKLOG="$updF/version.checklog"

eval $(grep '^VERSION=' /etc/os-release)
eval $(grep '^BUILD_ID=' /etc/os-release)
eval $(grep '^OPENWRT_BOARD=' /etc/os-release)

case $CHECKPERIOD in
	daily) INTERVALc="$(date +%Y%m%d)"; ;;
	monthly) INTERVALc="$(date +%Y%m)"; ;;
	weekly) INTERVALc="$(date +%Y%W)"; ;;
	hourly) INTERVALc="$(date +%Y%m%d%H)"; ;;
esac
RESULTtxt="$updF/versionchk-$INTERVALc"
UPTODATEtxt="$updF/version.uptodate-$INTERVALc"

if [ "$1" = "now" ]; then
	rm $RESULTtxt 2>/dev/null
	rm $RELEASETXT 2>/dev/null
	rm $UPTODATEtxt 2>/dev/null
fi


curlreleaselist() {
if [ ! -f "$RELEASETXT" ]; then
    curl -s "$releaseurl" | grep href | grep -v '(root)' | grep -v KeyCDN | grep -v faillogs | grep -v 'packages-' | sed s/'<\/a>.*'/''/ | sed s/'<tr><td class="n">'/''/ | cut -d'>' -f2 | grep -v '\-rc' > "$RELEASETXT"
else
	: #echo "$RELEASETXT found"
fi
if [ "$(cat $RELEASETXT | wc -l)" -eq 0 ]; then echo "no items in $RELEASETXT"; exit 0; fi
}

checkfornewversion() {
checkforinternet
case $VERSION in
	SNAPSHOT)
	if [ ! -z "$SNAPSHOTCHK" ]; then
		onlineVurl="https://downloads.openwrt.org/snapshots/targets/$OPENWRT_BOARD/version.buildinfo"
		onlineVERSION=$(curl $onlineVurl 2>/dev/null)
		if [ "$BUILD_ID" != "$onlineVERSION" ]; then
			rm $UPTODATEtxt 2>/dev/null
			echo "newversion $BUILD_ID > $onlineVERSION"
			logger -t $0-$1 "newversion $BUILD_ID > $onlineVERSION"
			echo "newversion $BUILD_ID > $onlineVERSION $D" >> $CHECKLOG
			echo "newversion $BUILD_ID > $onlineVERSION" >> $RESULTtxt
		else
			echo "uptodate: $BUILD_ID > $onlineVERSION $D"
			echo "uptodate: $BUILD_ID > $onlineVERSION" >> $CHECKLOG
		fi
	else
		logger -t $0-$1 "SNAPSHOT skipping version check"
	fi
	;;
	*)
	curlreleaselist		
	if [ ! -z "$RELEASESMAJORONLY" ]; then
	versionmajor=$(echo "$VERSION" | grep -woE '[0-9][0-9]*.[0-9][0-9]')
	newversions=$(cat $RELEASETXT | grep "^$versionmajor" | grep -A10 "$VERSION" | grep -v "$VERSION" | wc -l)
		if [ "$newversions" -gt 0 ]; then
			rm $UPTODATEtxt 2>/dev/null
			verlist=$(cat $RELEASETXT | grep "^$versionmajor" | grep -A10 "$VERSION" | grep -v "$VERSION" | sort -nr | tr '\n' ' ')
			echo "$VERSION[updates-available] $verlist"
			echo "$VERSION[updates-available] $verlist" >> $RESULTtxt
			echo -n "$VERSION[updates-available] $verlist" >> $CHECKLOG
		else
			# >> /etc/versionchecklog
			echo "$VERSION [uptodate]"
			echo "$VERSION [uptodate]" >> $RESULTtxt
			touch $UPTODATEtxt
		fi
	else
		newversions=$(cat $RELEASETXT | grep -A15 "$VERSION" | grep -v "$VERSION" | wc -l)
		if [ "$newversions" -gt 0 ]; then
			rm $UPTODATEtxt 2>/dev/null
			verlist=$(cat $RELEASETXT | grep -A15 "$VERSION" | grep -v "$VERSION" | sort -nr | tr '\n' ' ')
			echo "$VERSION[updates-available] $verlist"
			echo "$VERSION[updates-available] $verlist" > $RESULTtxt
			echo "$VERSION[updates-available] $verlist" >> $CHECKLOG
		else
			echo "$VERSION [uptodate]"
			echo "$VERSION [uptodate]" >> $RESULTtxt
			touch $UPTODATEtxt
		fi
	fi	
	;;
esac
}

showupdmessage() {
cat <<EOF
<div id="maincontent" class="container">
	<div class="alert-message notice">
	<h5>Updates are available
	$NEWVERSION
	<a href="$updateURL" target="_blank">$updateURL</a>
	</h>
EOF
#echo "$updMESSAGE"
cat <<EOF
</div>
EOF
}

checkforinternet() {
if ! ping -c 2 -w 3 "8.8.8.8" &>/dev/null; then echo $0-$1 no-internet; exit 0; fi
}


checkluciheader() {

if ! grep -q '/bin/updatecheck.sh' /usr/lib/lua/luci/view/themes/bootstrap/header.htm; then
cat <<'EOF' > /usr/lib/lua/luci/view/themes/bootstrap/header.htm
<%#
 Copyright 2008 Steven Barth <steven@midlink.org>
 Copyright 2008-2016 Jo-Philipp Wich <jow@openwrt.org>
 Copyright 2012 David Menting <david@nut-bolt.nl>
 Licensed to the public under the Apache License 2.0.
-%>

<%
	local sys  = require "luci.sys"
	local util = require "luci.util"
	local http = require "luci.http"
	local disp = require "luci.dispatcher"

	local boardinfo = util.ubus("system", "board")

	local node = disp.context.dispatched

	-- send as HTML5
	http.prepare_content("text/html")
-%>
<!DOCTYPE html>
<html lang="<%=luci.i18n.context.lang%>">
	<head>
		<meta charset="utf-8">

		<title><%=striptags( (boardinfo.hostname or "?") .. ( (node and node.title) and ' - ' .. translate(node.title) or '')) %> - LuCI UPDATES</title>
		<meta name="viewport" content="initial-scale=1.0">
		<link rel="stylesheet" href="<%=media%>/cascade.css?v=git-20.168.54559-27fc7a8">
		<link rel="stylesheet" media="only screen and (max-device-width: 854px)" href="<%=media%>/mobile.css?v=git-20.168.54559-27fc7a8" type="text/css" />
		<link rel="shortcut icon" href="<%=media%>/favicon.png">
		<% if node and node.css then %>
			<link rel="stylesheet" href="<%=resource%>/<%=node.css%>">
		<% end -%>
		<% if css then %>
			<style title="text/css"><%= css %></style>
		<% end -%>
		<script src="<%=url('admin/translations', luci.i18n.context.lang)%>?v=git-20.168.54559-27fc7a8"></script>
		<script src="<%=resource%>/cbi.js?v=git-20.168.54559-27fc7a8"></script>
	</head>

	<body class="lang_<%=luci.i18n.context.lang%> <% if node then %><%= striptags( node.title ) %><%- end %>" data-page="<%= pcdata(table.concat(disp.context.requestpath, "-")) %>">
		<header>
			<div class="fill">
				<div class="container">
					<a class="brand" href="/"><%=boardinfo.hostname or "?"%></a>
					<ul class="nav" id="topmenu" style="display:none"></ul>
					<div id="indicators" class="pull-right"></div>
				</div>
			</div>
		</header>

		<%-
local upd_msg
upd_msg = luci.sys.exec("/bin/updatecheck.sh luci 2>/dev/null")
if upd_msg then
	io.write(upd_msg)
end
		
		-%>

			<%- if luci.sys.process.info("uid") == 0 and luci.sys.user.getuser("root") and not luci.sys.user.getpasswd("root") then -%>
				<div class="alert-message warning">
					<h4><%:No password set!%></h4>
					<p><%:There is no password set on this router. Please configure a root password to protect the web interface and enable SSH.%></p>
					<% if disp.lookup("admin/system/admin") then %>
					  <div class="right"><a class="btn" href="<%=url("admin/system/admin")%>"><%:Go to password configuration...%></a></div>
					<% end %>
				</div>
			<%- end -%>

			<noscript>
				<div class="alert-message warning">
					<h4><%:JavaScript required!%></h4>
					<p><%:You must enable JavaScript in your browser or LuCI will not work properly.%></p>
				</div>
			</noscript>

			<div id="tabmenu" style="display:none"></div>
EOF
fi
}


if ! command -v curl >/dev/null; then logger -t $0-$1 "install curl"; exit 0; fi
checkluciheader
if [ ! -f /etc/profile.d/99-updatecheck.sh ]; then
	cp $0 /etc/profile.d/99-updatecheck.sh
fi

if [ -f "$UPTODATEtxt" ]; then
	exit 0
else
	: #logger -t $0-$1 "no $UPTODATEtxt"
fi

if [ -f "$RESULTtxt" ]; then
	updMESSAGE=$(cat $RESULTtxt | tail -n 1)
else
	updMESSAGE=$(checkfornewversion)
	if [ -f "$UPTODATEtxt" ]; then exit 0; fi
fi

case $VERSION in
	SNAPSHOT)
		NEWVERSION=$(cat $RESULTtxt | tail -n 1 | cut -d' ' -f2)
		updateURL="https://downloads.openwrt.org/snapshots/targets/$OPENWRT_BOARD/"
		logger -t $0-$1 "updateurl: $updateURL"
	;;
	*)
		NEWVERSION=$(cat $RESULTtxt | tail -n 1 | cut -d' ' -f2)
		updateURL="https://downloads.openwrt.org/releases/$NEWVERSION/targets/$OPENWRT_BOARD/"
		logger -t $0-$1 "updateurl: $updateURL"
	;;
esac

case "$1" in
	luci)
		showupdmessage
	;;
	*)
		echo $updMESSAGE $updateURL
	;;
esac
1 Like

Excellent for my once need.
Thanks anyway!

1 Like