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?

Scheintod
  • 391
  • 1
  • 5
  • 17

2 Answers2

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
dawud
  • 15,096
  • 3
  • 42
  • 61
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
Bgs
  • 208
  • 2
  • 5
  • Thanks. Good shortcut. But I need it for A records, too. – Scheintod Oct 10 '13 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 '13 at 11:19