loading...

re: Copy file to multi destinations at once - Linux Tips VIEW POST

FULL DISCUSSION
 

Here's a variant of the same theme: distribute.sh

#!/bin/bash

[ $# -eq 0 ] \
    && {
        echo "${CYAN} Usage ${WHITE} $(basename $0) /path/to/file ${YELLOW}[/destination/directory]${RESTORE}"
        exit
    }

[ -f "$1" -a ! -d "$1" ] \
&& {
    SCP="scp"
    FILE="$(realpath $1)"
    shift
} \
|| {
    [ -d "$1" ] \
    && {
        RECUR="recursive"
        SCP="scp -r"
        FILE="$(realpath $1)"
        shift
    } \
    || {
        echo "${RED} Error: ${WHITE} Unknown file or directory.${RESTORE}"
        exit 1
    }
}

[ $# -gt 0 ] \
    && {
        DESTDIR="$(realpath $1)/"
    } \
    || {
        DESTDIR="$(dirname $(realpath ${FILE}))/"
    }

echo -e "${WHITE}Copying ${RECUR}\nFrom: ${YELLOW}${FILE}\n${WHITE}To: ${YELLOW}${DESTDIR}${RESTORE}"


for DEST in $(cat ~/.ssh/authorized_keys | cut -f 3 -d ' ' | tr -d ' ' )
do
    DESTUSER=$( echo ${DEST} | cut -f 1 -d '@' | tr -d '[:space:]' )
    DESTHOST=$( echo ${DEST} | cut -f 2 -d '@' | tr -d '[:space:]' )

    echo -n "${CYAN}===>${WHITE} ${DESTUSER}${YELLOW} @${WHITE} ${DESTHOST}"

    if ping -c 1 -w 2 -nq ${DESTHOST} > /dev/null 2>&1
    then
        echo " ${GREEN}OK${RESTORE}"
    else
        echo " ${RED}NOT OK${RESTORE}"
        continue
    fi

    echo "${CYAN} Copy ${WHITE}${FILE} to ${DESTDIR}"
    $SCP ${FILE} ${DESTHOST}:${DESTDIR}

done
Code of Conduct Report abuse