- Set Variables
remoteDevice=10.25.0.20
username=admin
password="PASSWORDHERE" - Install sshpass if it doesn’t exist
if ! sshpass -v sshpass &> /dev/null
then
echo "Installing sshpass..."
apt install sshpass -y
exit
fi - Create a script to reboot devices
scriptFile=/usr/local/sbin/rebootDevice
sudo cat << EOF > $scriptFile
#!/bin/bash
device="\$1"
user="\$2"
pass="\$3"
command=\$(which sshpass)
\$command -p \$pass /usr/bin/ssh \$user@\$device reboot
EOF - Add a line to crontab
Option (a) Systematicallyline="
Option (b) Manually
# Reboot $remoteDevice
0 2 * * * /usr/local/sbin/rebootDevice $remoteDevice $username \"$password\" > /dev/null"
(crontab -l 2>/dev/null && echo "$line") | crontab -# Skip this alternative method of creating a new entry in cron manually
sudo crontab -e
#### Add this line ###
# Call the reboot script at 2am everyday
0 2 * * * /usr/local/sbin/rebootDevice x.x.x.x [USERNAMEHERE] "[PASSWORDHERE]" > /dev/null
Categories: