#!/bin/sh
# Author: Blake, Kuo-Lien Huang
# License: GPL
# Description: 
#   send the comand to all drbl clients
#

drblroot="/var/lib/diskless/default"

## --help -h
if [ "$1" = "-h" -o "$1" = "--help" ]; then
  echo "Usage:"
  echo "  $0 --wol                   use WOL to wake up all drbl clients"
  echo "  $0 [-u user] [-b] command  execute command on all drbl clients"
  exit 0
fi

## Wake on LAN (--wol)
if [ "$1" = "--wol" ]; then
  trap 'rm -f doit.zip; rm -rf doit; exit 1' HUP INT QUIT TERM
  tail +119 $0 > doit.zip
  unzip doit.zip
  broadcast_address="255.255.255.255"
  while read line
  do
    if [ "$(echo "$line" | grep "option broadcast-address")" != "" ]; then
      broadcast_address=$(echo "$line" | cut -d\; -f1 | awk '{ print $3; }')
    elif [ "$(echo "$line" | grep "hardware ethernet")" != "" ]; then
      ethernet_address=$(echo "$line" | cut -d\; -f1 | awk '{ print $3; }')
      echo "== $ethernet_address =="
      doit/wakeonlan -i $broadcast_address $ethernet_address
    fi
  done < /etc/dhcp3/dhcpd.conf
  rm -rf doit
  rm -rf doit.zip
  exit 0
fi

##
resshkeygen=0
if [ ! -f $HOME/.ssh/id_rsa ]; then
  trap 'rm -f doit.zip; rm -rf doit; exit 1' HUP INT QUIT TERM
  tail +119 $0 > doit.zip
  unzip doit.zip
  doit/autosshkeygen $USER
  mv $HOME/.ssh/id_rsa.pub $HOME/.ssh/authorized_keys
  rm -rf doit 
  rm -f doit.zip
  resshkeygen=1
fi

# check if the user is root
id=`id -u`
if [ "$id" == "0" ]; then
  for host in `ls $drblroot`
  do
    if [ "$host" = "." -o "$host" = ".." -o "$host" = "root" ]; then
      continue
    fi
    if [ $resshkeygen -eq 1 -o ! -f $drblroot/$host/root/.ssh/id_rsa ]; then
      mkdir -p $drblroot/$host/root/.ssh
      cp $HOME/.ssh/id_rsa $drblroot/$host/root/.ssh/id_rsa
      cp $HOME/.ssh/authorized_keys $drblroot/$host/root/.ssh/authorized_keys
    fi
  done
fi

## -u || --user
RUSER=$USER
if [ "$1" = "-u" -o "$1" = "--user" ]; then
  shift
  if [ "$1" = "" ]; then
    echo "Usage: $0 [-u|--user] username command"
    exit
  else
    RUSER="$1"
    shift
  fi
fi

## -b || --batch
batch_mode=0
if [ "$1" = "-b" -o "$1" = "--batch" ]; then
  shift
  batch_mode=1
fi

# 2003/08/15 before execute the command, rm ~/.ssh/known_hosts
rm -f $HOME/.ssh/known_hosts

# execute the command
for host in `ls $drblroot`
do
  if [ "$host" = "." -o "$host" = ".." -o "$host" = "root" ]; then
    continue
  fi
  # check if $host is myself
  me=`/sbin/ifconfig | grep "inet addr:$host" | cut -d: -f2 | cut -d' ' -f1 | grep "^$host$"`
  if [ "$me" = "" -o "$me" != "$host" ]; then
    echo "=== $host ==="
    if [ $batch_mode -eq 0 ]; then
      ## interactive mode, used for status check
      if /bin/ping -c 2 $host > /dev/null 2>&1; then
        if [ $# -lt 1 ]; then
          echo "$host is alive.."
        else
          ssh -o StrictHostKeyChecking=no $RUSER@$host $@
        fi
      else
        echo "$host is unreachable.." 
      fi
    else
      ## batch mode, used for sending command 
      ssh -o StrictHostKeyChecking=no $RUSER@$host $@ &
    fi
  fi
done
exit 0
PK    
}.WV       doit/autosshkeygenUT	 dt>|!AUx d uA0}O,P#)1]\-CBɰ3<q+-mnmF9aTB'ƶRG="бpo#_[	Qɼ78	#.>Tҙ<c|*5.گl GRo7PK    H1AUI  >    doit/wakeonlanUT	 sA|!AUx d XsFkDpLa4etNL[tN8io<w{{gWmdJ6&<n$LFp}q6GO!Gntǲ2@~	,I꺣40O*+oף
*?^|p󑘏i*Ϡt˚ڷ7ۇϟ;()8\K*̮\ufAR;uRL)HCϽ87M{"q;um)o	{M|XD"y^JX0xo9o2N;˃R;; ^zYO&Ng/]uSb ``%S)Vf;,Ro*$"
Үg&$ܛqP[Eb*R>kLE%`V*ǖ\+PSbO೗,Rc*Wps<klrJ}+3Th9X!2Eͱj^?F*(\sT9*S,oʓ矛˅	$e/qTNMcX2ڹKiB"``3#c+5g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