小編給大家分享一下怎么用shell腳本實現(xiàn)自動切換內(nèi)網(wǎng)和外網(wǎng),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
首先說明下我們的配置文件,都是類似格式的,假如內(nèi)網(wǎng)是192.168.0.3,外網(wǎng)是123.123.123.123,配置文件如下:
$db['salver']['hostname'] = '192.168.0.3:3306';
//$db['salver']['hostname'] = '123.123.123.123:3306';
這個時候就是外網(wǎng)是注釋的狀態(tài)。走的是內(nèi)網(wǎng)。
說下思路,就是一個配置文件,復(fù)制出來2分放到別的地方,這兩份一個里面設(shè)置的是內(nèi)網(wǎng),一個里面設(shè)置的是外網(wǎng),如果內(nèi)網(wǎng)不通的情況下,判斷當(dāng)前用的是內(nèi)網(wǎng)還是外網(wǎng),如果是內(nèi)網(wǎng)的情況下,就把外網(wǎng)的配置文件復(fù)制到網(wǎng)站文件里,如果內(nèi)網(wǎng)通了,就把內(nèi)網(wǎng)的配置文件再復(fù)制到網(wǎng)站文件里。就相當(dāng)于是內(nèi)網(wǎng)IP是主,外網(wǎng)IP是備一樣。把這個腳本放在計劃任務(wù)里,1分鐘執(zhí)行一次。
代碼如下:
#!/bin/bash
#主從數(shù)據(jù)庫的內(nèi)外網(wǎng)IP
master_IP_N=
master_IP_W=
salver_IP_N=
salver_IP_W=
#mongo的內(nèi)外網(wǎng)IP
mongo_IP_W=
mongo_IP_N=
#redis的主從內(nèi)外網(wǎng)IP
redis_master_IP_W=
redis_master_IP_N=
redis_slave_IP_W=
redis_slave_IP_N=
#sphinx的內(nèi)外網(wǎng)IP
sphinx_IP_W=
sphinx_IP_N=
#網(wǎng)站配置文件的位置
database_path=/home/caoy/database.php
mongo_path=/home/caoy/over_sea_stock.php
redis_path=/home/caoy/Rediska_connector.php
sphinx_path=/home/caoy/SphinxConnector.php
config_path=/data/check_ip_config/
#檢測當(dāng)前使用的網(wǎng)卡
function check_ip(){
IP_config=`cat $1 |grep $2 | cut -c1-2`
if [ "$IP_config" == "//" ];then
ip_status=1
else
ip_status=0
fi
}
#判斷內(nèi)網(wǎng)網(wǎng)卡是否故障
function check_network(){
nmap -sP $1 | grep "1 host up" >/dev/null || (sleep 5 ; nmap -sP $1) | grep "1 host up" >/dev/null
#ping $1 -c 5 > /dev/null
if [[ "$?" == "1" ]]; then
check_ip $database_path $1
if [[ "$ip_status" == "0" ]];then
cp -r $config_path/database_w.php $database_path
echo "cp database_w.php is ok!"
fi
check_ip $mongo_path $1
if [[ "$ip_status" == "0" ]];then
cp -r $config_path/over_sea_stock_w.php $mongo_path
echo "cp over_sea_stock_w.php is ok!"
fi
check_ip $redis_path $1
if [[ "$ip_status" == "0" ]];then
cp -r $config_path/Rediska_connector_w.php $redis_path
echo "cp Rediska_connector_w.php is ok!"
fi
check_ip $sphinx_path $1
if [[ "$ip_status" == "0" ]];then
cp -r $config_path/SphinxConnector_w.php $sphinx_path
echo "cp SphinxConnector_w.php is ok!"
fi
else
check_ip $database_path $1
if [[ "$ip_status" == "1" ]];then
cp -r $config_path/database_n.php $database_path
echo "cp database_n.php is ok!"
fi
check_ip $mongo_path $1
if [[ "$ip_status" == "1" ]];then
cp -r $config_path/over_sea_stock_n.php $mongo_path
echo "cp over_sea_stock_n.php is ok!"
fi
check_ip $redis_path $1
if [[ "$ip_status" == "1" ]];then
cp -r $config_path/Rediska_connector_n.php $redis_path
echo "cp Rediska_connector_n.php is ok!"
fi
check_ip $sphinx_path $1
if [[ "$ip_status" == "1" ]];then
cp -r $config_path/SphinxConnector_n.php $sphinx_path
echo "cp SphinxConnector_n.php is ok!"
fi
fi
}
#檢測各個網(wǎng)卡是否通暢
check_network $master_IP_N
check_network $salver_IP_N
check_network $mongo_IP_N
check_network $redis_master_IP_N
check_network $redis_slave_IP_N
check_network $sphinx_IP_N
以上是“怎么用shell腳本實現(xiàn)自動切換內(nèi)網(wǎng)和外網(wǎng)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
文章標(biāo)題:怎么用shell腳本實現(xiàn)自動切換內(nèi)網(wǎng)和外網(wǎng)-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://aaarwkj.com/article22/ppocc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、響應(yīng)式網(wǎng)站、定制開發(fā)、手機(jī)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容