2006-02-12 21:14:21 +08:00
|
|
|
function __fish_print_hostnames -d "Print a list of known hostnames"
|
2017-02-02 14:49:40 +08:00
|
|
|
# Print all hosts from /etc/hosts. Use 'getent hosts' on OSes that support it
|
|
|
|
# (OpenBSD and Cygwin do not).
|
|
|
|
#
|
|
|
|
# Test if 'getent hosts' works and redirect output so errors don't print.
|
|
|
|
if type -q getent
|
|
|
|
and getent hosts >/dev/null 2>&1
|
|
|
|
# Ignore zero IPs.
|
|
|
|
getent hosts | string match -r -v '^0.0.0.0' | string replace -r '^\s*\S+\s+' '' | string split ' '
|
2016-11-28 13:27:22 +08:00
|
|
|
else if test -r /etc/hosts
|
2017-02-02 14:49:40 +08:00
|
|
|
# Ignore commented lines and functionally empty lines.
|
|
|
|
string match -r -v '^\s*0.0.0.0|^\s*#|^\s*$' </etc/hosts | string replace -r -a '#.*$' '' | string replace -r '^\s*\S+\s+' '' | string trim | string replace -r -a '\s+' ' ' | string split ' '
|
2016-12-04 12:36:47 +08:00
|
|
|
end
|
2016-02-27 21:33:03 +08:00
|
|
|
|
2016-12-04 12:36:47 +08:00
|
|
|
# Print nfs servers from /etc/fstab
|
|
|
|
if test -r /etc/fstab
|
|
|
|
string match -r '^\s*[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3]:|^[a-zA-Z\.]*:' </etc/fstab | string replace -r ':.*' ''
|
|
|
|
end
|
2006-02-12 21:14:21 +08:00
|
|
|
|
2017-07-18 12:13:21 +08:00
|
|
|
# Check hosts known to ssh.
|
|
|
|
# Yes, seriously - the default specifies both with and without "2".
|
2018-03-06 22:27:34 +08:00
|
|
|
# Termux puts these in the android data directory if not rooted.
|
2018-03-07 00:55:25 +08:00
|
|
|
# The directory is available as $PREFIX/etc, but that variable name is so generic that
|
|
|
|
# it would cause false-positives.
|
2018-03-06 22:27:34 +08:00
|
|
|
# Also, some people might use /usr/local/etc.
|
2018-03-07 00:55:25 +08:00
|
|
|
set -l known_hosts ~/.ssh/known_hosts{,2} {/data/data/com.termux/files/usr,/usr/local,}/etc/ssh/{,ssh_}known_hosts{,2}
|
2017-07-18 12:13:21 +08:00
|
|
|
# Check default ssh configs.
|
2017-01-26 10:03:36 +08:00
|
|
|
set -l ssh_config
|
2017-07-18 12:13:21 +08:00
|
|
|
# Get alias and commandline options.
|
|
|
|
set -l ssh_func_tokens (functions ssh | string match '*command ssh *' | string split ' ')
|
|
|
|
set -l ssh_command $ssh_func_tokens (commandline -cpo)
|
|
|
|
# Extract ssh config path from last -F short option.
|
2017-01-26 10:03:36 +08:00
|
|
|
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
|
2017-07-27 11:41:00 +08:00
|
|
|
set -l relative_path $HOME/.ssh
|
|
|
|
if string match '/etc/ssh/*' -- $ssh_config
|
2017-01-26 10:03:36 +08:00
|
|
|
set relative_path '/etc/ssh'
|
|
|
|
end
|
|
|
|
|
|
|
|
function _recursive --no-scope-shadowing
|
2017-07-27 11:41:00 +08:00
|
|
|
set -l orig_dir $PWD
|
|
|
|
set -l paths
|
2017-01-26 10:03:36 +08:00
|
|
|
for config in $argv
|
2017-09-05 22:29:57 +08:00
|
|
|
if test -r "$config"
|
|
|
|
set paths $paths (
|
|
|
|
# Keep only Include lines and remove Include syntax
|
|
|
|
string replace -rfi '^\s*Include\s+' '' <$config \
|
|
|
|
# Normalize whitespace
|
|
|
|
| string trim | string replace -r -a '\s+' ' ')
|
|
|
|
end
|
2017-01-26 10:03:36 +08:00
|
|
|
end
|
2017-06-06 15:00:40 +08:00
|
|
|
|
2017-12-13 20:13:34 +08:00
|
|
|
# Skip unusable paths.
|
|
|
|
test -d "$relative_path" -a -x "$relative_path"
|
|
|
|
or return
|
|
|
|
|
2017-07-28 05:34:19 +08:00
|
|
|
builtin cd $relative_path
|
2017-06-06 15:00:40 +08:00
|
|
|
set -l new_paths
|
|
|
|
for path in $paths
|
|
|
|
set -l expanded_path
|
2017-07-27 11:41:00 +08:00
|
|
|
eval "set expanded_path (printf \"%s\n\" $path)"
|
2017-06-06 15:00:40 +08:00
|
|
|
for path in $expanded_path
|
|
|
|
# Resolve "relative" paths in accordance to ssh path resolution
|
|
|
|
if string match -qv '/*' $path
|
|
|
|
set path $relative_path/$path
|
2017-01-26 10:03:36 +08:00
|
|
|
end
|
2017-06-06 15:00:40 +08:00
|
|
|
echo $path
|
|
|
|
set new_paths $new_paths $path
|
2017-01-26 10:03:36 +08:00
|
|
|
end
|
2017-06-06 15:00:40 +08:00
|
|
|
end
|
2017-07-28 05:34:19 +08:00
|
|
|
builtin cd $orig_dir
|
2017-06-06 15:00:40 +08:00
|
|
|
|
2017-06-17 12:00:24 +08:00
|
|
|
if test -n "$new_paths"
|
2017-06-06 15:00:40 +08:00
|
|
|
_recursive $new_paths
|
2017-01-26 10:03:36 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
_recursive $ssh_config
|
|
|
|
end
|
2017-06-06 00:37:39 +08:00
|
|
|
set -l ssh_configs /etc/ssh/ssh_config (_ssh_include /etc/ssh/ssh_config) $ssh_config (_ssh_include $ssh_config)
|
2017-01-26 10:03:36 +08:00
|
|
|
|
|
|
|
for file in $ssh_configs
|
2016-12-04 12:36:47 +08:00
|
|
|
if test -r $file
|
|
|
|
# Print hosts from system wide ssh configuration file
|
2017-09-05 22:29:57 +08:00
|
|
|
string replace -rfi '^\s*Host\s+' '' <$file | string trim | string replace -r '\s+' ' ' | string split ' ' | string match -v '*\**'
|
2017-02-02 14:49:40 +08:00
|
|
|
# Extract known_host paths.
|
2017-09-05 22:29:57 +08:00
|
|
|
set known_hosts $known_hosts (string replace -rfi '.*KnownHostsFile\s*' '' <$file)
|
2016-12-04 12:36:47 +08:00
|
|
|
end
|
2016-11-28 13:27:22 +08:00
|
|
|
end
|
2017-09-26 22:21:16 +08:00
|
|
|
for file in $known_hosts
|
|
|
|
if test -r $file
|
|
|
|
# Ignore hosts that are hashed, commented or @-marked and strip the key.
|
|
|
|
awk '$1 !~ /[|#@]/ {
|
|
|
|
n=split($1, entries, ",")
|
|
|
|
for (i=1; i<=n; i++) {
|
|
|
|
# Ignore negated/wildcarded hosts.
|
|
|
|
if (!match(entry=entries[i], "[!*?]")) {
|
|
|
|
# Extract hosts with custom port.
|
|
|
|
if (substr(entry, 1, 1) == "[") {
|
|
|
|
if (pos=match(entry, "]:.*$")) {
|
|
|
|
entry=substr(entry, 2, pos-2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
print entry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}' $file
|
|
|
|
end
|
2016-12-04 12:36:47 +08:00
|
|
|
end
|
|
|
|
return 0
|
2006-02-12 21:14:21 +08:00
|
|
|
end
|