Nslookup in Bash Script

4.1k views Asked by At

How can I get result of a dns mx record with nslookup in a .sh file that it would do all its steps automatically without questioning set type=mx then domain. I just want execute ./file.sh then it give me the result.

The bash file might be something like this:

#!/bin/bash
nslookup
set type=mx
example.com

But everytime I execute the file it just runs the first line nslookup, and it requests set type=mx and domain again.

I got my answer in stackoverflow in another way with using -q=mx:

#!/bin/bash
nslookup -q=mx example.com

But I would like my answer to use set type=mx

2

There are 2 answers

1
Cyrus On BEST ANSWER

Try this:

echo -e "set type=mx\nexample.com" | nslookup
0
gniourf_gniourf On

You very likely want to use here-documents:

nslookup <<EOF
set type=mx
example.com
EOF