From b3ff42ea8e01028b30d25452939d80e9c2367ee1 Mon Sep 17 00:00:00 2001 From: Jephte CLAIN Date: Tue, 25 Feb 2014 07:45:43 +0400 Subject: [PATCH] ajout de USSH_USER ajout de l'option -c --- lib/default/nutools | 2 + ussh | 92 +++++++++++++++++++++++++-------------------- 2 files changed, 54 insertions(+), 40 deletions(-) diff --git a/lib/default/nutools b/lib/default/nutools index 199d9c6..2783d39 100644 --- a/lib/default/nutools +++ b/lib/default/nutools @@ -5,5 +5,7 @@ # auto (par défaut), system-or-local, default, manual ou vanilla #SHELL_TYPE=auto +# Utilisateur par défaut pour la commande ussh +#USSH_USER=root # Domaine par défaut pour la commande ussh #USSH_DOMAIN= diff --git a/ussh b/ussh index 0d60bf3..8bf9daa 100755 --- a/ussh +++ b/ussh @@ -106,29 +106,7 @@ function fix_hosts() { array_map hosts __dot_is_localhost } -action=ssh -quiet= -tty= -login= -options=() -domain="$USSH_DOMAIN" -ssh= -parse_opts + "${PRETTYOPTS[@]}" \ - --help '$exit_with display_help' \ - -q,--quiet quiet=1 \ - -t,--tty tty=1 \ - -l:,--login: login= \ - -o options \ - -d:,--domain: domain= \ - -S:,--ssh ssh= \ - -R,--remove action=remove \ - @ args -- "$@" && set -- "${args[@]}" || die "$args" - -if [ "$action" == "ssh" ]; then - [ -n "$1" ] || die "Vous devez spécifier l'hôte sur lequel faire la connexion" - hosts=("$1"); shift - fix_hosts - +function ussh() { args=(${quiet:+-q} ${tty:+-t} ${login:+-l "$login"}) for option in "${options[@]}"; do args=("${args[@]}" -o "$option") @@ -144,44 +122,78 @@ if [ "$action" == "ssh" ]; then "$ssh" "${user:+$user@}$host" "${args[@]}" [ -n "$showtitle" ] && eend done +} -elif [ "$action" == "remove" ]; then - urequire ipcalc - function remove() { - estep "$1" - ssh-keygen -R "$1" >&/dev/null - } - - [ -n "$1" ] || die "Vous devez spécifier les hôtes à supprimer de ~/.ssh/known_hosts" - hosts=("$@") - fix_hosts +function remove_key() { + estep "$1" + ssh-keygen -R "$1" >&/dev/null +} +function remove_keys() { + local -a __hosts; array_copy __hosts hosts + local -a allhosts hosts ips; local host user ip + array_copy allhosts __hosts etitle "Suppression des entrées dans ~/.ssh/known_hosts" - allhosts=("${hosts[@]}") for host in "${allhosts[@]}"; do splituserhost "$host" user host [[ "$host" == *.* ]] || host="$host${domain:+.$domain}" if ipcalc_checkip "$host" >/dev/null; then ip="$host" - remove "$ip" + remove_key "$ip" resolv_hosts hosts "$ip" for host in "${hosts[@]}"; do - remove "$host" + remove_key "$host" hostname="${host%%.*}" - [ "$hostname" != "$host" ] && remove "$hostname" + [ "$hostname" != "$host" ] && remove_key "$hostname" done else - remove "$host" + remove_key "$host" hostname="${host%%.*}" - [ "$hostname" != "$host" ] && remove "$hostname" + [ "$hostname" != "$host" ] && remove_key "$hostname" resolv_ips ips "$host" for ip in "${ips[@]}"; do - remove "$ip" + remove_key "$ip" done fi done eend +} + +action=ssh +quiet= +tty= +login="$USSH_USER" +options=() +domain="$USSH_DOMAIN" +ssh= +connect_after_remove= +parse_opts + "${PRETTYOPTS[@]}" \ + --help '$exit_with display_help' \ + -q,--quiet quiet=1 \ + -t,--tty tty=1 \ + -l:,--login: login= \ + -o options \ + -d:,--domain: domain= \ + -S:,--ssh ssh= \ + -R,--remove action=remove \ + -c,--connect connect_after_remove=1 \ + @ args -- "$@" && set -- "${args[@]}" || die "$args" + +if [ "$action" == "ssh" ]; then + [ -n "$1" ] || die "Vous devez spécifier l'hôte sur lequel faire la connexion" + hosts=("$1"); shift + fix_hosts + ussh "$@" + +elif [ "$action" == "remove" ]; then + urequire ipcalc + + [ -n "$1" ] || die "Vous devez spécifier les hôtes à supprimer de ~/.ssh/known_hosts" + hosts=("$@") + fix_hosts + remove_keys + [ -n "$connect_after_remove" ] && ussh fi