LinuxQuestions.org
Visit Jeremy's Blog.
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, 09: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, 09:44 AM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,134

Rep: Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456
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, 11:11 AM   #3
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6
Posts: 1,398

Rep: Reputation: 433Reputation: 433Reputation: 433Reputation: 433Reputation: 433
Pay www.nodeping.com $8 a month
 
Old 02-24-2014, 11:24 AM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux
Posts: 2,766
Blog Entries: 1

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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, 09: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, 09:45 AM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux
Posts: 2,766
Blog Entries: 1

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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, 09:56 AM   #7
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-19-live-usb/ aix
Posts: 3,822

Rep: Reputation: 586Reputation: 586Reputation: 586Reputation: 586Reputation: 586Reputation: 586
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 10:08 AM.
 
Old 02-25-2014, 10:00 AM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux
Posts: 2,766
Blog Entries: 1

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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, 10:12 AM   #9
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-19-live-usb/ aix
Posts: 3,822

Rep: Reputation: 586Reputation: 586Reputation: 586Reputation: 586Reputation: 586Reputation: 586
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, 10:18 AM   #10
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux
Posts: 2,766
Blog Entries: 1

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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, 10:28 AM   #11
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-19-live-usb/ aix
Posts: 3,822

Rep: Reputation: 586Reputation: 586Reputation: 586Reputation: 586Reputation: 586Reputation: 586
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 10:31 AM.
 
Old 02-25-2014, 10:31 AM   #12
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux
Posts: 2,766
Blog Entries: 1

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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 07:11 AM
Reverse DNS lookup fails <Ol>Origy Linux - Security 3 08-14-2008 11:48 AM
how do i perform an reverse dns lookup? HyperTrey Linux - Networking 4 05-23-2008 08:48 AM
Traceroute, Ping, Domain Name Server (DNS) Lookup, WHOIS, and DNS Records Lookup netoknet General 1 05-09-2005 03:43 AM
Recommendation about what Linux app to hook into PHP to perform revers dns on email? ServerStorm Programming 4 03-17-2005 01:57 PM


All times are GMT -5. The time now is 06:58 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration