figtrap
2/18/2016 - 7:53 PM

shell function to crawl the dns tree and search for a SRV record in each parent zone. It will exit once it finds the first SRV record.

shell function to crawl the dns tree and search for a SRV record in each parent zone. It will exit once it finds the first SRV record.

service-lookup(){
    # use like: service-lookup _mysql._tcp wtf.li.ux.co.uk
    local service="${1}"
    local zone="${2}"
    eLen=$(printf %s "${zone}" | tr "." " " | wc -w)
    if [ ! -z "${service}" -a ! -z "${zone}" -a ! "${1}" = "help" ] ; then
        for (( i=1; i<=${eLen}; i++ )) ; do
            query_zone=$(printf %s "${zone}" | cut --delimiter="." --fields="${i}"-)
            response=$(dig +short ${service}.${query_zone} srv | tr -d "\"")
            if [ ! -z "${response}" ] ; then
                printf "%s" "${response}"
                break
            fi
        done
    else
        [ ! -z $PS1 ] && printf "%s\n" "Usage: ${FUNCNAME[0]} <service to lookup> <dns zone>"
    fi
}