0

Could someone help me if there exists some way how to get from dig only hostnames if I want to get list of DNS zone? I work with this command:

dig @dns.example.com example.com axfr

output is:

example1.com.  1200   IN   A    1.1.1.1
example2.com.  1200   IN   A    2.2.2.2
....

I would like to get only

example1.com
example2.com
....

I tried to use filters dig .... | cut -d ' ' -f1 , but it doesn't work correctly in this case. Maybe I should use some regex?

jozinko9
  • 3
  • 1
  • `dig +short . . . ` or check out this question https://serverfault.com/questions/431080/dig-show-only-answer?rq=1 – Brandon Xavier Aug 31 '21 at 08:11
  • @BrandonXavier I tried `+short` but it gives me a lot of IP address instead of names. I think the better way will be to parse it. However I don't know how to cut first column. – jozinko9 Aug 31 '21 at 08:31
  • Your cut command looks correct. What is the problem you are having with it? – Michael Hampton Sep 04 '21 at 11:55

1 Answers1

0

Safe your ouput into TEST and then run this command. It compares your output with a regex which searches for the hostname.

[[ $TEST =~ ^([a-zA-Z1-9])*.[a-zA-Z]* ]] | echo "${BASH_REMATCH}"

Hope this Helps