1

I'm making a script to preform "dig ns google.com" and cut off the all of the result except for the answers section.

So far I have:

#!/bin/bash

echo -n "Please enter the domain: "
read d
echo "You entered: $d"
dr="$(dig ns $d)"
sr="$(sed -i 1,10d $dr)"
tr="$(head -n -6 $sr)"
echo "$tr"

Theoretically, this should work. The sed and head commands work individually outside of the script to cut off the first 10 and last 6 respectively, but when I put them inside my script sed comes back with an error and it looks like it's trying to read the variable as part of the command rather than the input. The error is:

sed: invalid option -- '>'

So far I haven't been able to find a way for it to read the variable as input. I've tried surrounding it in "" and '' but that doesn't work. I'm new to this whole bash scripting thing obviously, any help would be great!

3

1 Answer 1

4

sed takes its input from stdin, not from the command line, so your script won't work either theoretically or practically. sed -i 1,10d $dr does not do what you think it does...sed will treat the value of "$dr" as a list of filenames to process.

Try echo "$dr" | sed -e '1,10d' or sed -e '1,10d' <<<"$dr".

BTW, you must use double-quotes around "$dr" here, otherwise sed will not get multiple lines of input separated by \n, it will only get one input line.

Or better yet, to get only the NS records, replace all of your sed commands with just this one command:

tr=$(echo "$dr" | sed -n -e '/IN[[:space:]]\+NS[[:space:]]/p')

Alternatively, eliminate all the intermediate steps and just run this:

tr=$(dig ns "$d" | sed -n -e '/IN[[:space:]]\+NS[[:space:]]/p')

Or you can get just the nameservers' hostnames with:

tr=$(dig ns "$d" | awk '/IN[[:space:]]+NS[[:space:]]/ {print $5}')

BTW, the output of host -t ns domain.example.com may be easier to parse than the output of dig.

7
  • Great answer! Good to know about sed, makes sense why it doesn't work then. The end goal here is to create a command that will find all the DNS zone records (a aaaa cname mx srv txt) and display them in a nice format. I'm starting with NS because that's easiest
    – Egrodo
    Apr 11, 2016 at 2:06
  • Also, that host command is fantastic, and I'll definitely be using that instead. However it doesn't seem to support srv, txt, or cname. Do you know of anything that does?
    – Egrodo
    Apr 11, 2016 at 2:08
  • 1
    host can fetch any type of DNS resource record, but you need to remember that a domain has SOA and NS and some other records (including A and TXT) but can not be a CNAME (that would make no sense at all). A hostname within a domain can be a CNAME record, but not the domain itself.
    – cas
    Apr 11, 2016 at 2:13
  • Oh huh, okay. Sorry, new to this whole thing, so what are the cname records that I'm thinking of then like how in a DNS zone editor in a cPanel it lists the "mail; mail.domain.com; TTL". And can you clarify shortly the difference between a host name and a domain? A hostname is the alphabetical name of a system/computer, right?
    – Egrodo
    Apr 11, 2016 at 2:18
  • 1
    For example: example.com would be the domain. it can have NS, A, SOA, TXT, and some other records. it can NOT have a CNAME. mail.example.com is a hostname within example.com. It can have A, CNAME, TXT and/or other records - but if it has an NS record then it is a sub-domain of example.com. if a DNS record is a CNAME then it can have NO OTHER records, it's just an alias pointing at some other hostname. This is a gross simplification, you really need to read DNS & BIND (shop.oreilly.com/product/9780596100575.do) or some other reference book on DNS.
    – cas
    Apr 11, 2016 at 2:22

You must log in to answer this question.

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