@@ -2283,7 +2283,7 @@ _user_at_host()
22832283 _comp_initialize -n : -- " $@ " || return
22842284
22852285 if [[ $cur == * @* ]]; then
2286- _known_hosts_real " $cur "
2286+ _comp_compgen_known_hosts " $cur "
22872287 else
22882288 _comp_compgen -- -u -S @
22892289 compopt -o nospace
@@ -2292,20 +2292,21 @@ _user_at_host()
22922292shopt -u hostcomplete && complete -F _user_at_host talk ytalk finger
22932293
22942294# NOTE: Using this function as a helper function is deprecated. Use
2295- # `_known_hosts_real ' instead.
2295+ # `_comp_compgen_known_hosts ' instead.
22962296# TODO:API: rename per conventions
22972297_known_hosts ()
22982298{
22992299 local cur prev words cword comp_args
23002300 _comp_initialize -n : -- " $@ " || return
23012301
2302- # NOTE: Using `_known_hosts' as a helper function and passing options
2303- # to `_known_hosts' is deprecated: Use `_known_hosts_real' instead.
2302+ # NOTE: Using `_known_hosts' as a helper function and passing options to
2303+ # `_known_hosts' is deprecated: Use `_comp_compgen_known_hosts'
2304+ # instead.
23042305 local -a options=()
23052306 [[ ${1-} == -a || ${2-} == -a ]] && options+=(-a)
23062307 [[ ${1-} == -c || ${2-} == -c ]] && options+=(-c)
23072308 local IFS=$' \t\n ' # Workaround for connected ${v+"$@"} in bash < 4.4
2308- _known_hosts_real ${options[@]+" ${options[@]} " } -- " $cur "
2309+ _comp_compgen_known_hosts ${options[@]+" ${options[@]} " } -- " $cur "
23092310} # _known_hosts()
23102311
23112312# Helper function to locate ssh included files in configs
@@ -2369,7 +2370,7 @@ _included_ssh_config_files()
23692370# BASH_COMPLETION_KNOWN_HOSTS_WITH_AVAHI is set to a non-empty value.
23702371# Also hosts from HOSTFILE (compgen -A hostname) are added, unless
23712372# BASH_COMPLETION_KNOWN_HOSTS_WITH_HOSTFILE is set to an empty value.
2372- # Usage: _known_hosts_real [OPTIONS] CWORD
2373+ # Usage: _comp_compgen_known_hosts [OPTIONS] CWORD
23732374# Options:
23742375# -a Use aliases from ssh config files
23752376# -c Use `:' suffix
@@ -2378,9 +2379,17 @@ _included_ssh_config_files()
23782379# -4 Filter IPv6 addresses from results
23792380# -6 Filter IPv4 addresses from results
23802381# @return Completions, starting with CWORD, are added to COMPREPLY[]
2381- # TODO:API: rename per conventions
2382- _known_hosts_real ()
2382+ # @since 2.12
2383+ _comp_compgen_known_hosts ()
2384+ {
2385+ local known_hosts
2386+ _comp_compgen_known_hosts__impl " $@ " || return " $? "
2387+ _comp_compgen -U known_hosts set " ${known_hosts[@]} "
2388+ }
2389+ _comp_compgen_known_hosts__impl ()
23832390{
2391+ known_hosts=()
2392+
23842393 local configfile=" " flag prefix=" "
23852394 local cur suffix=" " aliases=" " i host ipv4=" " ipv6=" "
23862395 local -a kh tmpkh=() khd=() config=()
@@ -2483,8 +2492,6 @@ _known_hosts_real()
24832492
24842493 # If we have known_hosts files to use
24852494 if (( ${# kh[@]} + ${# khd[@]} > 0 )) ; then
2486- local -a tmp=()
2487-
24882495 if (( ${# kh[@]} > 0 )) ; then
24892496 # https://man.openbsd.org/sshd.8#SSH_KNOWN_HOSTS_FILE_FORMAT
24902497 for i in " ${kh[@]} " ; do
@@ -2506,7 +2513,7 @@ _known_hosts_real()
25062513 # Remove trailing ] + optional :port
25072514 host=${host% ]?(: +([0-9]))}
25082515 # Add host to candidates
2509- [[ $host ]] && tmp +=(" $host " )
2516+ [[ $host ]] && known_hosts +=(" $host " )
25102517 done
25112518 fi
25122519 done < " $i "
@@ -2521,21 +2528,21 @@ _known_hosts_real()
25212528 if [[ $i == * key_22_* .pub && -r $i ]]; then
25222529 host=${i/#* key_22_/ }
25232530 host=${host/% .pub/ }
2524- [[ $host ]] && tmp +=(" $host " )
2531+ [[ $host ]] && known_hosts +=(" $host " )
25252532 fi
25262533 done
25272534 fi
25282535
25292536 # apply suffix and prefix
2530- (( ${# tmp [@]} )) &&
2531- _comp_compgen -- -W ' "${tmp [@]}"' -P " $prefix " -S " $suffix "
2537+ (( ${# known_hosts [@]} )) &&
2538+ _comp_compgen -v known_hosts -- -W ' "${known_hosts [@]}"' -P " $prefix " -S " $suffix "
25322539 fi
25332540
25342541 # append any available aliases from ssh config files
25352542 if [[ ${# config[@]} -gt 0 && $aliases ]]; then
25362543 local -a hosts
25372544 if _comp_split hosts " $( command sed -ne ' s/^[[:blank:]]*[Hh][Oo][Ss][Tt][[:blank:]=]\{1,\}\(.*\)$/\1/p' " ${config[@]} " ) " ; then
2538- _comp_compgen -a -- -P " $prefix " \
2545+ _comp_compgen -av known_hosts -- -P " $prefix " \
25392546 -S " $suffix " -W ' "${hosts[@]%%[*?%]*}"' -X ' @(\!*|)'
25402547 fi
25412548 fi
@@ -2548,38 +2555,37 @@ _known_hosts_real()
25482555 local generated=$( avahi-browse -cprak 2> /dev/null | awk -F' ;' \
25492556 ' /^=/ && $5 ~ /^_(ssh|workstation)\._tcp$/ { print $7 }' |
25502557 sort -u)
2551- _comp_compgen -a -- -P " $prefix " -S " $suffix " -W ' $generated'
2558+ _comp_compgen -av known_hosts -- -P " $prefix " -S " $suffix " -W ' $generated'
25522559 fi
25532560
25542561 # Add hosts reported by ruptime.
25552562 if type ruptime & > /dev/null; then
25562563 local generated=$( ruptime 2> /dev/null | awk ' !/^ruptime:/ { print $1 }' )
2557- _comp_compgen -a -- -W ' $generated'
2564+ _comp_compgen -av known_hosts -- -W ' $generated'
25582565 fi
25592566
25602567 # Add results of normal hostname completion, unless
25612568 # `BASH_COMPLETION_KNOWN_HOSTS_WITH_HOSTFILE' is set to an empty value.
25622569 if [[ ${BASH_COMPLETION_KNOWN_HOSTS_WITH_HOSTFILE-${COMP_KNOWN_HOSTS_WITH_HOSTFILE-1} } ]]; then
2563- _comp_compgen -a -- -A hostname -P " $prefix " -S " $suffix "
2570+ _comp_compgen -av known_hosts -- -A hostname -P " $prefix " -S " $suffix "
25642571 fi
25652572
2566- if (( ${# COMPREPLY [@]} )) ; then
2573+ if (( ${# known_hosts [@]} )) ; then
25672574 if [[ $ipv4 ]]; then
2568- COMPREPLY =(" ${COMPREPLY [@]/*:* $suffix / } " )
2575+ known_hosts =(" ${known_hosts [@]/*:* $suffix / } " )
25692576 fi
25702577 if [[ $ipv6 ]]; then
2571- COMPREPLY =(" ${COMPREPLY [@]/ +([0-9]).+([0-9]).+([0-9]).+([0-9])$suffix / } " )
2578+ known_hosts =(" ${known_hosts [@]/ +([0-9]).+([0-9]).+([0-9]).+([0-9])$suffix / } " )
25722579 fi
25732580 if [[ $ipv4 || $ipv6 ]]; then
2574- for i in " ${! COMPREPLY [@]} " ; do
2575- [[ ${COMPREPLY [i]} ]] || unset -v ' COMPREPLY [i]'
2581+ for i in " ${! known_hosts [@]} " ; do
2582+ [[ ${known_hosts [i]} ]] || unset -v ' known_hosts [i]'
25762583 done
25772584 fi
2585+ _comp_compgen -v known_hosts -c " $prefix$cur " ltrim_colon " ${known_hosts[@]} "
25782586 fi
25792587
2580- _comp_ltrim_colon_completions " $prefix$cur "
2581-
2582- } # _known_hosts_real()
2588+ } # _comp_compgen_known_hosts__impl()
25832589complete -F _known_hosts traceroute traceroute6 \
25842590 fping fping6 telnet rsh rlogin ftp dig drill mtr ssh-installkeys showmount
25852591
0 commit comments