Languages/Bash/RunUpdates
From UIT
|
Introduction
This script is used in our programming labs for doing unattended changes on all machines. It will be run once every reboot, fetch the necessary script from a ssh server and execute it.
Prerequisites
- A ssh accessible server
Setup
- Log into the machine to be updated
- Copy this script into root/run-updates.sh, the REMOTE_PATH should be updated to match your environment.
#!/bin/bash # # Update script # # * This script will fetch update scripts from REMOTE_PATH # * Update scripts should be named 1.sh, 2.sh, ... # * REMOTE_PATH should be accessible through ssh, without password (public key login) # * The file LAST in DEST will hold the last script downloaded regardless of the success of executi$ REMOTE_PATH=user@ssh-server.example.com: DEST=/usr/share/heivs/heivsupdate mkdir -p $DEST cd $DEST # The file named LAST will hold the last update number # Create it if it does not exist # if [ ! -e LAST ] then CURRENT=0 echo -n $CURRENT > LAST fi CURRENT=`cat LAST` while [ 1 ] do ((CURRENT++)) scp -q $REMOTE_PATH$CURRENT.sh . if [ ! -e $CURRENT.sh ] then echo nothing new exit 0 fi echo -n $CURRENT > LAST ./$CURRENT.sh done
- create a RSA key for remote login into /root/.ssh/id_rsa (with an empty passphrase)
ssh-keygen -t rsa
- copy the id_rsa.pub key into the ssh server
- create a RSA key for remote login into /root/.ssh/id_rsa
ssh user@ssh-server.example.com mkdir -p .ssh cat /root/.ssh/id_rsa.pub | ssh user@ssh-server.example.com 'cat >> .ssh/authorized_keys'
- edit the root crontab ("crontab -e")
@reboot /root/run-updates.sh >/dev/null 2>&1
Update scripts
Update scripts should be placed into the user@ssh-server.example.com directory. The file name should be 1.sh, 2.sh, ...