mirror of
https://github.com/fish-shell/fish-shell
synced 2025-01-12 21:18:53 +00:00
numerous improvements to ssh/scp completions
Add IPV6 /etc/hosts completion support. Parses columns rather than values which produces improved output. Support ssh -F and Include completion Ignore ssh Hostname and Host with wildcard. The following only get in the way: - Hostname: Host resolves to Hostname - Wildcard Host: Cannot ssh to a glob pattern Improve scp completions * complete only local files when no host provided * complete only remote files when host is provided * complete local files or hosts when no separator Disable username completion for ssh/scp Username completion only provides local users which will unlikely be useful on a remote machine. ssh will use the current username (the only useful one) or one provided in the ssh config.
This commit is contained in:
parent
949fc7bdd7
commit
eff6b98813
3 changed files with 110 additions and 19 deletions
|
@ -8,7 +8,14 @@ __fish_complete_ssh scp
|
|||
# scp specific completions
|
||||
#
|
||||
|
||||
complete -c scp -d Hostname -a "
|
||||
#
|
||||
# Hostname
|
||||
#
|
||||
complete \
|
||||
--command scp \
|
||||
--description Hostname \
|
||||
--condition "commandline --cut-at-cursor --current-token | string match --invert '*:*'" \
|
||||
--arguments "
|
||||
|
||||
(__fish_print_hostnames):
|
||||
|
||||
|
@ -17,14 +24,28 @@ complete -c scp -d Hostname -a "
|
|||
commandline -ct |sed -ne 's/\(.*@\).*/\1/p'
|
||||
)(__fish_print_hostnames):
|
||||
|
||||
(__fish_print_users)@\tUsername
|
||||
# Disable as username completion is not very useful
|
||||
# (__fish_print_users)@\tUsername
|
||||
|
||||
"
|
||||
|
||||
#
|
||||
# Local path
|
||||
#
|
||||
complete \
|
||||
--command scp \
|
||||
--description "Local Path" \
|
||||
--condition "commandline -ct | string match ':'"
|
||||
|
||||
#
|
||||
# Remote path
|
||||
#
|
||||
complete -c scp -d "Remote Path" -n "commandline -ct| __fish_sgrep -o '.*:'" -a "
|
||||
complete \
|
||||
--command scp \
|
||||
--description "Remote Path" \
|
||||
--no-files \
|
||||
--condition "commandline --cut-at-cursor --current-token | string match --regex '.+:'" \
|
||||
--arguments "
|
||||
|
||||
(
|
||||
#Prepend any user@host information supplied before the remote completion
|
||||
|
@ -43,4 +64,3 @@ complete -c scp -s p --description "Preserves modification times, access times,
|
|||
complete -c scp -s q --description "Do not display progress bar"
|
||||
complete -c scp -s r --description "Recursively copy"
|
||||
complete -c scp -s S --description "Encryption program"
|
||||
|
||||
|
|
|
@ -14,9 +14,10 @@ complete -x -c ssh -d Hostname -a "
|
|||
)(__fish_print_hostnames)
|
||||
"
|
||||
|
||||
complete -x -c ssh -d User -a "
|
||||
(__fish_print_users | string match -r -v '^_')@
|
||||
"
|
||||
# Disable as username completion is not very useful
|
||||
# complete -x -c ssh -d User -a "
|
||||
# (__fish_print_users | string match -r -v '^_')@
|
||||
# "
|
||||
complete -c ssh --description "Command to run" -x -a '(__fish_complete_subcommand --fcs-skip=2)'
|
||||
|
||||
complete -c ssh -s a --description "Disables forwarding of the authentication agent"
|
||||
|
|
|
@ -1,16 +1,23 @@
|
|||
|
||||
function __fish_print_hostnames -d "Print a list of known hostnames"
|
||||
# HACK: This only deals with ipv4
|
||||
|
||||
# Print all hosts from /etc/hosts
|
||||
# use 'getent hosts' on OSes that support it (OpenBSD and Cygwin do not)
|
||||
if type -q getent
|
||||
and getent hosts >/dev/null 2>&1 # test if 'getent hosts' works and redirect output so errors don't print
|
||||
if type -q getent; and getent hosts >/dev/null 2>&1 # test if 'getent hosts' works and redirect output so errors don't print
|
||||
# Ignore zero ips
|
||||
getent hosts | string match -r -v '^0.0.0.0' | string replace -r '[0-9.]*\s*' '' | string split " "
|
||||
getent hosts | string match --regex --invert '^0.0.0.0' \
|
||||
# Remove left addresses column
|
||||
| string replace --regex '^\s*\S+\s+' '' \
|
||||
# Tokenize remaining hostnames and aliases columnss
|
||||
| string split ' '
|
||||
else if test -r /etc/hosts
|
||||
# Ignore commented lines and functionally empty lines. Strip comments.
|
||||
string match -r -v '^\s*0.0.0.0|^\s*#|^\s*$' </etc/hosts | string replace -ra '#.*$' '' | string replace -r '[0-9.]*\s*' '' | string trim | string replace -ra '\s+' '\n'
|
||||
# Ignore commented lines and functionally empty lines
|
||||
string match --regex --invert '^\s*0.0.0.0|^\s*#|^\s*$' </etc/hosts \
|
||||
# Strip comments
|
||||
| string replace --regex --all '#.*$' '' \
|
||||
# Remove left addresses column
|
||||
| string replace --regex '^\s*\S+\s+' '' \
|
||||
# Tokenize remaining hostnames and aliases columns
|
||||
| string trim | string replace --regex --all '\s+' ' ' | string split ' '
|
||||
end
|
||||
|
||||
# Print nfs servers from /etc/fstab
|
||||
|
@ -20,13 +27,76 @@ function __fish_print_hostnames -d "Print a list of known hostnames"
|
|||
|
||||
# Check hosts known to ssh
|
||||
set -l known_hosts ~/.ssh/known_hosts{,2} /etc/ssh/known_hosts{,2} # Yes, seriously - the default specifies both with and without "2"
|
||||
for file in /etc/ssh/ssh_config ~/.ssh/config
|
||||
# Check default ssh configs
|
||||
set -l ssh_config
|
||||
# Get alias and commandline options
|
||||
set -l ssh_command (functions ssh | string split ' ') (commandline -cpo)
|
||||
# Extract ssh config path from last -F short option
|
||||
if contains -- '-F' $ssh_command
|
||||
set -l ssh_config_path_is_next 1
|
||||
for token in $ssh_command
|
||||
if contains -- '-F' $token
|
||||
set ssh_config_path_is_next 0
|
||||
else if test $ssh_config_path_is_next -eq 0
|
||||
set ssh_config (eval "echo $token")
|
||||
set ssh_config_path_is_next 1
|
||||
end
|
||||
end
|
||||
else
|
||||
set ssh_config $ssh_config ~/.ssh/config
|
||||
end
|
||||
|
||||
# Extract ssh config paths from Include option
|
||||
function _ssh_include --argument-names ssh_config
|
||||
# Relative paths in Include directive use /etc/ssh or ~/.ssh depending on
|
||||
# system or user level config. -F will not override this behaviour
|
||||
if test $ssh_config = '/etc/ssh/ssh_config'
|
||||
set relative_path '/etc/ssh'
|
||||
else
|
||||
set relative_path $HOME/.ssh
|
||||
end
|
||||
|
||||
function _recursive --no-scope-shadowing
|
||||
set paths
|
||||
for config in $argv
|
||||
set paths $paths (cat $config ^/dev/null \
|
||||
# Keep only Include lines
|
||||
| string match --regex --ignore-case '^\s*Include\s+.+' \
|
||||
# Remove Include syntax
|
||||
| string replace --regex --ignore-case '^\s*Include\s+' '' \
|
||||
# Normalize whitespace
|
||||
| string trim | string replace --regex --all '\s+' ' ')
|
||||
end
|
||||
if test -n "$paths"
|
||||
# Expand paths which may have globbing and tokenize
|
||||
set paths (eval "echo $paths" | string split ' ')
|
||||
for path_index in (seq (count $paths))
|
||||
# Resolve relative paths
|
||||
if string match --invert '/*' $paths[$path_index] >/dev/null
|
||||
set paths[$path_index] $relative_path/$paths[$path_index]
|
||||
end
|
||||
echo $paths[$path_index]
|
||||
end
|
||||
_recursive $paths
|
||||
end
|
||||
end
|
||||
_recursive $ssh_config
|
||||
end
|
||||
set -l ssh_configs (_ssh_include /etc/ssh/ssh_config) (_ssh_include $ssh_config)
|
||||
|
||||
for file in $ssh_configs
|
||||
if test -r $file
|
||||
# Print hosts from system wide ssh configuration file
|
||||
# Note the non-capturing group to avoid printing "name"
|
||||
string match -ri '\s*Host(?:name)?(?:\s+|\s*=\s*)\w.*' <$file | string replace -ri '^\s*Host(?:name)?\s*(\S+)' '$1' | string replace -r '\s+' ' ' | string split ' '
|
||||
set known_hosts $known_hosts (string match -ri '^\s*UserKnownHostsFile|^\s*GlobalKnownHostsFile' < $file \
|
||||
| string replace -ri '.*KnownHostsFile\s*' '')
|
||||
string match --regex --ignore-case '^\s*Host\s+\S+' <$file \
|
||||
# We only want the value(s)
|
||||
| string replace --regex --ignore-case '^\s*Host\s+' '' \
|
||||
# Print one per line
|
||||
| string trim | string replace --regex '\s+' ' ' | string split ' ' \
|
||||
# Ignore hosts with a glob
|
||||
| string match --invert '*\**'
|
||||
# Extract known_host paths
|
||||
set known_hosts $known_hosts (string match -ri '^\s*UserKnownHostsFile|^\s*GlobalKnownHostsFile' <$file \
|
||||
| string replace -ri '.*KnownHostsFile\s*' '')
|
||||
end
|
||||
end
|
||||
for file in $known_hosts
|
||||
|
|
Loading…
Reference in a new issue