1
0
Fork 0
misc-linux-tools/bin/ssh-hosts

121 lines
2.1 KiB
Text
Raw Normal View History

2015-03-06 09:12:26 -05:00
#!/usr/bin/env bash
IFS=$'\n'
readonly PROGRAM_NAME=$(basename $0)
readonly PROGRAM_LOC=$(readlink -m $(dirname $0))
readonly ARGS="$@"
readonly SSH_CONFIG_FILE=~/.ssh/config
error () {
echo $PROGRAM_NAME: missing operand
echo Try \'$PROGRAM_NAME --help\' for information.
exit 0
}
usage () {
echo usage: $PROGRAM_NAME options
echo "Try '$PROGRAM_NAME -h' for more information."
exit 0
}
info () {
cat <<- EOF
usage: $PROGRAM_NAME options
Used to make hosts configured in a .ssh config file easily accesible.
It pulls in the .ssh/config file from the user's home directory and
prints it based on options.
OPTIONS:
-g List all the different groups of hosts
-l List all hosts for a specific group number
-h This help information
Examples:
List all groups
$PROGRAM_NAME -g
List all hosts for group 3
$PROGRAM_NAME -l 3
EOF
exit
}
ssh_config () {
cat $SSH_CONFIG_FILE
}
ssh_groups () {
ssh_config \
| grep --regexp=^# \
| /usr/bin/tr -d '#'
}
ssh_hosts_for_group () {
local readonly group=$1; shift
local readonly groups=($(ssh_groups))
local readonly start=${groups[$group]}
local readonly end=${groups[$group + 1]}
ssh_config \
| sed -n "/$start/,/$end/p" \
| /usr/bin/head -n -1
}
show_ssh_groups () {
2014-06-16 15:24:54 -04:00
local readonly groups=($(ssh_groups))
local group
local group_number=0
for group in "${groups[@]}"
do
echo "$group_number) $group"
group_number=$[$group_number + 1]
done
}
show_hosts_for_ssh_group () {
local readonly group=$1; shift
ssh_hosts_for_group $group
}
cmdline () {
while getopts ":gl:h" opt; do
case "${opt}" in
g)
show_ssh_groups
;;
l)
show_hosts_for_ssh_group $OPTARG
;;
h)
info
;;
2014-06-16 15:24:54 -04:00
:)
echo "Option -$OPTARG requires an argument." >&2
exit
;;
*)
usage
;;
esac
done
2014-06-16 15:24:54 -04:00
if [ $OPTIND -eq 1 ]
then
usage
fi
shift $((OPTIND-1))
}
main () {
cmdline $ARGS
}
main