Today I had a heck of a time finding a good script that will automatically run radmind. I looked all over and found lots of scripts that had tons of bells and whistles, but nothing that was nice clean and simple. I had to go back and find the one that I had been using at EMU back when I implemented radmind in the labs there.
Read more for the script.The Script
#!/bin/bash
###
# radmind-image.sh:
# A script to run the radmind client tools to update a machine.
# Handles several types of output. Inspired by the logout.hook
# Bourne shell script supplied with the iHook tools.
# See http://rsug.itd.umich.edu/software/ for details about both
# iHook and radmind.
# author:
# nathan hruby
# date:
# April 22, 2003
# version:
# 0.7
# requires:
# radmind, {ba}sh
# optional:
# ConsoleMessage, iHook
# TODO:
# – put all programs in varaibles for easier tuning (eg: -w flag support)
# – flexible output messages
# – Do we need a config file?
# – better documentation
# Quick HOWTO:
# Edit this file for your environemnt, if using SSL in radmind,
# you'll need to edit the functions directly to add the -w flag for
# your environment. Once done, you can use this as a logout hook,
# iHook hook, or startupItem by wrapping it in another simple shell
# script with the needed flags. Run this script with the -h flag
# for a rundown of what flag does what.
###
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/libexec
ME=`basename $0`
HOST=`hostname`
# Enter the address of your radmind server
SERVER=radmind.example.edu
# Enter the default hash you use with radmind
HASH=sha1
# These are the different fsdiff commands, you shouldn't need to fiddle
FSDIFF=”fsdiff”
CK_FSDIFF=”fsdiff -c $HASH”
# Where to start fsdiff in the filetree
# DO NOT CHANGE unless you know what you're doing!
DIFF_PATH=/
# Where the radmind client files live
RADMIND_CLIENT_DIR=/var/radmind/client
# the following block makes a temp file for fsdiff securely
DIFF_FILE=`mktemp /tmp/fsdiff.T.XXXXXX`
if [ $? -ne 0 ]; then
echo “$ME: could not create diff file”
exit 1
fi
# This is the number of changes fsdiff found
DELTA=”0″
# Maximum number of times you want to loop though ktcheck/fsdiff/lapply
MAX_LAPPLY_LOOPS=5
# the path to an image file to use for iHook's background
IHOOK_BACKGROUND=”none”
# DEFAULT: Speedy tells us to either use checksumming or not with fsdiff
SPEEDY=”no”
# DEFAULT: Should we run a ktcheck?
DO_KTCHECK=”yes”
# Should we console message?
USE_CONSOLE_MESSAGE=”no”
# DEFAULT: use iHook % vocab
USE_IHOOK=”no”
# DEFAULT: Reboot the machine when complete?
REBOOT=”yes”
####
# You should not need to change anything below this line
####
function say() {
if [ $USE_CONSOLE_MESSAGE = "yes" ]; then
ConsoleMessage “$1″
else
echo “$1″
fi
}
function isay() {
if [ $USE_CONSOLE_MESSAGE = "no" ]; then
if [ $USE_IHOOK = "yes" ]; then
echo “$1″
fi
fi
}
function ipause() {
if [ $USE_CONSOLE_MESSAGE = "no" ]; then
if [ $USE_IHOOK = "yes" ]; then
sleep 2
fi
fi
}
function bail() {
say “$ME: ERROR: $1″
if [ $USE_IHOOK = "yes" ]; then
sleep 2
fi
exit 1
}
function do_ktcheck() {
if [ $DO_KTCHECK = "yes" ]; then
say “Removing old command and transcript files…”
rm -f $RADMIND_CLIENT_DIR/*T
say “Done!”
say “Updating Command and transcript files…”
ktcheck -c $HASH -h $SERVER 1>&2
if [ $? -gt 1 ]; then
bail “ACK.. ktcheck failed for $SERVER”
else
say “Done!”
fi
else
say “Update of command files skipped (clearly you're not worthy)”
ipause
fi
isay “%10″
}
function do_fsdiff() {
if [ $SPEEDY = "yes" ]; then
say “Generating filesystem diff. Please wait.”
$FSDIFF -A $DIFF_PATH > $DIFF_FILE
else
/Library/Management/log_update
say “Generating filesystem diff with checksum. Go get coffee.”
$CK_FSDIFF -A $DIFF_PATH > $DIFF_FILE
fi
if [ $? -ne 0 ]; then
bail “fsdiff died!”
else
DELTA=`wc -l < $DIFF_FILE | tr -d [:blank:]`
fi
isay “%60″
}
function do_lapply() {
say “Resolving $DELTA discovered changes to orginal state”
lapply -c $HASH -h $SERVER -F $DIFF_FILE 1>&2
if [ $? -eq 1 ]; then
bail “network lapply died”
fi
}
function printhelp() {
echo usage: `basename $0` [-s] [-k] [-c] [-i] [-r]
echo usage: `basename $0` -h
echo Options are as Follows:
echo “-s Perform fsdiff without using checksumming (speedier)”
echo “-k Update the command files using ktcheck”
echo “-c Output messages using ConsoleMessage instead of echo”
echo “-i Output addtional iHook Status Messages”
echo “-r Reboot this machine when finished if all goes well”
echo “-h Print this help (ignores -c flag, always echos)”
echo “”
exit 0
}
function report_alert() {
logger -i -t $ME “ALERT: $HOST did not finish a image run completely!”
}
while getopts “:skcirh” Option
do
case $Option in
s)
SPEEDY=”yes”;;
k)
DO_KTCHECK=”yes”;;
c)
USE_CONSOLE_MESSAGE=”yes”;;
i)
USE_IHOOK=”yes”;;
r)
REBOOT=”yes”;;
h)
printhelp;;
*)
nothing;;
esac
done
shift $(($OPTIND – 1))
if [ $IHOOK_BACKGROUND != "none" ]; then
isay “%$IHOOK_BACKGROUND”
fi
isay “%0″
say “—-==>> Now imaging this client with Radmind! <<==----"
if [ -f /var/db/.DoNotRadmind ]; then
say “Radmind run skipped due to client configuration”
exit 0
fi
CURR_LAPPLY=0
while [ $CURR_LAPPLY -lt $MAX_LAPPLY_LOOPS ]; do
do_ktcheck
do_fsdiff
do_lapply
# Here's a bit of trickery, lapply will output one of three return codes
# 0 = All went well, I'm finished
# 1 = Something happened and I consider it it fatal, goodbye
# >2 = Something happened, but I modified the system, you should try again
#
# The do_lapply will catch a return of 1 and bail, so all we need to do is
# look for a return of 0. If we don't get that we'll simply up out
# counter and try again, the loop will break us when we hit our
# MAX_LAPPLY_LOOPS. Note that we're depending on $? having the correct
# infomation at the end of the loop, please be sure not to not modify
# the above mentioned functions unless you're aware of what $? is doing.
if [ $? -eq 0 ]; then
break
else
CURR_LAPPLY=`expr $CURR_LAPPLY + 1`
fi
done
if [ $CURR_LAPPLY = $MAX_LAPPLY_LOOPS ]; then
say “YIPES! We've encountered a problem!”
ipause
say “Drive is in a semi-restored state!”
ipause
report_alert
bail “Exiting, please alert someone!”
fi
isay “%100″
say “—-==>> Imaging of this client is complete! <<==----"
if [ $REBOOT = "yes" ]; then
say “Now rebooting this machine…. g'bye!”
/sbin/reboot
else
ipause
exit 0
fi