Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > Linux Forums > Programming/Scripts

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 1st May 2013, 19:22
K_meleonu K_meleonu is offline
Senior Member
 
Join Date: May 2006
Posts: 112
Thanks: 1
Thanked 5 Times in 4 Posts
Default bash script to change dns on ping fail

Hello to all,
It seems today i am a little or more stupid.
I am trying to write a bash script wich should change the actual dns servers with new ones if ping geths ping: unknown host domain.com or a loss ower 20% or ping fails.

If is someone here who wants to help me or knows a script to do what i nees please post it here.
Will be much apreciate.

Thank you all in advance.

P.S.
The script i wrote and does now work is:
Code:
#!/bin/bash
PING=`ping -c 5 yahoo.com | grep packets | awk '{print $1 $4 $6}' | cut -c 1-3`;
RASPUNS="550";
DNSACT=`tail -n 2 /etc/resolv.conf`;
DNS1=`cat dns1 > /etc/resolv.conf`;
DNS2=`cat dns2 > /etc/resolv.conf`;
DNNS1=`cat dns1`;
DNNS2=`cat dns2`;
if [ "$PING" -ne "$RASPUNS" ];
then
{
if ["$DNSACT" = "$DNS1"]
then {
echo $DNS2;
}
else {
echo $DNS1;
}
fi
}
else
{
echo "yahoo.com raspunde";
}
fi
in file dns1 i have nameserver x.x.x.x and nameserver x.x.x.y
in file dns2 i have nameserver w.w.w.w and nameserver w.w.w.q

This script should do the following:
ping 5 times yahoo.com. If the result is not equal with 550 (means 5 packets sent, 5 packets received and 0% loss) it check's if actual dns servers are the same with the dns servers from dns1 file.
If they are the same the script should put in /etc/resolv.conf the dns servers from file dns2. If they are not the same the script should put in /etc/resolv.conf the dns servers from dns1 file.
Reply With Quote
Sponsored Links
  #2  
Old 1st May 2013, 20:22
K_meleonu K_meleonu is offline
Senior Member
 
Join Date: May 2006
Posts: 112
Thanks: 1
Thanked 5 Times in 4 Posts
Default

Hello again to all,
I have modifyed the script to this:
Code:
#!/bin/bash
PING=`ping -c 5 yahoo.com | grep packets | awk -F% '{print $1 $4 $6}' | cut -c 36-38`;
RASPUNS="20";
DNSACT=`tail -n 2 /etc/resolv.conf`;
DNNS1=`cat dns1`;
DNNS2=`cat dns2`;
if [[(" $PING" > "$RASPUNS") || ("$DNSACT" = "$DNS1")]];
then
{
cat dns2 > /etc/resolv.conf;
}
else 
{
cat dns1 > /etc/resolv.conf;
}
fi
It works fine exceptin when the ping answer is ping: unknown host yahoo.com. If i receive this answer the script terminates and doen not change the dns servers.

Still waiting for some help.
Thank you all in advance

Last edited by K_meleonu; 1st May 2013 at 20:55.
Reply With Quote
  #3  
Old 6th May 2013, 15:55
sjau sjau is offline
Local Meanie
 
Join Date: Apr 2006
Location: Switzerland
Posts: 1,158
Thanks: 4
Thanked 58 Times in 54 Posts
Default

here's a bit an improved version (bash-wise)

Code:
#!/bin/bash
# Don't use ALLCAPS for custom variables. By convention only Environmental vars are ALLCAPS
makeping=$(ping -c 5 "yahoo.com" | grep "packets" | awk -F% '{print $1 $4 $6}' | cut -c 36-38)
raspuns="20";

dnsact=$(tail -n 2 "/etc/resolv.conf")
dnns1=$(cat dns1)
dnns2=$(cat dns2)

if [[ "${makeping}" -gt "${raspuns}" || "${dnsact}" = "${dns1}" ]]
then
    cat "dns2" > "/etc/resolv.conf"
else 
    cat "dns1" > "/etc/resolv.conf"
fi
As for the problem, I have no idea.
__________________
"Common sense is not as common as commonly believed" by sjau

Auto-Install Script for ISPConfig and Horde on a Vanilla Debian Stable

Need more Repos for Ubuntu? Repository Generator
Need more Repos for Debian? Debian Repository Generator
Reply With Quote
  #4  
Old 7th May 2013, 09:24
K_meleonu K_meleonu is offline
Senior Member
 
Join Date: May 2006
Posts: 112
Thanks: 1
Thanked 5 Times in 4 Posts
Default

