LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-25-2014, 03:18 AM   #1
srinivasanece
LQ Newbie
 
Registered: Mar 2012
Location: Komarapalayam,TamilNadu,INDIA
Distribution: CentOS,FreeBSD,RedHatLinux
Posts: 26

Rep: Reputation: Disabled
Scripting Help


Hi Everyone,

I have two gateway and i need to change route if one fails by using script.

Ex: 100,101 are two gateways.

first the route is in 100.

If ping to yahoo.com fails route need to changed to 101.

Help me with this
 
Old 04-25-2014, 03:21 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Ok, so what have you written so far?
 
Old 04-25-2014, 03:36 AM   #3
srinivasanece
LQ Newbie
 
Registered: Mar 2012
Location: Komarapalayam,TamilNadu,INDIA
Distribution: CentOS,FreeBSD,RedHatLinux
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TenTenths View Post
Ok, so what have you written so far?
#!/bin/sh

ping -q -c5 yahoo.com > /dev/null

if [ $? -eq 0 ]
then
echo "Route Ok"
else
echo "Changing Route"
/sbin/route del default
/sbin/route add default gw 192.168.27.101
ping -q -c5 yahoo.com > /dev/null
if [ $? -eq 0 ]
then
echo "Route Ok"
else
echo "Changing Route"
/sbin/route del default
/sbin/route add default gw 192.168.27.100
fi
fi

Last edited by srinivasanece; 04-25-2014 at 03:38 AM.
 
Old 04-25-2014, 03:52 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
First put [code] tags around your scripts.

Code:
#!/bin/sh

 ping -q -c5 yahoo.com > /dev/null

 if [ $? -eq 0 ]
 then
   echo "Route Ok"
 else
   echo "Changing Route"
   /sbin/route del default
   /sbin/route add default gw 192.168.27.101 
   if [ $? -eq 0 ]
   then
     echo "Route Ok"
   else
     echo "Changing Route"
     /sbin/route del default
     /sbin/route add default gw 192.168.27.100 
   fi
fi
Secondly, your script will change the gateway to 101 and then if changing the gateway isn't successful it'll change it to 100, not what I think you want to achieve.

Do you have a preferred gateway or do you only want to switch gateways when there is a failure?

If you're only interested in switching when there's a failure you could do something like:

Code:
#!/bin/bash
ping -q -c5 yahoo.com > /dev/null

if [ $? -eq 0 ] ; then
  echo "Route Ok"
else
  echo "Changing Route"
  GW=$(/sbin/route | grep default | awk -F. {'print $4'})  # Find out what the last octet of the current default route
  if [ ${GW} -eq "100" ] ; then                      # If it's 100
    GW="101"                                         # we change it to 101
  else                                               # If it's not 100
    GW="100"                                         # we change it t0 100
  fi
  /sbin/route del default
  /sbin/route add default gw 192.168.27.${GW} 
fi
You may have to tweak the code to get it to run right as I've not tested it but it should be an idea to get you in the right direction.
 
1 members found this post helpful.
Old 04-25-2014, 03:55 AM   #5
srinivasanece
LQ Newbie
 
Registered: Mar 2012
Location: Komarapalayam,TamilNadu,INDIA
Distribution: CentOS,FreeBSD,RedHatLinux
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TenTenths View Post
First put [code] tags around your scripts.

Code:
#!/bin/sh

 ping -q -c5 yahoo.com > /dev/null

 if [ $? -eq 0 ]
 then
   echo "Route Ok"
 else
   echo "Changing Route"
   /sbin/route del default
   /sbin/route add default gw 192.168.27.101 
   if [ $? -eq 0 ]
   then
     echo "Route Ok"
   else
     echo "Changing Route"
     /sbin/route del default
     /sbin/route add default gw 192.168.27.100 
   fi
fi
Secondly, your script will change the gateway to 101 and then if changing the gateway isn't successful it'll change it to 100, not what I think you want to achieve.

Do you have a preferred gateway or do you only want to switch gateways when there is a failure?

If you're only interested in switching when there's a failure you could do something like:

Code:
#!/bin/bash
ping -q -c5 yahoo.com > /dev/null

if [ $? -eq 0 ] ; then
  echo "Route Ok"
else
  echo "Changing Route"
  GW=$(/sbin/route | grep default | awk -F. {'print $4'})  # Find out what the last octet of the current default route
  if [ ${GW} -eq "100" ] ; then                      # If it's 100
    GW="101"                                         # we change it to 101
  else                                               # If it's not 100
    GW="100"                                         # we change it t0 100
  fi
  /sbin/route del default
  /sbin/route add default gw 192.168.27.${GW} 
fi
You may have to tweak the code to get it to run right as I've not tested it but it should be an idea to get you in the right direction.
Yeah You got it right.Thank You So much.

