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.
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 1st May 2013, 18:22
K_meleonu K_meleonu is offline
Senior Member
 
Join Date: May 2006
Posts: 108
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
 

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 20:19
Issue when running a bash script cj09 Programming/Scripts 1 13th July 2011 15:22
Apache2 FCGID locking up the box? crypted General 6 12th April 2011 16:16
Can't Ping over VPN RoMiONeT Server Operation 2 4th July 2010 00:19
When External IP is Change Error on DNS Service Occurs bluegrass HOWTO-Related Questions 7 17th January 2006 18:01


All times are GMT +2. The time now is 04:16.


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