LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-24-2014, 10:17 AM   #1
116Fanatic
LQ Newbie
 
Registered: Dec 2012
Posts: 5

Rep: Reputation: Disabled
Script to perform DNS lookup and email if it fails


I have been searching for a script that will be hosted on an external network that performs a DNS lookup (put it in a cron job) and if it fails it will send an email alert. I plan to use a google account for smtp. Does anyone have a script that would do this?

Thank you!
 
Old 02-24-2014, 10:44 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,420

Rep: Reputation: 8111Reputation: 8111Reputation: 8111Reputation: 8111Reputation: 8111Reputation: 8111Reputation: 8111Reputation: 8111Reputation: 8111Reputation: 8111Reputation: 8111
Quote:
Originally Posted by 116Fanatic View Post
I have been searching for a script that will be hosted on an external network that performs a DNS lookup (put it in a cron job) and if it fails it will send an email alert. I plan to use a google account for smtp. Does anyone have a script that would do this?

Thank you!
Well, if you can't find something on Google that does what you want, post what you have written/tried on your own, and tell us where you're stuck. Otherwise, there are many easily-found scripting tutorials (like the one in my posting signature for starters), that will get you going. But we're not going to write scripts for you.

You can probably find scripts that run ping tests against a list of IP addresses....modify it to run a dig against a domain list.
 
Old 02-24-2014, 12:11 PM   #3
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7 / 8
Posts: 3,538

Rep: Reputation: 1593Reputation: 1593Reputation: 1593Reputation: 1593Reputation: 1593Reputation: 1593Reputation: 1593Reputation: 1593Reputation: 1593Reputation: 1593Reputation: 1593
Pay www.nodeping.com $8 a month
 
Old 02-24-2014, 12:24 PM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
do something and when it FAILS do something else. Sounds perfect for an OR operation.

Code:
nslookup somewhere.com || #send mail to someone saying it didnt work!
 
2 members found this post helpful.
Old 02-25-2014, 10:36 AM   #5
116Fanatic
LQ Newbie
 
Registered: Dec 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
I wasn't expecting someone to write a script for me, I was just hoping someone already had something in place. Here is what I used in case it helps anyone in the future:

The first script creates a temporary file with the result of the lookup. I grep for the address. It calls the second script to check the temp file to see if it is empty. It then deletes the temp file:
#!/bin/bash
dig @yourdnsserver test.com | grep 1.1.1.1 > dnsfile.tmp
dnsfilecompare.sh dnsfile.tmp
rm dnsfile.tmp


Script 2 sends an email if the file is empty and only echos if the file has contents:

#!/bin/bash
_file="$1"
[ $# -eq 0 ] && { echo "Usage: $0 filename"; exit 1; }
[ ! -f "$_file" ] && { echo "Error: $0 file note found."; exit 2; }

if [ -s "$_file" ]
then
echo "All is well"
else
echo "DNS lookup of test.com failed" | mail -s "DNS lookup of test.com failed" user@test.com
fi
 
Old 02-25-2014, 10:45 AM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
thats cool. you can do this same procedure with just this oneliner though:

Code:
nslookup oaiwhefoih.com || echo "DNS lookup of test.com failed" | mail -s "DNS lookup of test.com failed" user@test.com
 
Old 02-25-2014, 10:56 AM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326

Rep: Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920
Quote:
Originally Posted by szboardstretcher View Post
do something and when it FAILS do something else. Sounds perfect for an OR operation.

Code:
nslookup somewhere.com || #send mail to someone saying it didnt work!
good idea but nslookup still returns 0 even if the site cant be found (its not that the program failed -- its reporting correctly that the site cant be found).

maybe ping -c 1 ... would be better here ?

this is what i would do:
Code:
server=does_not_exist.com
if [ -n "`nslookup $server | grep can\'t\ find`" ]
then 
 echo "DNS lookup of $server failed" | mail -s "DNS lookup of $server failed" user@test.com
fi

Last edited by schneidz; 02-25-2014 at 11:08 AM.
 
Old 02-25-2014, 11:00 AM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Quote:
Originally Posted by schneidz View Post
good idea but nslookup still returns 0 even if the site cant be found (its not that the program failed -- its reporting correctly that the site cant be found).
No it doesn't.

Code:
# nslookup asdfioejf.com 

** server can't find asdfioejf.com: NXDOMAIN

# echo $?
1
 
Old 02-25-2014, 11:12 AM   #9
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326

Rep: Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920
Quote:
Originally Posted by szboardstretcher View Post
No it doesn't.

Code:
# nslookup asdfioejf.com 

** server can't find asdfioejf.com: NXDOMAIN

# echo $?
1
thats so weird:
Code:
[schneidz@hyper sd-bak-04.04.2013]$ nslookup asdfioejf.com
Server:		192.168.1.1
Address:	192.168.1.1#53

** server can't find asdfioejf.com: NXDOMAIN

[schneidz@hyper sd-bak-04.04.2013]$ echo $?
0
 
Old 02-25-2014, 11:18 AM   #10
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Well then. Seems we are using different versions that treat exit codes differently?

In that case, dig it:

Code:
dig asdfefefefaf.com | grep NOERROR 1>/dev/null || echo "DNS lookup of test.com failed" | mail -s "DNS lookup of test.com failed" user@localhost
That work on yours?
 
Old 02-25-2014, 11:28 AM   #11
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326

Rep: Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920
this is what i get:
Code:
[schneidz@hyper sd-bak-04.04.2013]$ dig asdfefefefaf.com | grep NOERROR 1>/dev/null || echo "DNS lookup of test.com failed" 
DNS lookup of test.com failed
[schneidz@hyper sd-bak-04.04.2013]$ dig asdfioejf.com

; <<>> DiG 9.8.0-P4-RedHat-9.8.0-7.P4.fc15 <<>> asdfioejf.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 3073
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0

;; QUESTION SECTION:
;asdfioejf.com.			IN	A

;; AUTHORITY SECTION:
com.			627	IN	SOA	a.gtld-servers.net. nstld.verisign-grs.com. 1393345357 1800 900 604800 86400

;; Query time: 14 msec
;; SERVER: 192.168.1.1#53(192.168.1.1)
;; WHEN: Tue Feb 25 11:27:28 2014
;; MSG SIZE  rcvd: 104

[schneidz@hyper sd-bak-04.04.2013]$ echo $?
0
this old nettop is running fc-15 (prolly has something to do with it ?).

Last edited by schneidz; 02-25-2014 at 11:31 AM.
 
Old 02-25-2014, 11:31 AM   #12
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
That worked as expected. If dig works (NOERROR) then it will do nothing. If dig does not work (NOERROR not found) it mails a message.

Wow.. yeah, the nslookup difference is definitely related to fedora15. You should really consider updating to something newer.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
DNS lookup fails (I think) matazar42 Linux - Networking 2 02-04-2009 08:11 AM
Reverse DNS lookup fails <Ol>Origy Linux - Security 3 08-14-2008 12:48 PM
how do i perform an reverse dns lookup? HyperTrey Linux - Networking 4 05-23-2008 09:48 AM
Traceroute, Ping, Domain Name Server (DNS) Lookup, WHOIS, and DNS Records Lookup netoknet General 1 05-09-2005 04:43 AM
Recommendation about what Linux app to hook into PHP to perform revers dns on email? ServerStorm Programming 4 03-17-2005 02:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration