4

I'm using dns to manage my virtual hosts. In order to do so I query my nameserver with the host command for certain values I need. For example:

> host -t txt mycl1.vz
mycl1.vz.myserver.de descriptive text "1026"

but I only need 1026 as answer without the chatter. Currently I'm using sed to remove it like:

| sed -e 's/.*descriptive text "\(.*\)"/\1/'

but this seems a little "unstable" and I wonder if there isn't some command which would give me the plain output in the first place?

2 Answers 2

9

Use dig(1) with the +short flag instead:

$ host -t txt google.com
google.com descriptive text "v=spf1 include:_spf.google.com ip4:216.73.93.70/31 ip4:216.73.93.72/31 ~all"

$ dig -t txt google.com +short
"v=spf1 include:_spf.google.com ip4:216.73.93.70/31 ip4:216.73.93.72/31 ~all"

If you want to remove the quotes, just filter the output through sed:

$ dig -t txt google.com +short | sed 's/"//g'
v=spf1 include:_spf.google.com ip4:216.73.93.70/31 ip4:216.73.93.72/31 ~all
2
  • That's what I was looking for. Now I have still to remove the quotes.
    – Scheintod
    Oct 10, 2013 at 10:37
  • "How about reverse PTR records" you might ask? dig has the "-x" argument for that, nowadays: dig +short -x 1.1.1.1
    – conny
    Jul 7, 2023 at 5:25
1

My first choice would be dig as dawud pointed out. If you stick with 'host', you could replace sed with:

cut -d \" -f 2
2
  • Thanks. Good shortcut. But I need it for A records, too.
    – Scheintod
    Oct 10, 2013 at 10:38
  • 1
    Host has a variable format output. One more reason to use dig for such operations :) (For example cut -d " " -f 4 for A records and host)
    – Bgs
    Oct 10, 2013 at 11:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .