#!/usr/bin/env bash set -eu synopsis() { cat << EOF Usage : `basename $0` [-h] [option...] src dst EOF } help() { cat << EOF `basename $0` effectue un scp à partir ou à destination d'un système virtuel. `synopsis` -h : affiche cette aide Les options autorisées sont celles de scp(1). Exemple : # Copie de l'hôte vers la machine virtuelle # /etc/passwd de l'hôte vers /tmp de la machine virtuelle vdn-scp /etc/passwd test@vm-1:/tmp # Copie de la machine virtuelle vers l'hôte # /etc/passwd de la machine virtuelle vers /tmp de l'hôte vdn-scp test@vm-1:/etc/passwd /tmp EOF } usage() { synopsis exit 1 } args() { opts="" [ $# -eq 1 -a "$1" = "-h" ] && { help; exit 0; } while [ $# -gt 2 ]; do if [ "$1" = "-h" ]; then help; exit 0; fi opts="$opts $1" shift done LOGIN="${USER}@" GUEST_NAME="" for i in 1 2; do reg=':' if [[ "$1" =~ "$reg" ]]; then user=`echo $1 | cut -d ':' -f 1` path=`echo $1 | cut -d ':' -f 2` LOGIN="${USER}@" GUEST_NAME="$user" reg="@" if [[ "$user" =~ "$reg" ]]; then LOGIN=`echo $user | cut -d '@' -f 1`@ GUEST_NAME=`echo $user | cut -d '@' -f 2` fi path="${LOGIN}localhost:$path" else path="$1" fi [ $i -eq 1 ] && src="$path" || dst="$path" shift done #echo "opts=$opts src=$src dst=$dst GUEST_NAME=$GUEST_NAME" } # Programme principal GUEST_OWNER=$USER VDN_PATH=$(readlink -f $(dirname $0)/..); . $VDN_PATH/bin/functions.sh args "$@" [ -z "$GUEST_NAME" ] && usage #setGuestVars $GUEST_NAME #loadGuestVars $GUEST_NAME [ ! -e $TMPDIR/vdn-$GUEST_NAME-$GUEST_OWNER-redirs ] && error "Impossible de joindre $GUEST_NAME" SSH_REDIR_PORT=`cat $TMPDIR/vdn-$GUEST_NAME-$GUEST_OWNER-redirs | \ sed -rne 's/^tcp:([0-9]+):22(:|$).*$/\1/p'` [ -z "$SSH_REDIR_PORT" ] && error "Impossible de joindre $GUEST_NAME" if [ -z "$SSH_REDIR_PORT" ]; then error "Aucune redirection vers le port 22 du système $GUEST_NAME !" exit 1 fi SSH_MASTER="" [ $SSH_GUEST_MASTER = 1 ] && { sshGuestControlMaster SSH_MASTER="-o ControlPath=$TMPDIR/vdn-$GUEST_NAME-$GUEST_OWNER-guest-%r@%h:%p" } scp $SSH_MASTER $SCP_OPTS $opts -P $SSH_REDIR_PORT $src $dst