我正在这样解析dig的输出,以获取SRV记录的端口。

export SERVER_DNS_NAME=myserver
echo "SERVER_DNS_NAME: " $SERVER_DNS_NAME
echo "dig: " $(dig +noall +answer $SERVER_DNS_NAME SRV )
echo "port old: " $(dig +noall +answer $SERVER_DNS_NAME SRV | cut -d ' ' -f 6)
SERVER_DIG_RESULT=$(dig +noall +answer $SERVER_DNS_NAME SRV )
echo "SERVER_DIG_RESULT: " $SERVER_DIG_RESULT
SERVER_STRING_ARRAY=($SERVER_DIG_RESULT)
for i in "${SERVER_STRING_ARRAY[@]}"
do
  :
  echo $i
done
SERVER_PORT=${SERVER_STRING_ARRAY[6]}
echo "server port new: " $SERVER_PORT
if [ -z $SERVER_PORT ]; then
  echo "invalid port"
  exit 1
fi
until nc -z $SERVER_DNS_NAME $SERVER_PORT
... do something

我的问题是,有时端口在数组项6处,有时在数组项7处。
我的问题是:如何从挖掘结果中可靠地解析端口?

最佳答案

使用+short选项来dig它将为您提供最简短的输出,然后对其进行简单的分析:

% dig +short _xmpp-client._tcp.jabber.org. SRV
31 30 5222 hermes2v6.jabber.org.
30 30 5222 hermes2.jabber.org.

09-25 16:09