2008年6月19日 星期四

mass_ssh

經由 ssh, 呼叫一定數量機器運行特定指令.


#!/bin/sh
#
# call other machine to exec cmd by ssh
#

# function
exec_cmd()
{
LSUB=$1
IPS=$2
LCMD=$3

CHKRNG=`echo "${IPS}" | grep "-"`

if [ ${CHKRNG} ]; then
OIFS=$IFS
IFS="-"
SIP=
EIP=
for A in ${IPS}
do
if [ ! ${SIP} ]; then
SIP=${A}
else
EIP=${A}
fi
done
IFS=$OIFS

if [[ ${SIP} -gt ${EIP} ]]; then
echo "Start IP : ${SIP} greater than End IP : ${EIP}"
else
CIP=${SIP}
while [[ ${CIP} -le ${EIP} ]]
do
echo "Exec [${LCMD}] on ${LSUB}.${CIP}"
ssh ${LSUB}.${CIP} "$LCMD"
echo
CIP=$(($CIP + 1))
done
fi
else
echo "Exec [${LCMD}] on ${LSUB}.${IPS}"
ssh ${LSUB}.${IPS} "$LCMD"
echo
fi
}


if [[ $# -lt 3 ]]; then
echo "Not enough parameter."
echo "Usage : $0 "
echo "e.g. $0 192.168.0 ls 1 3 5"
exit -1
fi

ALLARGV=$@

if [[ -z ${ALLARGV} ]]; then
echo "Usage : $0 "
echo "e.g. $0 192.168.0 ls 1 3 5"
exit -1
fi

# separate Command and Machine Numbers
SUBNET=$1
CMD=$2
shift 2

if [[ -z "$@" ]]; then
echo "Please input "
echo "e.g $0 192.168.0 \"ls -l\" \"1-3\""
exit -1
fi

for A in $@
do
exec_cmd ${SUBNET} ${A} "${CMD}"
done

exit 0

沒有留言: