2006-02-12 21:14:21 +08:00
|
|
|
|
|
|
|
function __fish_print_hostnames -d "Print a list of known hostnames"
|
2016-02-27 07:30:16 +08:00
|
|
|
# HACK: This only deals with ipv4
|
2006-02-12 21:14:21 +08:00
|
|
|
|
|
|
|
# Print all hosts from /etc/hosts
|
2015-06-19 16:42:48 +08:00
|
|
|
if type -q getent
|
2016-02-27 07:30:16 +08:00
|
|
|
# Ignore zero ips
|
2016-04-08 10:46:51 +08:00
|
|
|
getent hosts | string match -r -v '^0.0.0.0' \
|
2016-02-27 07:30:16 +08:00
|
|
|
| string replace -r '[0-9.]*\s*' '' | string split " "
|
2015-06-19 16:42:48 +08:00
|
|
|
else if test -r /etc/hosts
|
2016-02-27 07:30:16 +08:00
|
|
|
# Ignore commented lines and functionally empty lines
|
2016-04-08 10:46:51 +08:00
|
|
|
string match -r -v '^\s*0.0.0.0|^\s*#|^\s*$' < /etc/hosts \
|
2016-02-27 07:30:16 +08:00
|
|
|
# Strip comments
|
|
|
|
| string replace -ra '#.*$' '' \
|
|
|
|
| string replace -r '[0-9.]*\s*' '' | string trim | string replace -ra '\s+' '\n'
|
2006-02-12 21:14:21 +08:00
|
|
|
end
|
2016-02-27 21:33:03 +08:00
|
|
|
|
2006-02-12 21:14:21 +08:00
|
|
|
# Print nfs servers from /etc/fstab
|
2016-02-27 07:32:10 +08:00
|
|
|
if test -r /etc/fstab
|
2016-02-27 21:33:03 +08:00
|
|
|
string match -r '^\s*[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3]:|^[a-zA-Z\.]*:' </etc/fstab | string replace -r ':.*' ''
|
2006-02-12 21:14:21 +08:00
|
|
|
end
|
|
|
|
|
2016-02-27 07:31:21 +08:00
|
|
|
# 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
|
|
|
|
if test -r $file
|
|
|
|
# Print hosts from system wide ssh configuration file
|
|
|
|
# Note the non-capturing group to avoid printing "name"
|
2016-06-15 23:54:40 +08:00
|
|
|
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*' '')
|
2016-02-27 07:31:21 +08:00
|
|
|
end
|
2014-01-11 03:58:54 +08:00
|
|
|
end
|
2016-02-27 07:31:21 +08:00
|
|
|
for file in $known_hosts
|
|
|
|
# Ignore hosts that are hashed, commented or have custom ports (like [localhost]:2200)
|
|
|
|
test -r $file; and string replace -ra '(\S+) .*' '$1' < $file | string match -r '^[^#|[=]+$' | string split ","
|
2009-02-03 05:02:42 +08:00
|
|
|
end
|
2016-02-27 07:31:21 +08:00
|
|
|
return 0
|
2006-02-12 21:14:21 +08:00
|
|
|
end
|