Score:0

dig - get only hostnames

cn flag

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?

Brandon Xavier avatar
us flag
`dig +short . . . ` or check out this question https://serverfault.com/questions/431080/dig-show-only-answer?rq=1
jozinko9 avatar
cn flag
@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.
Michael Hampton avatar
cz flag
Your cut command looks correct. What is the problem you are having with it?
Score:0
fr flag

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

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.