| 1 | #!/bin/bashscip-shell filesystem . . `/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 | USAGEscip-shell shell shell-project 0.0.0 USAGE().() { |
| 14 | echoscip-shell system . . echo. "Usage: `basenamescip-shell system . . basename. $0` Options [command]" |
| 15 | echoscip-shell system . . echo. "Options:" |
| 16 | echoscip-shell system . . echo. "-b, --batch Batch mode, same with -n/--no-ping" |
| 17 | echoscip-shell system . . 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 | echoscip-shell system . . echo. "-n, --no-ping Do NOT ping machine to check if hosts are alive before running command" |
| 19 | echoscip-shell system . . 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 | echoscip-shell system . . echo. "-u, --user USER Execute command on all drbl clients as username USER" |
| 21 | echoscip-shell system . . echo. "-v, --verbose: Verbose mode." |
| 22 | echoscip-shell system . . echo. "-w, --wol Use Wake On LAN to wake up all drbl clients (Clients must be soft shutdown)" |
| 23 | echoscip-shell system . . echo. "Examples:" |
| 24 | echoscip-shell system . . echo. " To poweroff all DRBL clients" |
| 25 | echoscip-shell system . . echo. " `basenamescip-shell system . . basename. $0` -u root /sbin/poweroff" |
| 26 | echoscip-shell system . . echo. " To reboot all Cygwin clients, use" |
| 27 | echoscip-shell system . . echo. " `basenamescip-shell system . . basename. $0` -u Administrator /usr/bin/reboot -f now" |
| 28 | } |
| 29 | |
| 30 | # main |
| 31 | [scip-shell system . . `[`. $# -eq 0 ] && USAGEscip-shell shell shell-project 0.0.0 USAGE(). && exit 1 |
| 32 | |
| 33 | # Parse command |
| 34 | while [scip-shell system . . `[`. $# -gt 0 ]; do |
| 35 | case "$1" in |
| 36 | -l|--language) |
| 37 | shift |
| 38 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . grep. ^-.)" ]; then |
| 39 | # skip the -xx option, in case |
| 40 | specified_lang="$1" |
| 41 | shift |
| 42 | fi |
| 43 | [scip-shell system . . `[`. -z "$specified_lang" ] && USAGEscip-shell shell shell-project 0.0.0 USAGE(). && exit 1 |
| 44 | ;; |
| 45 | -w|--wol) |
| 46 | shift; mode="WOL" ;; |
| 47 | -h|--hosts) |
| 48 | LIST_HOST="on" |
| 49 | shift |
| 50 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . grep. ^-.)" ]; then |
| 51 | # skip the -xx option, in case |
| 52 | IP_LIST="$1" |
| 53 | shift |
| 54 | fi |
| 55 | [scip-shell system . . `[`. -z "$IP_LIST" ] && USAGEscip-shell shell shell-project 0.0.0 USAGE(). && exit 1 |
| 56 | ;; |
| 57 | -u|--user) |
| 58 | shift |
| 59 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . grep. ^-.)" ]; then |
| 60 | # skip the -xx option, in case |
| 61 | specified_user="$1" |
| 62 | shift |
| 63 | fi |
| 64 | [scip-shell system . . `[`. -z "$specified_user" ] && USAGEscip-shell shell shell-project 0.0.0 USAGE(). && exit 1 |
| 65 | ;; |
| 66 | -p|--password) |
| 67 | shift |
| 68 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . grep. ^-.)" ]; then |
| 69 | # skip the -xx option, in case |
| 70 | specified_passwd="$1" |
| 71 | shift |
| 72 | fi |
| 73 | [scip-shell system . . `[`. -z "$specified_passwd" ] && USAGEscip-shell shell shell-project 0.0.0 USAGE(). && exit 1 |
| 74 | ;; |
| 75 | -n|--no-ping|-b|--batch) |
| 76 | shift; ping="no" ;; |
| 77 | -v|--verbose) |
| 78 | shift; verbose="on" ;; |
| 79 | -*) echoscip-shell system . . echo. "${0}: ${1}: invalid option" >&2 |
| 80 | USAGEscip-shell shell shell-project 0.0.0 USAGE(). >& 2 |
| 81 | exit 2 ;; |
| 82 | *) break ;; |
| 83 | esac |
| 84 | done |
| 85 | |
| 86 | # get the command |
| 87 | CMD=$1 |
| 88 | [scip-shell system . . `[`. -n "$verbose" ] && echoscip-shell system . . echo. "CMD:$CMD" |
| 89 | [scip-shell system . . `[`. -n "$verbose" ] && echoscip-shell system . . 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 grepscip-shell system . . grep. -Ew "^$USER" /etc/passwdscip-shell filesystem . . `/etc/passwd`. | cutscip-shell system . . 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 | echoscip-shell system . . echo. -n "Parsing the $DHCPDCONF_DIR/dhcpd.conf..." |
| 109 | WOL_TMP=`mktempscip-shell system . . mktemp. /tmp/wol_tmp.XXXXXXscip-shell filesystem . . `/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 | echoscip-shell system . . 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 [scip-shell system . . `[`. "$UID" = "0" ]; then |
| 118 | # Dave Haakenhout suggests to create the static ethers files so that |
| 119 | # the arp can work. |
| 120 | echoscip-shell system . . echo. -n "Creating the /etc/ethers... " |
| 121 | if [scip-shell system . . `[`. -f /etc/ethersscip-shell filesystem . . `/etc/ethers`. ]; then |
| 122 | echoscip-shell system . . echo. -n "Backup the original /etc/ethers as /etc/ethers.orig... " |
| 123 | mvscip-shell system . . mv. -f /etc/ethersscip-shell filesystem . . `/etc/ethers`. /etc/ethers.origscip-shell filesystem . . `/etc/ethers.orig`. |
| 124 | fi |
| 125 | # skip those comment lines |
| 126 | (grepscip-shell system . . grep. "^[^#]" $WOL_TMP; echoscip-shell system . . echo. ) | # make sure there is a LF at the end |
| 127 | while read host ip mac; do |
| 128 | echoscip-shell system . . echo. $mac $ip >> /etc/ethersscip-shell filesystem . . `/etc/ethers`. |
| 129 | done |
| 130 | echoscip-shell system . . echo. -n "Reading the ARP address mapping... " |
| 131 | arp -f /etc/ethersscip-shell filesystem . . `/etc/ethers`. |
| 132 | echoscip-shell system . . 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 [scip-shell system . . `[`. "$LIST_HOST" = "on" ]; then |
| 138 | [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $IP_LIST | grepscip-shell system . . grep. -E "\<$ihost\>")" ] && continue |
| 139 | fi |
| 140 | ethernet_address="$(grepscip-shell system . . grep. -E "${ihost}\>" $WOL_TMP | awkscip-shell system . . awk. -F" " '{print $3}')" |
| 141 | echoscip-shell system . . echo. "-------------------------------------------------------------------" |
| 142 | if [scip-shell system . . `[`. -n "$ethernet_address" ]; then |
| 143 | # find the etherwake/ether-wake program first. For FC: ether-wake, for Debian: etherwake. |
| 144 | if type etherwake &>/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 145 | ETH_WAKE="etherwake" |
| 146 | elif type ether-wake &>/dev/nullscip-shell filesystem . . `/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 [scip-shell system . . `[`. -n "$ETH_WAKE" ] && [scip-shell system . . `[`. "$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 echoscip-shell system . . echo. $ihost | sedscip-shell system . . sed. -e "s/\.[[:digit:]]*$//g")" |
| 157 | iport="$(LC_ALL=C ip route show | grepscip-shell system . . grep. -E "^$IP_grp_1_3\." | awkscip-shell system . . awk. '{for (i=1; i<=NF; i++) if ($i=="dev") print $(i+1)}')" |
| 158 | wnl_cmd="$ETH_WAKE -i $iport $ethernet_address" |
| 159 | echoscip-shell system . . echo. "Running: $wnl_cmd" |
| 160 | eval $wnl_cmd |
| 161 | else |
| 162 | wnl_cmd="drbl-wakeonlan -i ${ihost} $ethernet_address" |
| 163 | echoscip-shell system . . echo. "Running: $wnl_cmd" |
| 164 | eval $wnl_cmd |
| 165 | fi |
| 166 | else |
| 167 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 168 | echoscip-shell system . . echo. "The MAC address of the specified host ${ihost} NOT found!" |
| 169 | echoscip-shell system . . echo. "Do you use the range mode in dhcpd.conf or is there any stale directory in $drblroot/ ?" |
| 170 | echoscip-shell system . . echo. "Skip host ${ihost}!" |
| 171 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 172 | fi |
| 173 | done |
| 174 | [scip-shell system . . `[`. -f "$WOL_TMP" ] && rmscip-shell system . . rm. -f $WOL_TMP |
| 175 | |
| 176 | echoscip-shell system . . echo. "$msg_delimiter_star_line" |
| 177 | echoscip-shell system . . echo. "$msg_wol_0" |
| 178 | echoscip-shell system . . echo. "$msg_wol_1" |
| 179 | echoscip-shell system . . echo. "$msg_wol_2" |
| 180 | echoscip-shell system . . echo. "$msg_wol_3" |
| 181 | echoscip-shell system . . echo. "$msg_wol_4" |
| 182 | echoscip-shell system . . echo. "$msg_delimiter_star_line" |
| 183 | |
| 184 | exit 0 |
| 185 | ;; |
| 186 | *) |
| 187 | # for mode "RUN"... |
| 188 | [scip-shell system . . `[`. -z "$CMD" ] && USAGEscip-shell shell shell-project 0.0.0 USAGE(). && exit 1 |
| 189 | truescip-shell system . . true. |
| 190 | esac |
| 191 | |
| 192 | # create the rsa key if not exists |
| 193 | if [scip-shell system . . `[`. ! -f $REALHOME/.ssh/id_rsa ]; then |
| 194 | ssh-keygenscip-shell system . . 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 [scip-shell system . . `[`. "$UID" = "0" ]; then |
| 203 | # for root, copy id_rsa.pub as authorized_keys in clients. |
| 204 | for ihost in $drblroot/*; do |
| 205 | if [scip-shell system . . `[`. -f "$REALHOME/.ssh/id_rsa.pub" ]; then |
| 206 | mkdirscip-shell system . . mkdir. -p $ihost/root/.ssh |
| 207 | cpscip-shell system . . cp. -af $REALHOME/.ssh/id_rsa.pub $ihost/root/.ssh/authorized_keys |
| 208 | fi |
| 209 | done |
| 210 | else |
| 211 | if [scip-shell system . . `[`. -f "$REALHOME/.ssh/id_rsa.pub" ]; then |
| 212 | cpscip-shell system . . 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 | [scip-shell system . . `[`. -z "$specified_user" ] && specified_user="$USER" |
| 218 | |
| 219 | # |
| 220 | if [scip-shell system . . `[`. "$LIST_HOST" = "on" ]; then |
| 221 | for ihost in $IP_LIST; do |
| 222 | [scip-shell system . . `[`. ! -d "$drblroot/$ihost" ] && echoscip-shell system . . echo. "Can NOT find DRBL client $ihost (i.e. no $drblroot/$ihost)! Assume it's DRBL SSI client." |
| 223 | done |
| 224 | [scip-shell system . . `[`. -n "$verbose" ] && echoscip-shell system . . 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 [scip-shell system . . `[`. "$LIST_HOST" = "on" ]; then |
| 231 | [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $IP_LIST | grepscip-shell system . . grep. -E "\<$IP\>")" ] && continue |
| 232 | fi |
| 233 | # check if $IP is myself |
| 234 | me="$(LC_ALL=C ip addr show | grepscip-shell system . . grep. -w "$IP" | awkscip-shell system . . awk. '{print $2}' | cutscip-shell system . . cut. -d/ -f1 | headscip-shell system . . head. -n1)" |
| 235 | if [scip-shell system . . `[`. "$me" = "" -o "$me" != "$IP" ]; then |
| 236 | echoscip-shell system . . echo. "Sending command \"$@\" to $specified_user@$IP..." |
| 237 | if [scip-shell system . . `[`. "$ping" = "no" ]; then |
| 238 | if [scip-shell system . . `[`. -n "$specified_passwd" ]; then |
| 239 | if [scip-shell system . . `[`. -n "$(echoscip-shell system . . echo. $@ | grepscip-shell system . . grep. -E "(reboot|poweroff)")" ]; then |
| 240 | # If command includes reboot or shutdown, redirect the output to /dev/null. |
| 241 | echoscip-shell system . . echo. "spawn ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $specified_user@$IP $@; expect password:; send $specified_passwd\r; expect eof" | expect -f - &>/dev/nullscip-shell filesystem . . `/dev/null`. & |
| 242 | else |
| 243 | echoscip-shell system . . 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 [scip-shell system . . `[`. -n "$(echoscip-shell system . . echo. $@ | grepscip-shell system . . grep. -E "(reboot|poweroff)")" ]; then |
| 247 | # If command includes reboot or shutdown, redirect the output to /dev/null. |
| 248 | sshscip-shell system . . ssh. -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/nullscip-shell filesystem . . `/dev/null`. $specified_user@$IP $@ &>/dev/nullscip-shell filesystem . . `/dev/null`. & |
| 249 | else |
| 250 | sshscip-shell system . . ssh. -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/nullscip-shell filesystem . . `/dev/null`. $specified_user@$IP $@ & |
| 251 | fi |
| 252 | fi |
| 253 | else |
| 254 | # ping before run command |
| 255 | if ping -c 2 $IP > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 256 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS |
| 257 | echoscip-shell system . . echo. "$IP is alive... Sending command now..." |
| 258 | if [scip-shell system . . `[`. -n "$specified_passwd" ]; then |
| 259 | if [scip-shell system . . `[`. -n "$(echoscip-shell system . . echo. $@ | grepscip-shell system . . grep. -E "(reboot|poweroff)")" ]; then |
| 260 | # If command includes reboot or shutdown, redirect the output to /dev/null. |
| 261 | echoscip-shell system . . echo. "spawn ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $specified_user@$IP $@; expect password:; send $specified_passwd\r; expect eof" | expect -f - &>/dev/nullscip-shell filesystem . . `/dev/null`. & |
| 262 | else |
| 263 | echoscip-shell system . . 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 [scip-shell system . . `[`. -n "$(echoscip-shell system . . echo. $@ | grepscip-shell system . . grep. -E "(reboot|poweroff)")" ]; then |
| 267 | # If command includes reboot or shutdown, redirect the output to /dev/null. |
| 268 | sshscip-shell system . . ssh. -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/nullscip-shell filesystem . . `/dev/null`. $specified_user@$IP $@ &>/dev/nullscip-shell filesystem . . `/dev/null`. & |
| 269 | else |
| 270 | sshscip-shell system . . ssh. -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/nullscip-shell filesystem . . `/dev/null`. $specified_user@$IP $@ & |
| 271 | fi |
| 272 | fi |
| 273 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 274 | else |
| 275 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 276 | echoscip-shell system . . echo. "$IP is unreachable... Abort command." |
| 277 | [scip-shell system . . `[`. "$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 |