Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Openwrt scripts

Openwrt scripts

A couple of scripts for publishing the state of an openwrt router to mqtt.

Relies on mosquitto_pub and mosquitto_sub being present

init

Init.d scripts including one to start a pair of file monitors and one to publish whether or not the router is onlien

scripts

Scripts supporting the init scripts, see each for more information

#!/bin/sh /etc/rc.common
# Example script
# Copyright (C) 2007 OpenWrt.org
START=50
PIDFILE=/var/pids/lively
PIDDIR=/var/pids
BINFILE=/usr/bin/lively
ARGS="/openwrt summer.ceit.uq.edu.au"
start() {
[ -d $PIDDIR ] || mkdir -p $PIDDIR
[ -f $PIDFILE ] && exit 1
$BINFILE $ARGS 2>&1 >/dev/null &
echo $! > $PIDFILE
}
stop() {
kill `cat $PIDFILE`
rm $PIDFILE
}
#!/bin/sh
# lively - looks alive on an mqtt server
# Publishes 'online' to topic/status when lively starts
# Publishes 'offline' when it quits
# Includes will messages for when lively quits unexpectedly
# usage: lively.sh <topic> <host>
exec 3>&1 2>/dev/null
topic=$1
host=$2
# Note: spawns a mosquitto_pub whose output is ignored. This is for producing will messages.
(
mosquitto_pub -t ${topic}/status -m online -h $host -r 2>&1 >/dev/null &&
mosquitto_sub -t "empty" -h $host --will-payload offline --will-topic ${topic}/status --will-retain 2>&1 >/dev/null
) </dev/tty >/dev/tty &
PID=$!
trap 'kill $PID' HUP INT TERM QUIT KILL
wait $PID && mosquitto_pub -t ${topic}/status -m offline -h $host -r 2>&1 /dev/null
exit
#!/bin/sh
# publisher - periodically publish files
# usage: publisher.sh <file> <topic> <host> <delay>
# file - the file to monitor
# topic - the topic to publish the file on
# host - the broker to publish to
# delay - wait time between publishes
file=$1
topic=$2
server=$3
delay=$4
while true
do
cat $file | mosquitto_pub -h $server -s -t $topic -r
sleep $delay
done
#!/bin/sh /etc/rc.common
# Example script
# Copyright (C) 2007 OpenWrt.org
START=50
PIDDIR=/var/pids
PIDFILE=/var/pids/startpubs
start() {
[ -d $PIDDIR ] || mkdir -p $PIDDIR
[ -f $PIDFILE ] && exit 1
/usr/bin/startpubs &
echo $! > $PIDFILE
}
stop() {
kill -2 `cat $PIDFILE`
rm $PIDFILE
}
#!/bin/sh
# startpubs - start a couple of publishing things
exec 3>&1 2>/dev/null
host=$1
(publisher /var/dhcp.leases /openwrt/dhcp $host 20 >&3) </dev/tty >/dev/tty &
PIDS="$PIDS $!"
(publisher /proc/net/arp /openwrt/arp $host 20 >&3) </dev/tty >/dev/tty &
PIDS="$PIDS $!"
tidy() {
for pid in $PIDS
do
kill $pid
done
}
trap 'tidy' HUP INT TERM QUIT
ret=0
for pid in $PIDS
do
wait $PID || ret=1
done
exit ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment