summaryrefslogtreecommitdiff
path: root/getsrv
blob: 58fb03b2d14fbc0d120c9d186c77aff9fad9dd45 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env bash
if [ "_$1" = '_-a' ];then
  head="uniq"
  shift
else
  head="head -n1"
fi
record="_$1._$2.$3"
shift 3
if [ "_$*" != "_" ];then
  filter='grep -vF "'"$(printf "%s\n" "$*" | tr ' ' '\n')"'"'
else
  filter=cat
fi
if which host >/dev/null;then
  host -t SRV "$record" | cut '-d ' -f5-
  #echo "after host" >&2
elif which dig >/dev/null;then
  dig +short -t SRV "$record" 
  #echo "after dig" >&2
else
  echo "how am I supposed to get a srv record without dig or host? giving up." >&2
  exit 1
fi | grep '^[0-9][0-9]* [0-9][0-9]*' \
  | sort -rn \
  | rev \
  | cut -b2- \
  | rev \
  | awk '{print $1, $2, $4":"$3}' \
  | eval "$filter" \
  | awk '{if(NR==1){D=$1}if(D==$1)print $0}' \
  | cut '-d ' -f2- \
  | awk '{for(i=0;i<=$1;i++) {print $0} }' \
  | cut '-d ' -f2- \
  | shuf 2>/dev/null \
  | head -n1 \
## that last pipeline tries to get pretty close to printing a properly weighted response, but not perfect.