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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
02-24-2014, 10:17 AM
|
#1
|
LQ Newbie
Registered: Dec 2012
Posts: 5
Rep:
|
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!
|
|
|
02-24-2014, 10:44 AM
|
#2
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,420
|
Quote:
Originally Posted by 116Fanatic
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.
|
|
|
02-24-2014, 12:11 PM
|
#3
|
Senior Member
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7 / 8
Posts: 3,538
|
|
|
|
02-24-2014, 12:24 PM
|
#4
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
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.
|
02-25-2014, 10:36 AM
|
#5
|
LQ Newbie
Registered: Dec 2012
Posts: 5
Original Poster
Rep:
|
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
|
|
|
02-25-2014, 10:45 AM
|
#6
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
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
|
|
|
02-25-2014, 10:56 AM
|
#7
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326
|
Quote:
Originally Posted by szboardstretcher
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.
|
|
|
02-25-2014, 11:00 AM
|
#8
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
Quote:
Originally Posted by schneidz
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
|
|
|
02-25-2014, 11:12 AM
|
#9
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326
|
Quote:
Originally Posted by szboardstretcher
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
|
|
|
02-25-2014, 11:18 AM
|
#10
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
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?
|
|
|
02-25-2014, 11:28 AM
|
#11
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326
|
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.
|
|
|
02-25-2014, 11:31 AM
|
#12
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
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.
|
|
|
All times are GMT -5. The time now is 04:07 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|