Last edited by srinivasanece; 04-25-2014 at 04:28 AM.
 
Old 04-25-2014, 04:30 AM   #6
srinivasanece
LQ Newbie
 
Registered: Mar 2012
Location: Komarapalayam,TamilNadu,INDIA
Distribution: CentOS,FreeBSD,RedHatLinux
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by srinivasanece View Post
Yeah You got it right.Thank You So much.


#!/bin/bash
ping -q -c1 192.168.28.11 > /dev/null

if [ $? -eq 0 ] ; then
echo "Route Ok"
else
echo "Changing Route"
GW=$(/sbin/route | grep default | awk -F. {'print $4'})|cut -d ' ' -f1 # Find out what the last octet of the current default route
echo $GW |cut -d ' ' -f1
if [ ${GW} -eq "100" ] ; then # If it's 100
GW="101" # we change it to 101
else # If it's not 100
GW="100" # we change it t0 100
fi
/sbin/route del default
/sbin/route add default gw 192.168.27.${GW}
fi

When i run the above script i get an error.

line 10: [: -eq: unary operator expected

---------- Post added 04-25-14 at 03:01 PM ----------

Quote:
Originally Posted by TenTenths View Post
First put [code] tags around your scripts.

Code:
#!/bin/sh

 ping -q -c5 yahoo.com > /dev/null

 if [ $? -eq 0 ]
 then
   echo "Route Ok"
 else
   echo "Changing Route"
   /sbin/route del default
   /sbin/route add default gw 192.168.27.101 
   if [ $? -eq 0 ]
   then
     echo "Route Ok"
   else
     echo "Changing Route"
     /sbin/route del default
     /sbin/route add default gw 192.168.27.100 
   fi
fi
Secondly, your script will change the gateway to 101 and then if changing the gateway isn't successful it'll change it to 100, not what I think you want to achieve.

Do you have a preferred gateway or do you only want to switch gateways when there is a failure?

If you're only interested in switching when there's a failure you could do something like:

Code:
#!/bin/bash
ping -q -c5 yahoo.com > /dev/null

if [ $? -eq 0 ] ; then
  echo "Route Ok"
else
  echo "Changing Route"
  GW=$(/sbin/route | grep default | awk -F. {'print $4'})  # Find out what the last octet of the current default route
  if [ ${GW} -eq "100" ] ; then                      # If it's 100
    GW="101"                                         # we change it to 101
  else                                               # If it's not 100
    GW="100"                                         # we change it t0 100
  fi
  /sbin/route del default
  /sbin/route add default gw 192.168.27.${GW} 
fi
You may have to tweak the code to get it to run right as I've not tested it but it should be an idea to get you in the right direction.
#!/bin/bash
ping -q -c1 192.168.28.11 > /dev/null

if [ $? -eq 0 ] ; then
echo "Route Ok"
else
echo "Changing Route"
GW=$(/sbin/route | grep default | awk -F. {'print $4'})|cut -d ' ' -f1 # Find out what the last octet of the current default route
echo $GW |cut -d ' ' -f1
if [ ${GW} -eq "100" ] ; then # If it's 100
GW="101" # we change it to 101
else # If it's not 100
GW="100" # we change it t0 100
fi
/sbin/route del default
/sbin/route add default gw 192.168.27.${GW}
fi

When i run the above script i get an error.

line 10: [: -eq: unary operator expected
 
Old 04-25-2014, 04:42 AM   #7
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Try this instead to get the last octet of the gateway:

Code:
GW=$(/sbin/route -n | grep "^0.0" | awk {'print $2'} | awk -F"." {'print $4'})
Then add
Code:
set -x
as the second line of the script and see what happens.
 
1 members found this post helpful.
Old 04-25-2014, 05:11 AM   #8
srinivasanece
LQ Newbie
 
Registered: Mar 2012
Location: Komarapalayam,TamilNadu,INDIA
Distribution: CentOS,FreeBSD,RedHatLinux
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TenTenths View Post
Try this instead to get the last octet of the gateway:

Code:
GW=$(/sbin/route -n | grep "^0.0" | awk {'print $2'} | awk -F"." {'print $4'})
Then add
Code:
set -x
as the second line of the script and see what happens.
Thank You So Much.It is working as expected.

Love this Forum
 
  


Reply



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
LXer: Scripting the Linux desktop, Part 2: Scripting Nautilus LXer Syndicated Linux News 0 02-17-2011 04:02 AM
Firefox Scripting Add-on (Scripting HTML / Javascript inside Firefox) linuxbeatswindows Programming 1 09-18-2009 10:09 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
Need help scripting Tamara Programming 1 06-05-2005 03:18 PM
Scripting? eXor Linux - Newbie 3 06-15-2004 06:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:11 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration