[Login]

scripts/copy to each host

#!/bin/bash
# Source: Ari Polsky

function usage() {
cat << EOF
usage: $0 -f <host_file> -c <file_to_copy> [OPTIONS]

OPTIONS:
-a              asynchronous mode
-d DIR          destination directory (defaults to /tmp/)
-m CONCURRENCY  maximum number of concurrent asynchronous operations
EOF
}

function num_jobs() {
    NUM_JOBS=$(jobs | grep Running | wc -l)
    if [ "$NUM_JOBS" == "" ]; then
        NUM_JOBS=0
    fi
}

BATCH_SIZE=20
DELAY=5
POLLING_INTERVAL=1

while getopts "hs:b:af:d:c:m:" opt; do
  case $opt in
    h)
        usage
        exit 0
        ;;
    f)
        FILE=$OPTARG
        ;;
    d)
        DEST=$OPTARG
        ;;
    a)
        ASYNC=1
        ;;
    c)
        FILE_TO_CP=$OPTARG
        ;;
    s)
        DELAY=$OPTARG
        ;;
    b)
        BATCH_SIZE=$OPTARG
        ;;
    m)
        MAX_CONCURRENCY=$OPTARG
        ;;
    \?)
        echo "Invalid option: -$OPTARG" >&2
        usage
        exit 1
        ;;
    :)
        echo "Option -$OPTARG requires an argument." >&2
        exit 1
        ;;
  esac
done

if [ "$FILE" == "" ]; then
    echo missing required -f \<filename\> option
    usage
    exit 1
fi

if [ "$FILE_TO_CP" == "" ]; then
    echo missing required -c \<filename\> option
    usage
    exit 1
fi

if [ "$ASYNC" == "1" ] && [ "$OUTDIR" == "" ]; then
    OUTDIR=_for_each_host_output
fi

if [ "$OUTDIR" != "" ]; then
    mkdir -p $OUTDIR
fi

if [ "$DEST" == "" ]; then
    DEST=/tmp/
fi

copy_file(){
    local L_MAX_RETRIES=4
    local L_RETRIES=0
    local L_SCP_RETVAL=1
    local L_FILE_TO_CP=$1
    local L_HOST=$2
    local L_DEST=$3

    while [ $L_RETRIES -lt $L_MAX_RETRIES ] && [ "$L_SCP_RETVAL" != "0" ]; do
        scp -o PasswordAuthentication=no -o BatchMode=yes -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no 2>/dev/null -o ConnectTimeout=10 $L_FILE_TO_CP $L_HOST:$L_DEST </dev/null > /dev/null
        L_SCP_RETVAL=$?
        L_RETRIES=$((L_RETRIES+1))
#        echo result: $L_SCP_RETVAL >> $L_OUTDIR/$L_HOST
        echo result: $L_SCP_RETVAL
    done
}

COUNT=0
while read HOST; do
    if [ "$ASYNC" == "1" ]; then
        echo executing for host $HOST
        #scp -o PasswordAuthentication=no -o BatchMode=yes -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no 2>/dev/null -o ConnectTimeout=3 $FILE_TO_CP $HOST:$DEST </dev/null > $OUTDIR/$HOST &
        copy_file $FILE_TO_CP $HOST $DEST > $OUTDIR/$HOST &
        if [ "$MAX_CONCURRENCY" != "" ]; then
            num_jobs
            while [ "$NUM_JOBS" -ge "$MAX_CONCURRENCY" ]; do
                sleep $POLLING_INTERVAL
                num_jobs
            done
        else
            COUNT=$((COUNT+1))
            if [ $COUNT == $BATCH_SIZE ]; then
                COUNT=0
                sleep $DELAY
            fi
        fi
    else
        OUTPUT=$(copy_file $FILE_TO_CP $HOST $DEST)
        if [ "$OUTDIR" == "" ]; then
            echo "$HOST ";
            echo "--------------------------------";
            echo "$OUTPUT"; echo "";
        else
            echo "$OUTPUT" > $OUTDIR/$HOST
        fi
    fi
done<$FILE