Thank you for your reply and improvement. For me, the script worked even witll all caps (tested on ubuntu 12.0.4 LTS, Mandriva 2010 / 2011, Fedora 16).
The problem for wich i didn't found a solution yet is the one stated in the post above. If the answer is unknown host the script stops.
The only solution i found is to modify the host (in this case yahoo.com) with an IP address. But if that IP of yahoo.com fails then i will get a false response.
If i use the host, even if the IP fails at first check, at the second check the IP will be another one.

Again thank you for the improvement.

Best regards
Reply With Quote
  #5  
Old 7th May 2013, 09:25
sjau sjau is offline
Local Meanie
 
Join Date: Apr 2006
Location: Switzerland
Posts: 1,158
Thanks: 4
Thanked 58 Times in 54 Posts
Default

as said, you shouldn't use ALLCAPS on vars. Also use $() to run commands instead of backticks.
__________________
"Common sense is not as common as commonly believed" by sjau

Auto-Install Script for ISPConfig and Horde on a Vanilla Debian Stable

Need more Repos for Ubuntu? Repository Generator
Need more Repos for Debian? Debian Repository Generator
Reply With Quote
  #6  
Old 15th September 2013, 12:39
K_meleonu K_meleonu is offline
Senior Member
 
Join Date: May 2006
Posts: 112
Thanks: 1
Thanked 5 Times in 4 Posts
Default Here is the solution

Code:
#!/bin/bash
PING=`ping -c 5 yahoo.com | grep packets | awk -F% '{print $1 $4 $6}' | cut -c 36-38`;
RASPUNS="20";
DNSACT=`tail -n 2 /etc/resolv.conf`;
DNNS1=`cat dns1`;
DNNS2=`cat dns2`;
if [[(" $PING" > "$RASPUNS") || ("$DNSACT" = "$DNS1")]];
then
{
cat dns2 > /etc/resolv.conf;
}
else
{
cat dns1 > /etc/resolv.conf;
}
fi
This script will ping yahoo.com 5 times. If the ping result shows a loos ower 20% it will change the DNS servers.
You will have to create 2 files named dns1 and dns2 in wich you have to write your nameserver x.x.x.x and nameserver y.y.y.y and nameserver x.x.x.x-alternative and nameserver y.y.y.y-alternative and run the script into a cron job.
Reply With Quote
  #7  
Old 22nd September 2013, 12:25
Marios Zindilis Marios Zindilis is offline
Junior Member
 
Join Date: Sep 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's a potential problem with the script: ping performs two DNS resolutions during its operation:
  1. resolves yahoo.com to an IP, then
  2. resolves that IP back to a fully qualified domain name.

For example:
Code:
marios@yovan ~ $ ping -c 1 yahoo.com
PING yahoo.com (206.190.36.45) 56(84) bytes of data.
64 bytes from ir1.fp.vip.gq1.yahoo.com (206.190.36.45): icmp_req=1 ttl=49 time=306 ms

--- yahoo.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 306.046/306.046/306.046/0.000 ms
You can see that ping resolved yahoo.com to 206.190.36.45 and then resolved again 206.190.36.45 to ir1.fp.vip.gq1.yahoo.com (the FQDN).

If by coinsidence, the FQDN contains the word "packets", this will throw off your grep. So a small improvement to your script would be to add the -q option to ping (stands for "quiet"), so that the output is only the summary lines:
Code:
marios@yovan ~ $ ping -q -c 5 yahoo.com
PING yahoo.com (98.139.183.24) 56(84) bytes of data.

--- yahoo.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4003ms
rtt min/avg/max/mdev = 180.680/221.430/286.038/42.478 ms
Reply With Quote
  #8  
Old 22nd September 2013, 16:44
K_meleonu K_meleonu is offline
Senior Member
 
Join Date: May 2006
Posts: 112
Thanks: 1
Thanked 5 Times in 4 Posts
 
Default

Hello there,
You are right but you can also use ping 8.8.8.8 (google dns) or other yahoo.com or google.com IP address instead of domain name.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
DNS FAIL!!! witchcraft??? loadingjkr Installation/Configuration 15 3rd June 2012 21:19
Issue when running a bash script cj09 Programming/Scripts 1 13th July 2011 16:22
Apache2 FCGID locking up the box? crypted General 6 12th April 2011 17:16
Can't Ping over VPN RoMiONeT Server Operation 2 4th July 2010 01:19
When External IP is Change Error on DNS Service Occurs bluegrass HOWTO-Related Questions 7 17th January 2006 19:01


All times are GMT +2. The time now is 10:25.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.