I wrote this script to automate the rebooting of access points in an enterprise deployment. I make it available here under the GPLv3+.
#!/usr/bin/bash
# Copyright 2022 Soren Stoutner <soren@smallbusinesstech.net>.
#
# This script is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this script. If not, see <http://www.gnu.org/licenses/>.
# This script processes a CSV (Comma-Separated Values) file named `reboot.csv` located
# in the same directory as the script. The CSV file should have three columns with no
# headers. The first column is the day of the week as defined below. The second
# column is the IP address of the target device. The third column is the password of
# the target device. It is assumed that the SSH username is root.
# Sun = Sunday
# Mon = Monday
# Tue = Tuesday
# Wed = Wednesday
# Thu = Thursday
# Fri = Friday
# Sat = Saturday
# An example CSV file will look like the following:
# Mon,192.168.1.12,Passw0rd
# Fri,192.168.1.45,N0Acc3ss
# You can have the script reboot all devices in the CSV regardless of the day by
# using the following syntax: `/path/to/script -a`.
# The script requires `sshpass`. On Debain-based systems, sshpass can be installed by
# running `apt install sshpass`.
# The script must be excutable. After copying the script to the desired location,
# you can set it to be executable by running `chmod 755 /path/to/script`.
# This script can be automated by adding an entry to /etc/crontab. For example,
# the following entry will run the script every day at 1:00 AM.
# 0 1 * * * root /path/to/script
# Note that the `read` function of this script assumes that the last line of the file
# ends in a newline before the EOF (End Of File) character. This is the default file
# format on all operating systems. If, in some way, you have managed to create a CSV
# file that does not have a newline character at the end of the last line then the
# final line will not be processed. In my testing I have not been able to create a
# CSV file that does not have a newline character before the EOF, so this shouldn't
# be an easy pitfall to fall into.
# Get the options.
while getopts :a options
do
case $options in
# Set the `do_all` varible if specified.
a) do_all=true;;
esac
done
# Get the current day.
current_day=$(date +%a)
# Use `,` as the field separator so that a CSV file can be used as the input.
IFS=','
# Process the CSV file named `reboot.csv`.
while read -r day ip_address password
do
# Reboot the access point if the specified day is the current day or if `do_all` has been set.
if [[ ($day == $current_day) || (-v do_all) ]]
then
# Inform the user.
printf "Attempting to reboot $ip_address.\n"
# SSH to the access point and reboot it.
sshpass -p $password ssh -o "StrictHostKeyChecking no" -l root $ip_address "reboot"
fi
done < reboot.csv