You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.0 KiB

#!/usr/bin/env bash
# début des fonctions
synopsis() {
cat << EOF
Usage : `basename $0` [-h] [-i [identity_file]] [login@]system
EOF
}
help() {
cat << EOF
`basename $0` copie votre clé publique dans un système virtuel.
`synopsis`
-h : affiche cette aide
-i : indique le fichier contenant la clé publique à copier
EOF
}
usage() {
synopsis
exit 1
}
args() {
local opt
while getopts "hi:" opt; do
case $opt in
h) help; exit 0;;
i) IDENTITY="$OPTARG";;
?) usage;;
esac
done
shift $(($OPTIND - 1))
[ $# -ne 1 ] && usage
LAST="$1"
}
# Programme principal
VDN_PATH=$(readlink -f $(dirname $0)/..); . $VDN_PATH/bin/functions.sh
IDENTITY=""
args $@
LOGIN="$USER@"
GUEST_NAME=$LAST
if echo $LAST | grep -q '@'; then
LOGIN=`echo $LAST | cut -d '@' -f 1`@
GUEST_NAME=`echo $LAST | cut -d '@' -f 2`
fi
if echo $GUEST_NAME | grep -q '/'; then
error "$GUEST_NAME est un nom de système invalide"
fi
[ $GUEST_NAME != nested ] && setGuestVars $GUEST_NAME
GUEST_OWNER=$USER
[ $GUEST_NAME != nested ] && loadGuestVars $GUEST_NAME
if [ $GUEST_NAME = nested ]; then
SSH_REDIR_PORT=2222
SSH_GUEST_MASTER=0
else
[ ! -e $TMPDIR/vdn-$GUEST_NAME-$GUEST_OWNER-redirs ] && {
echo "Le fichier $TMPDIR/vdn-$GUEST_NAME-$GUEST_OWNER-redirs n'existe pas !" >&2
echo "Le système $GUEST_NAME est-il démarré ?" >&2
exit 1
}
SSH_REDIR_PORT=`cat $TMPDIR/vdn-$GUEST_NAME-$GUEST_OWNER-redirs | \
sed -rne 's/^tcp:([0-9]+):22(:|$).*$/\1/p'`
fi
if [ -z "$SSH_REDIR_PORT" ]; then
echo "Aucune redirection vers le port 22 du système $GUEST_NAME !" >&2
exit 1
fi
opts="-p $SSH_REDIR_PORT -o NoHostAuthenticationForLocalhost=yes"
[ -n "$IDENTITY" ] && opts="-i $IDENTITY $opts"
RUSER=$USER
RUSER=$(echo $@ | sed -rne 's/^.*(^|[[:space:]])(.*)@.*$/\2/p')
[ -z "$RUSER" ] && RUSER=$USER
name=$(echo $@ | sed -re 's/^.*@(.*)$/\1/')
args=$(echo $LAST | sed -re "s/$name/localhost/g")
ssh-copy-id $opts $args
[ $? -eq 0 ] && {
echo -e "Try : \n\nvdn-ssh $RUSER@$name\n"
}