View Single Post
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 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.

The script i wrote and does now work is:
PING=`ping -c 5 | grep packets | awk '{print $1 $4 $6}' | cut -c 1-3`;
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" ];
if ["$DNSACT" = "$DNS1"]
then {
echo $DNS2;
else {
echo $DNS1;
echo " raspunde";
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 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