tenir compte de la configuration de ssh_config pour le choix du user de connexion

This commit is contained in:
Jephté Clain 2014-06-24 09:32:06 +04:00
parent f2f6bada9e
commit 32d5a83fa6
1 changed files with 59 additions and 10 deletions

69
ussh
View File

@ -184,16 +184,60 @@ function remove_keys() {
eend
}
function __have_ssh_config() {
local host="$1" config="$2"
[ -f "$config" ] || return 1
awkrun host="$host" '
BEGIN {
in_host = 0
ec = 1
}
tolower($1) == "host" {
in_host = 0
for (i = 2; i <= NF; i++) {
if ($i == host) {
in_host = 1
break
}
}
}
in_host && tolower($1) == "user" {
ec = 0
exit 0
}
END {
exit ec
}
' -- "$config"
}
function __update_sshopts_l() {
# Ajouter l'option -l USSH_USER au tableau sshopts si aucune définition pour
# l'hôte $1 n'existe dans l'un des fichiers de configuration de ssh
if [ -n "$1" ]; then
__have_ssh_config "$1" ~/.ssh/config && return
__have_ssh_config "$1" /etc/ssh/ssh_config && return
fi
# Rajouter l'option au début pour que l'utilisateur puisse la surcharger
sshopts=(${USSH_USER:+-l "$USSH_USER"} "${sshopts[@]}")
}
function show_vars() {
[ "${#hosts[*]}" -gt 1 ] && exec=
local -a sshopts
array_copy sshopts SSHOPTS
__update_sshopts_l "${hosts[0]}"
set_var_cmd ssh "$SSH"
set_var_cmd exec "$exec"
set_array_cmd options SSHOPTS
set_array_cmd options sshopts
set_array_cmd hosts
set_array_cmd args @ "$@"
}
function do_ssh() {
local -a sshopts
local onehost r
if [ "${#hosts[*]}" -gt 1 ]; then
onehost=
@ -203,15 +247,20 @@ function do_ssh() {
fi
for host in "${hosts[@]}"; do
[ -z "$onehost" ] && etitle "$host"
${exec:+exec} "$SSH" "${SSHOPTS[@]}" "$host" "$@" || r=$?
array_copy sshopts SSHOPTS
__update_sshopts_l "$host"
${exec:+exec} "$SSH" "${sshopts[@]}" "$host" "$@" || r=$?
[ -z "$onehost" ] && eend
done
return "${r:-0}"
}
function __update_SSHOPTS() { SSHOPTS=("${SSHOPTS[@]}" "$@"); }
# charger USSH_DOMAIN et USSH_USER
set_defaults nutools
SSHOPTS=(${USSH_USER:+-l "$USSH_USER"}) # options de ssh
function sshopt() { SSHOPTS=("${SSHOPTS[@]}" "$@"); }
DOMAIN="$USSH_DOMAIN"
SSH=
@ -221,12 +270,12 @@ exec=1
parse=
parse_opts "${PRETTYOPTS[@]}" \
--help '$exit_with display_help' \
-1,-2,-4,-6,-A,-a,-C,-f,-g,-K,-k,-M,-N,-n,-q,-s,-T,-t,-V,-v,-X,-x,-Y,-y '$sshopt "$option_"' \
-b:,-c:,-D:,-e:,-F:,-I:,-i:,-L:,-l:,-m:,-O:,-o:,-p:,-R:,-S:,-W:,-w: '$sshopt "$option_" "$value_"' \
--quiet '$sshopt -q' \
--tty '$sshopt -t' \
--login: '$sshopt -l "$value_"' \
--port: '$sshopt -p "$value_"' \
-1,-2,-4,-6,-A,-a,-C,-f,-g,-K,-k,-M,-N,-n,-q,-s,-T,-t,-V,-v,-X,-x,-Y,-y '$__update_SSHOPTS "$option_"' \
-b:,-c:,-D:,-e:,-F:,-I:,-i:,-L:,-l:,-m:,-O:,-o:,-p:,-R:,-S:,-W:,-w: '$__update_SSHOPTS "$option_" "$value_"' \
--quiet '$__update_SSHOPTS -q' \
--tty '$__update_SSHOPTS -t' \
--login: '$__update_SSHOPTS -l "$value_"' \
--port: '$__update_SSHOPTS -p "$value_"' \
-d:,--domain: DOMAIN= \
-z:,--ssh: SSH= \
-r,--remove '$remove=1; parse=' \