Skip to content

Instantly share code, notes, and snippets.

@toke
Last active June 8, 2016 20:23
Show Gist options
  • Select an option

  • Save toke/cd78cb204015c704f1c458529d45bbee to your computer and use it in GitHub Desktop.

Select an option

Save toke/cd78cb204015c704f1c458529d45bbee to your computer and use it in GitHub Desktop.
cluster ssh to clusters via DNS SRV RR

Cluster ssh into a cluster via DNS SRV RR's.

Example:

# Looking up SSH Endpoints
$ host -t SRV _ssh._tcp.service.domain.tld
_ssh._tcp.service.domain.tld IN SRV 0 1 22 a.service.domain.tld.
_ssh._tcp.service.domain.tld IN SRV 0 1 22 b.service.domain.tld.

# Open all Hosts via a small wrapper script for cssh
# It basically just look in DNS SRV records
$ srv-cssh service.domain.tld

This is a proof of concept and has no error handling. Requires bash, awk, dig and cssh (cluster ssh).

#
# Calls cssh with the result of the _ssh._tcp records for Host.
# srv-cssh user@host # user@ is optional
# host needs _ssh._tcp. SRV DNS RR
# All listed hosts will be connected
#
function srv-cssh {
array=(${1//@/ })
if [[ -z ${array[0]} ]] ; then
echo "Usage: srv-cssh [user@]host"
return 1
elif [[ -z ${array[1]} ]] ; then
host=${array[0]}
user=""
else
host=${array[1]}
user="-l ${array[0]}"
fi
dns="@a.ns.kerpe.net" # dig syntax @ns
$(dig +short -t SRV "_ssh._tcp.${host}" ${dns} \
| awk -v USER="${user}" -e 'BEGIN{ printf "cssh " USER} /^[a-zA-Z0-9\.\-_ ]+$/ { printf " " $4;} END {print "";}')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment