| 1 | #!/bin/bash |
| 2 | # Author: Blake, Kuo-Lien Huang and Steven Shiau <steven _at_ clonezilla org> |
| 3 | # License: GPL |
| 4 | # Description: send commands to drbl clients |
| 5 | # |
| 6 | |
| 7 | # Load DRBL setting and functions |
| 8 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 9 | |
| 10 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 11 | |
| 12 | # some userful function |
| 13 | USAGE() { |
| 14 | echo "Usage: `basename $0` Options [command]" |
| 15 | echo "Options:" |
| 16 | echo "-b, --batch Batch mode, same with -n/--no-ping" |
| 17 | echo "-h, --hosts IP_LIST Instead of all DRBL clients, assign the clients by IP address, like: -h \"192.168.0.1 192.168.0.2\" NOTE!!! You must put \" \" before and after the IP_LIST!" |
| 18 | echo "-n, --no-ping Do NOT ping machine to check if hosts are alive before running command" |
| 19 | echo "-p, --password PASSWD Assign the plain text password of the USER (option -u|--user) to be used. //NOTE// This is not secure. It's recommended to avoid using this option. Better to use the ssh public key method." |
| 20 | echo "-u, --user USER Execute command on all drbl clients as username USER" |
| 21 | echo "-v, --verbose: Verbose mode." |
| 22 | echo "-w, --wol Use Wake On LAN to wake up all drbl clients (Clients must be soft shutdown)" |
| 23 | echo "Examples:" |
| 24 | echo " To poweroff all DRBL clients" |
| 25 | echo " `basename $0` -u root /sbin/poweroff" |
| 26 | echo " To reboot all Cygwin clients, use" |
| 27 | echo " `basename $0` -u Administrator /usr/bin/reboot -f now" |
| 28 | } |
| 29 | |
| 30 | # main |
| 31 | [ $# -eq 0 ] && USAGE && exit 1 |
| 32 | |
| 33 | # Parse command |
| 34 | while [ $# -gt 0 ]; do |
| 35 | case "$1" in |
| 36 | -l|--language) |
| 37 | shift |
| 38 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 39 | # skip the -xx option, in case |
| 40 | specified_lang="$1" |
| 41 | shift |
| 42 | fi |
| 43 | [ -z "$specified_lang" ] && USAGE && exit 1 |
| 44 | ;; |
| 45 | -w|--wol) |
| 46 | shift; mode="WOL" ;; |
| 47 | -h|--hosts) |
| 48 | LIST_HOST="on" |
| 49 | shift |
| 50 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 51 | # skip the -xx option, in case |
| 52 | IP_LIST="$1" |
| 53 | shift |
| 54 | fi |
| 55 | [ -z "$IP_LIST" ] && USAGE && exit 1 |
| 56 | ;; |
| 57 | -u|--user) |
| 58 | shift |
| 59 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 60 | # skip the -xx option, in case |
| 61 | specified_user="$1" |
| 62 | shift |
| 63 | fi |
| 64 | [ -z "$specified_user" ] && USAGE && exit 1 |
| 65 | ;; |
| 66 | -p|--password) |
| 67 | shift |
| 68 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 69 | # skip the -xx option, in case |
| 70 | specified_passwd="$1" |
| 71 | shift |
| 72 | fi |
| 73 | [ -z "$specified_passwd" ] && USAGE && exit 1 |
| 74 | ;; |
| 75 | -n|--no-ping|-b|--batch) |
| 76 | shift; ping="no" ;; |
| 77 | -v|--verbose) |
| 78 | shift; verbose="on" ;; |
| 79 | -*) echo "${0}: ${1}: invalid option" >&2 |
| 80 | USAGE >& 2 |
| 81 | exit 2 ;; |
| 82 | *) break ;; |
| 83 | esac |
| 84 | done |
| 85 | |
| 86 | # get the command |
| 87 | CMD=$1 |
| 88 | [ -n "$verbose" ] && echo "CMD:$CMD" |
| 89 | [ -n "$verbose" ] && echo "specified_user: $specified_user" |
| 90 | |
| 91 | # |
| 92 | ask_and_load_lang_set $specified_lang |
| 93 | |
| 94 | # The reason we do not to use $HOME is that sudo will not change |
| 95 | # environmental variable $HOME, but it will change the $USER |
| 96 | # we need to know who is really running this after applying sudo. |
| 97 | # say, sudo echo "$HOME", it will show user's home, instead of root's home, |
| 98 | REALHOME=$(LC_ALL=C grep -Ew "^$USER" /etc/passwd | cut -d":" -f6) |
| 99 | |
| 100 | # get client IP lists |
| 101 | drbl_client_list="$(LC_ALL=C get-client-ip-list)" |
| 102 | |
| 103 | # |
| 104 | case "$mode" in |
| 105 | "WOL") |
| 106 | ## Wake on LAN (--wol) |
| 107 | broadcast_address="255.255.255.255" |
| 108 | echo -n "Parsing the $DHCPDCONF_DIR/dhcpd.conf..." |
| 109 | WOL_TMP=`mktemp /tmp/wol_tmp.XXXXXX` |
| 110 | trap "[ -f "$WOL_TMP" ] && rm -f $WOL_TMP" 0 1 2 5 15 |
| 111 | parse_dhcpd_conf $WOL_TMP |
| 112 | echo "done!" |
| 113 | # find the interfaces for the wake-on-lan packet to be sent, in DRBL, only |
| 114 | # private IP address is used to connect to client |
| 115 | |
| 116 | # only root can update arp data |
| 117 | if [ "$UID" = "0" ]; then |
| 118 | # Dave Haakenhout suggests to create the static ethers files so that |
| 119 | # the arp can work. |
| 120 | echo -n "Creating the /etc/ethers... " |
| 121 | if [ -f /etc/ethers ]; then |
| 122 | echo -n "Backup the original /etc/ethers as /etc/ethers.orig... " |
| 123 | mv -f /etc/ethers /etc/ethers.orig |
| 124 | fi |
| 125 | # skip those comment lines |
| 126 | (grep "^[^#]" $WOL_TMP; echo ) | # make sure there is a LF at the end |
| 127 | while read host ip mac; do |
| 128 | echo $mac $ip >> /etc/ethers |
| 129 | done |
| 130 | echo -n "Reading the ARP address mapping... " |
| 131 | arp -f /etc/ethers |
| 132 | echo "done!" |
| 133 | fi |
| 134 | |
| 135 | for ihost in $drbl_client_list; do |
| 136 | # skip those IP not listed in the $IP_LIST |
| 137 | if [ "$LIST_HOST" = "on" ]; then |
| 138 | [ -z "$(echo $IP_LIST | grep -E "\<$ihost\>")" ] && continue |
| 139 | fi |
| 140 | ethernet_address="$(grep -E "${ihost}\>" $WOL_TMP | awk -F" " '{print $3}')" |
| 141 | echo "-------------------------------------------------------------------" |
| 142 | if [ -n "$ethernet_address" ]; then |
| 143 | # find the etherwake/ether-wake program first. For FC: ether-wake, for Debian: etherwake. |
| 144 | if type etherwake &>/dev/null; then |
| 145 | ETH_WAKE="etherwake" |
| 146 | elif type ether-wake &>/dev/null; then |
| 147 | ETH_WAKE="ether-wake" |
| 148 | else |
| 149 | ETH_WAKE="" |
| 150 | fi |
| 151 | wnl_cmd="" |
| 152 | # use etherwake/ether-wake for the 1st priority, but it need to be root to run it |
| 153 | if [ -n "$ETH_WAKE" ] && [ "$UID" = "0" ]; then |
| 154 | # find the ether port, we can find it from the routing table |
| 155 | # 1st, find the subnet: 192.168.1.x -> 192.168.1 |
| 156 | IP_grp_1_3="$(LC_ALL=C echo $ihost | sed -e "s/\.[[:digit:]]*$//g")" |
| 157 | iport="$(LC_ALL=C ip route show | grep -E "^$IP_grp_1_3\." | awk '{for (i=1; i<=NF; i++) if ($i=="dev") print $(i+1)}')" |
| 158 | wnl_cmd="$ETH_WAKE -i $iport $ethernet_address" |
| 159 | echo "Running: $wnl_cmd" |
| 160 | eval $wnl_cmd |
| 161 | else |
| 162 | wnl_cmd="drbl-wakeonlan -i ${ihost} $ethernet_address" |
| 163 | echo "Running: $wnl_cmd" |
| 164 | eval $wnl_cmd |
| 165 | fi |
| 166 | else |
| 167 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 168 | echo "The MAC address of the specified host ${ihost} NOT found!" |
| 169 | echo "Do you use the range mode in dhcpd.conf or is there any stale directory in $drblroot/ ?" |
| 170 | echo "Skip host ${ihost}!" |
| 171 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 172 | fi |
| 173 | done |
| 174 | [ -f "$WOL_TMP" ] && rm -f $WOL_TMP |
| 175 | |
| 176 | echo "$msg_delimiter_star_line" |
| 177 | echo "$msg_wol_0" |
| 178 | echo "$msg_wol_1" |
| 179 | echo "$msg_wol_2" |
| 180 | echo "$msg_wol_3" |
| 181 | echo "$msg_wol_4" |
| 182 | echo "$msg_delimiter_star_line" |
| 183 | |
| 184 | exit 0 |
| 185 | ;; |
| 186 | *) |
| 187 | # for mode "RUN"... |
| 188 | [ -z "$CMD" ] && USAGE && exit 1 |
| 189 | true |
| 190 | esac |
| 191 | |
| 192 | # create the rsa key if not exists |
| 193 | if [ ! -f $REALHOME/.ssh/id_rsa ]; then |
| 194 | ssh-keygen -t rsa -q -f $REALHOME/.ssh/id_rsa -N "" |
| 195 | fi |
| 196 | |
| 197 | # Put authorized_keys for the user. 2 cases: |
| 198 | # (1). It is root running drbl-doit. |
| 199 | # (2). It is normal user running drbl-doit. |
| 200 | # For root, we will NOT copy authorized_keys in server, since the root in the client should not share the same authorized_keys with that in server. We only put them in the client. (Note! Every client has its own root directory in $ihost/root/) |
| 201 | # For normal user, we let user can ssh login back to server and other machine without password (Note! This is NFS-based home, so we just have to copy id_rsa.pub as authorized_keys in user's home, then no matter which machine user logins, it will use this key). |
| 202 | if [ "$UID" = "0" ]; then |
| 203 | # for root, copy id_rsa.pub as authorized_keys in clients. |
| 204 | for ihost in $drblroot/*; do |
| 205 | if [ -f "$REALHOME/.ssh/id_rsa.pub" ]; then |
| 206 | mkdir -p $ihost/root/.ssh |
| 207 | cp -af $REALHOME/.ssh/id_rsa.pub $ihost/root/.ssh/authorized_keys |
| 208 | fi |
| 209 | done |
| 210 | else |
| 211 | if [ -f "$REALHOME/.ssh/id_rsa.pub" ]; then |
| 212 | cp -af $REALHOME/.ssh/id_rsa.pub $REALHOME/.ssh/authorized_keys |
| 213 | fi |
| 214 | fi |
| 215 | |
| 216 | # if user is not specified, set it as the one who is running this script |
| 217 | [ -z "$specified_user" ] && specified_user="$USER" |
| 218 | |
| 219 | # |
| 220 | if [ "$LIST_HOST" = "on" ]; then |
| 221 | for ihost in $IP_LIST; do |
| 222 | [ ! -d "$drblroot/$ihost" ] && echo "Can NOT find DRBL client $ihost (i.e. no $drblroot/$ihost)! Assume it's DRBL SSI client." |
| 223 | done |
| 224 | [ -n "$verbose" ] && echo "IP_LIST: $IP_LIST" |
| 225 | fi |
| 226 | |
| 227 | # Execute the command |
| 228 | for IP in $drbl_client_list; do |
| 229 | # skip those IP not listed in the $IP_LIST |
| 230 | if [ "$LIST_HOST" = "on" ]; then |
| 231 | [ -z "$(echo $IP_LIST | grep -E "\<$IP\>")" ] && continue |
| 232 | fi |
| 233 | # check if $IP is myself |
| 234 | me="$(LC_ALL=C ip addr show | grep -w "$IP" | awk '{print $2}' | cut -d/ -f1 | head -n1)" |
| 235 | if [ "$me" = "" -o "$me" != "$IP" ]; then |
| 236 | echo "Sending command \"$@\" to $specified_user@$IP..." |
| 237 | if [ "$ping" = "no" ]; then |
| 238 | if [ -n "$specified_passwd" ]; then |
| 239 | if [ -n "$(echo $@ | grep -E "(reboot|poweroff)")" ]; then |
| 240 | # If command includes reboot or shutdown, redirect the output to /dev/null. |
| 241 | echo "spawn ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $specified_user@$IP $@; expect password:; send $specified_passwd\r; expect eof" | expect -f - &>/dev/null & |
| 242 | else |
| 243 | echo "spawn ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $specified_user@$IP $@; expect password:; send $specified_passwd\r; expect eof" | expect -f - & |
| 244 | fi |
| 245 | else |
| 246 | if [ -n "$(echo $@ | grep -E "(reboot|poweroff)")" ]; then |
| 247 | # If command includes reboot or shutdown, redirect the output to /dev/null. |
| 248 | ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $specified_user@$IP $@ &>/dev/null & |
| 249 | else |
| 250 | ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $specified_user@$IP $@ & |
| 251 | fi |
| 252 | fi |
| 253 | else |
| 254 | # ping before run command |
| 255 | if ping -c 2 $IP > /dev/null 2>&1; then |
| 256 | [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS |
| 257 | echo "$IP is alive... Sending command now..." |
| 258 | if [ -n "$specified_passwd" ]; then |
| 259 | if [ -n "$(echo $@ | grep -E "(reboot|poweroff)")" ]; then |
| 260 | # If command includes reboot or shutdown, redirect the output to /dev/null. |
| 261 | echo "spawn ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $specified_user@$IP $@; expect password:; send $specified_passwd\r; expect eof" | expect -f - &>/dev/null & |
| 262 | else |
| 263 | echo "spawn ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $specified_user@$IP $@; expect password:; send $specified_passwd\r; expect eof" | expect -f - & |
| 264 | fi |
| 265 | else |
| 266 | if [ -n "$(echo $@ | grep -E "(reboot|poweroff)")" ]; then |
| 267 | # If command includes reboot or shutdown, redirect the output to /dev/null. |
| 268 | ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $specified_user@$IP $@ &>/dev/null & |
| 269 | else |
| 270 | ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $specified_user@$IP $@ & |
| 271 | fi |
| 272 | fi |
| 273 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 274 | else |
| 275 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 276 | echo "$IP is unreachable... Abort command." |
| 277 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 278 | fi |
| 279 | fi |
| 280 | fi |
| 281 | done |
| 282 | # wait for all processes to finish before exit |
| 283 | wait |
| 284 | |
| 285 | exit 0 |