1

I have a stateful set for MongoDB replication where I need all the hostnames of pods through the service endpoint. I am able to fetch the names but not exactly the only list of hostnames.

$ dig srv mongodb +search +short
0 100 27017 mongodb-0.mongodb.pilot-bots.svc.cluster.local.

Here I'm getting extra content 0 100 27017 whichI want to exclude.

How can I get only names? like IPS below.

$ dig mongodb A +search +short
172.1.0.220

SNR
  • 460
  • 5
  • 20

1 Answers1

0

@anemyte provided a valid solution with his awk:

dig srv mongodb +search +short | awk '{print $4}'

However, you could also approach this from a different way. Dig has several options that can be adjusted in order to print only the info you desire as an output. This guide explains this approach in more detail:

Get a not-quite-so-short answer?

Note that a short answer is different from only an answer. The way to get a detailed answer, but without any auxiliary information, is to turn off all the results (+noall) and then turn on only those sections you want.

Here’s a short answer followed by only an answer; the latter includes all the configuration information, including time-to-live (TTL) data, displayed in a format compatible with BIND configuration files.

Using +noall followed by other option of your choosing will result in showing only those records that interest you. You can also just hide the records you don't want to print, for example, hide the TTL by using +nottlid option.

Wytrzymały Wiktor
  • 11,492
  • 5
  • 29
  • 37
  • I didn't exactly get the [no] filter to remove ```0 100 27017``` I tried +noport but still 27017 is showing – SNR Jun 10 '21 at 09:03
  • Unfortunately, there is no such option as `+[no]port`. Instead you could try with `+nocl`and `+nottl`. Or do as suggested in the guide and start with `+noall` followed by sections you want. – Wytrzymały Wiktor Jun 10 '21 at 10:08
  • I tried ```dig srv mongodb +search +noall +answer +short +nottl +nocl```. But no help. Can you share the command that worked for you? – SNR Jun 12 '21 at 09:36
  • If none of the above suggestions worked for you than simply stick with the `awk` solution proposed by @anemyte. I will change my answer to community wiki one to give him more credit for that. – Wytrzymały Wiktor Jun 14 '21 at 07:22