bitreich-drist

fork of bitreich.org/drist
git clone git://git.pyratebeard.net/bitreich-drist.git
Log | Files | Refs | README | LICENSE

drist (2966B)


      1 #!/bin/sh
      2 
      3 SUDO=0
      4 SUDO_BIN=sudo
      5 EXEC=""
      6 SSHONCE=0
      7 TRUNCATE=0
      8 TEMPDIR=""
      9 USETAG=0
     10 TAG=""
     11 
     12 usage() {
     13 	echo "$0 [-p] [-d] [-s [-e sudo|doas]] [-P tag] server [...]"
     14 	exit 0
     15 }
     16 
     17 # $1 = directory name
     18 # $2 = remote server
     19 # $3 = tempdir
     20 copy_files() {
     21 	# -l = keep symlink / -D = special device
     22 	if [ -d "${1}" ]
     23 	then
     24 		LIST=$(mktemp /tmp/drist-rsync.XXXXXXXXXX)
     25 		if [ -f "$LIST" ]
     26 		then
     27 			printf 'Copying files from "%s" to temporary directory %s:\n' "$1" "$3"
     28 			find "${1}"/ -type f -or -type l | cut -d '/' -f 2- | tee "${LIST}" | sed 's/^/	\//'
     29 			rsync -t -e "ssh $SSH_PARAMS" -lD --files-from="${LIST}" "${1}/" "${2}":"/${3}"
     30 			rm "$LIST"
     31 		fi
     32 	fi
     33 }
     34 
     35 # $1 = script filename
     36 # $2 = remote server
     37 # $3 = tempdir
     38 remote_script() {
     39 	if [ -f "${1}" ]
     40 	then
     41 		printf 'Executing file "%s":\n' "$1"
     42 		ssh $SSH_PARAMS "${2}" "cd ${3} && DRIST=${3}/script &&
     43 		    cat - > \$DRIST &&
     44 		    chmod u+x \$DRIST &&
     45 		    ${EXEC} \$DRIST" < "$1"
     46 	fi
     47 }
     48 
     49 # $1 = remote server
     50 create_temp() {
     51     TEMPDIR=$(ssh $SSH_PARAMS "$1" "mktemp -d ~/.drist_files_XXXXXXXXXXXXXXX")
     52     if [ "$TEMPDIR" = "" ]; then
     53         echo "mktemp error, aborting"
     54         exit 1
     55     fi
     56 }
     57 
     58 # $1 = remote server
     59 # $2 = temporary directory
     60 delete_temp() {
     61     if echo "${2}" | grep drist_files_ >/dev/null ; then
     62         ssh $SSH_PARAMS "$1" "rm -fr ${2}"
     63     else
     64         echo "Problem, TEMPDIR was reset during execution, current value is = $2"
     65 	exit 2
     66     fi
     67 }
     68 
     69 
     70 # RUNTIME BEGINS HERE
     71 while getopts pndse:P: arg; do
     72 	case ${arg} in
     73 		d) TRUNCATE=1 ;;
     74 		s) SUDO=1 ;;
     75 		e) SUDO_BIN="${OPTARG}" ;;
     76 		p) SSHONCE=1 ;;
     77 		P) USETAG=1 ; TAG="${OPTARG}" ;;
     78 		*) usage ;;
     79 	esac
     80 done
     81 shift $((OPTIND - 1))
     82 
     83 # allow to use a privilege escalation program
     84 if [ "$SUDO" -eq 1 ]
     85 then
     86 	EXEC="$SUDO_BIN"
     87 fi
     88 
     89 # use ControlMaster to make connections persistent
     90 if [ "$SSHONCE" -eq 1 ]
     91 then
     92 	SSH_PARAMS=-o"ControlMaster=auto"" "-o"ControlPath=/tmp/drist_ssh_%h_%p_%r.sock"" "-o"ControlPersist=1m"
     93 fi
     94 
     95 if [ "$USETAG" -eq 1 ]
     96 then
     97 	SSH_PARAMS="${SSH_PARAMS}"" "-P"${TAG}"
     98 fi
     99 
    100 # start looping over server list
    101 if [ -f "$1" ]
    102 then
    103 	SERVER_LIST="$(tr '\n' ' ' < $1)"
    104 else
    105 	SERVER_LIST="$@"
    106 fi
    107 
    108 if [ "${SERVER_LIST}" = "" ]
    109 then
    110 	echo "No server specified"
    111 	exit 1
    112 fi
    113 
    114 for remote_server in ${SERVER_LIST}
    115 do
    116 	echo "Running on ${remote_server}"
    117 
    118 	# check if host exists
    119 	HOSTNAME=$(ssh $SSH_PARAMS "${remote_server}" "uname -n")
    120 	if [ "$?" -ne 0 ]; then
    121 		echo "Error while ssh ${remote_server}"
    122 		continue
    123 	fi
    124 
    125 	if [ "$TRUNCATE" -eq 1 ]; then
    126 		HOSTNAME="${HOSTNAME%%.*}"
    127 	fi
    128 
    129 	create_temp "${remote_server}"
    130 	copy_files "files" "${remote_server}" "$TEMPDIR"
    131 	copy_files "files-${HOSTNAME}" "${remote_server}" "$TEMPDIR"
    132 	remote_script "script" "${remote_server}" "$TEMPDIR"
    133 	remote_script "script-${HOSTNAME}" "${remote_server}" "$TEMPDIR"
    134 	delete_temp "${remote_server}" "$TEMPDIR"
    135 
    136 	# close socket if persistance is actived
    137 	if [ "$SSHONCE" -eq 1 ]
    138 	then
    139 		ssh $SSH_PARAMS -O exit -N "$1"
    140 	fi
    141 
    142 	unset TEMPDIR HOSTNAME
    143 done