Linux uses Shell to automatically switch gateways

Source: Internet
Author: User

Write the name Auto_change_ gw.sh script, used to automatically switch the host gateway, the instructions are as follows: 1) There are 2 available gateway addresses, respectively, the primary gateway 192.168.10.254 and the standby gateway 192.168.10.253, the default gateway currently used is 192.168.10.254;2) if the gateway address 192 is currently used .168.10.254 a problem, automatically switch the gateway address to 192.168.10.253;3) when the gateway address 192.168.10.254 can work properly, the current host then switches the gateway address to 192.168.10.254. Talk about the idea: 1, first use the while  cycle to build a dead loop, in order to let the script forever cycle, because to test the main gateway can be connected while :d Odone2, into the dead loop, in the dead loop inside again nested two loop the first loop while loop, Is ping the main gateway, can ping to continue ping,ping loop end. Go into the second loop before entering the second loop to delete the default gateway, replace the alternate gateway 3, enter the second loop, is the until loop, continue to ping the main gateway, Ping will continue to ping,ping the end of the loop, notice the difference with the first loop at this time because it is the cause of the cycle of death, Start the first loop again, before entering the loop, you need to remove the default gateway and replace it with the primary gateway, which is why the two commands are written at the beginning [[Email protected] shell]# vim auto_change_ gw.sh  1 #!/bin/bash 2 gw1=192.168.10.253 3 gw2=192.168.10.251 4  ( 5 while :    //into the dead loop  6 do       Operation of     //into the dead loop  7        route del  default         8        route add default gw  $GW 1  //Delete the default gateway and set GW1 as the default gateway. These two commands are primarily for operations after the end of the second loop  9        while ping -c 1   $GW 1 &> /dev/null    //Test If you can ping the primary gateway. While loop is the test condition will continue to loop, that is, continue to ping 10                 do      11                         sleep 1       //ping successfully sleeps for one second 12                 done               //ping End Loop 13        route del  Default    14        route add default gw  $GW 2           //the operation after the first loop is completed, delete the default gateway, and set GW2 as the default gateway 15        until  ping -c 1  $GW 1 &> /dev/null  //into the second loop to continue pinging the primary gateway. Until loop is the test condition is not established will continue to cycle, that is, Ping does not pass continue ping16                 do      17                         sleep  1            //ping will sleep for a second. And then Ping18                done.                   // Ping through the loop, and then back into the dead loop operation. is to remove the default gateway and GW1 as the default gateway 19 done20 )  &    //Add the commands in the script to the background execution 


This article is from the "Sunset" blog, please be sure to keep this source http://whluwit.blog.51cto.com/2306565/1438147

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.