Posted On December 14, 2020

Linux: Shell Script to Reboot Remote Devices Daily

kimconnect 0 comments
blog.KimConnect.com >> Linux >> Linux: Shell Script to Reboot Remote Devices Daily
  1. Set Variables
    remoteDevice=900.468.20.1
    username=admin
    password="PASSWORDHERE"
  2. Install sshpass if it doesn’t exist
    if ! sshpass -v sshpass &> /dev/null
    then
    echo "Installing sshpass..."
    apt install sshpass -y
    exit
    fi
  3. 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
  4. Add a line to crontab
    Option (a) Systematically
    line="
    # Reboot $remoteDevice
    0 2 * * * /usr/local/sbin/rebootDevice $remoteDevice $username \"$password\" > /dev/null"
    (crontab -l 2>/dev/null && echo "$line") | crontab -
    Option (b) Manually
    # 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

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Useful Ubuntu Settings for VNC

# Disable screensaver Ubuntu (Gnome) gsettings set org.gnome.desktop.screensaver lock-enabled false # Purge screensaver and update…

Ideas for Open Source SAN using local storage

Use a server with local drives Set up Raid 0 Install "SheepDog on Centos/Ubuntu's KVM"…

CentOS 5.1 Server Setup Notes

CentOS 5.1 Server Setup: LAMP, Email, DNS, FTP, ISPConfig (a.k.a. The Perfect Server) This tutorial…