-
Notifications
You must be signed in to change notification settings - Fork 899
Ssh resource model generator
#!/bin/bash
# Requires: mktemp
#
# DESCRIPTION
# Takes a set of newline delimited hosts and runs a remote
# script to collect host metadata to generate resources.xml document.
#
# DISCOVERING HOSTS
# Hosts lists can be generated with a tool like nmap. Here an nmap script
# returns hosts listening on SSH port 22 on the specified subnet:
# nmap -sP 10.0.1.* -PS22 -oG nmap.out && \
# awk '/Status: Up/ {print $2}' nmap.out > hosts
die() { echo $* 1>&2 ; exit 1 ; }
PROG=basename $0
WORKSPACE=$(mktemp -d /tmp/${PROG}.XXXX) || die
SSH_ARGS=
SSH_USER=$(whoami)
VERBOSE=0
NODE_SCRIPT=
HOST_FILE=
SSHID_FILE=
usage()
{
cat <<EOF
usage: $PROG options
OPTIONS:
-h Show this message
-i identify file (default reads current user)
-f hosts file (default reads stdin)
-s script file (default uses internal)
-u Remote SSH username (default=$SSH_USER)
EXAMPLES:
Read hosts from stdin
echo "dev01" | $PROG > resources.xml
Read hosts from a file
$PROG -f hostsfile > resources.xml
Use an external collection script
$PROG -f hostsfile -s myscript.sh > resources.xml
EOF
}
while getopts "hvu:i:f:s:" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
v)
VERBOSE=1
;;
u)
SSH_USER=$OPTARG
;;
i)
SSHID_FILE=$OPTARG
;;
f)
HOST_FILE=$OPTARG
;;
s)
NODE_SCRIPT=$OPTARG
;;
*)
usage
exit 1
esac
done
verbose() { [ "$VERBOSE" == "1" ] && { echo "VERBOSE : $*" 1>&2 ; } }
[ -s "$HOST_FILE" -a ! -r "$HOST_FILE" ] && {
die "file not readable: $HOST_FILE"
}
[ -s "$SSHID_FILE" -a ! -r "$SSHID_FILE" ] && {
die "file not readable: $SSHID_FILE"
} || {
SSH_ARGS="$SSH_ARGS -i $SSHID_FILE"
}
mkdir -p ${WORKSPACE} || { die "Failed creating data directory: $WORKSPACE" ; }
if [ -z "${NODE_SCRIPT}" ]
then
NODE_SCRIPT=mktemp ${WORKSPACE}/node-collect.XXXX
cat > ${NODE_SCRIPT} <<EOF
#!/bin/bash
USAGE="$0 "
[ $# = 1 ] || { echo "$USAGE" ; exit 1 ; }
outputfile=$1
hostname=$(hostname)
osArch=$(uname -p)
osName=$(uname -s)
osVers=$(uname -r)
username=$(whoami)
dstamp=$(date "+%Y-%m-%d %H:%M:%S")
echo " <node name='${hostname}' description='Last updated ${dstamp}'
osArch='${osArch}' osName='${osName}' osVers='${osVers}'
username='${username}'
tags='' />" > $outputfile
EOF
fi
[ -f "$HOST_FILE" ] && { exec 0<> $HOST_FILE ; }
verbose "{SSH_USER="$SSH_USER", WORKSPACE="$WORKSPACE", HOST_FILE="$HOST_FILE", SSH_ARGS="$SSH_ARGS"}"
numProcessed=0
while read line
do
echo $line | egrep -q '^#' && continue ;#skip comment lines
hostinfo=( $line ) ;#parse the line into an array
[ ${#hostinfo[*]} -lt 1 ] && continue ;#skip lines with no info
host=${hostinfo[0]}
verbose "collecting node info from host: ${host} ..."
scp $SSH_ARGS $NODE_SCRIPT ${SSH_USER}@${host}:/tmp/$(basename $NODE_SCRIPT) || {
die "Failed copying collection script on host: ${host}"
}
ssh $SSH_ARGS -n ${SSH_USER}@${host} sh /tmp/$(basename $NODE_SCRIPT) /tmp/node.xml.$$ || {
die "Failed executing collection script on host: ${host}"
}
scp $SSH_ARGS ${SSH_USER}@${host}:/tmp/node.xml.$$ ${WORKSPACE}/${host}.xml || {
die "Failed copying resources data from host: ${host}"
}
numProcessed=$(expr "$numProcessed" + 1)
done
if [ "$numProcessed" -gt 0 ]
then
verbose "Generating resources.xml for $i hosts ..."
echo ""
cat ${WORKSPACE}/*.xml || die "Failure due to internal script error"
echo ""
verbose "Done."
fi
verbose "cleaning up temporary files"
rm -rf ${WORKSPACE}