Skip to content

Instantly share code, notes, and snippets.

@juliocbc
Created March 31, 2021 19:28
Show Gist options
  • Save juliocbc/e922eea01649612bb60ec416b09445e2 to your computer and use it in GitHub Desktop.
Save juliocbc/e922eea01649612bb60ec416b09445e2 to your computer and use it in GitHub Desktop.
OPNsense - Factory Defaults example script (using sound instead reset button)
#!/bin/sh
# Copyright (c) 2021 Cloudfence - Julio Camargo
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
CTRL_FILE="/usr/local/var/cfagent-factory-defaults.ctl"
CHK_CTRL=$(cat $CTRL_FILE)
# first cycle
if [ "$CHK_CTRL" -eq 0 ];then
echo 1 > $CTRL_FILE
echo "A..." > /dev/speaker
# poweroff after the warning sound will change state to 2
sleep 3
# else normal boot continues
echo 0 > $CTRL_FILE
exit 0
# second cycle
elif [ "$CHK_CTRL" -eq 1 ];then
echo 2 > $CTRL_FILE
# warns user that is the 2nd cycle
echo "A.A.A..." > /dev/speaker
sleep 3
# poweroff here will proceed to final step
echo 0 > $CTRL_FILE
elif [ "$CHK_CTRL" -eq 2 ];then
# final warning
echo "A...A...A..." > /dev/speaker
echo 0 > $CTRL_FILE
# to cancel the factory defaults user must poweroff after the final warning
sleep 3
# else factory defaults will be be called
echo "reseting..."
echo y | exec /usr/local/opnsense/scripts/shell/defaults.php
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment