当服务器有电信、移动双线时,并且都需要直接对外提供服务,服务器需要做路由跟踪。电信用户访问电信线路,并从电信网关返回;移动用户访问移动线路,并从移动网关返回。服务器主动外连,走默认网关。

      可以使用如下脚本进行配置,使用方法为:

     IpRoute editer by K.C   Date:Dec 09 2010 18:15:50
    ./IpRoute.sh [help] driver0:line:lineView:lineGateWay  driver1:line:lineView:lineGateWay
   ./IpRoute.sh [help] 設備0:線路簡稱0:線路描述0:線路網關0 設備1:線路簡稱1:線路描述1:線路網關1

例:電信+移動
./IpRoute.sh eth0:TEL:TELCOME:x.x.x.x eth1:CM:CMCC:x.x.x.x

 

 


1. #!/bin/bash 
2. #定義配置文件 
3. if [ ${1} = "help" ];then
4. "IpRoute editer by K.C  Date:Dec 09 2010 18:15:50\t"
5. "${0} [help] driver0:line:lineView:lineGateWay driver1:line:lineView:lineGateWay\t"
6. "${0} [help] 設備0:線路簡稱0:線路描述0:線路網關0 設備1:線路簡稱1:線路描述1:線路網關1 \n"
7. "例:電信+移動\t"
8. "${0} eth0:TEL:TELCOME:x.x.x.x eth1:CM:CMCC:x.x.x.x"
9.         exit 
10. fi 
11. rtTables="/etc/iproute2/rt_tables"
12. GWFile="/etc/sysconfig/network"
13. if [ ! -f ${rtTables} ];then
14. "${rtTables} 文件不存在,請確認OS是否為RHEL linux ."
15.         exit 2 
16. fi 
17. scriptsPath="/root/scripts"
18. flushRoute="${scriptsPath}/flushRoute.sh"
19. if [ ! -d "${scriptsPath}" ];then
20. "Make a scripts dir..\n"
21.         mkdir ${scriptsPath} 
22. fi 
23. #配置文件是否已被修改 
24. rtTablesTemp=`tail -1 ${rtTables}|awk '{print $2}'` 
25. if [ ${rtTablesTemp} != "inr.ruhep" ];then
26. "請確認本機是否已做過IPROUTE."
27.         exit 2 
28. fi 
29. if [ ! -n "${1}" ];then
30. "請輸入第一塊網卡參數,如 eth0:tel:telcom:x.x.x.x"
31.         exit 2 
32. else
33. "${1}"|awk -F":" '{print NF}'` 
34. "4" ];then
35. "你輸入的第一塊網卡參數(${1})有誤,請按以下格式重新輸入 設備:線路簡稱:線路描述:線路網關 .如電信線路請輸入 eth0:TEL:TELCOME:x.x.x.x\n"
36. else
37. ":" '{print $1}'` 
38. "inet addr"|awk '{print $2}'|awk -F ":" '{print $2}'` 
39. "${eth0IP}" ];then
40. "獲取設備IP失敗,請檢查輸入參數是否為 設備:線路簡稱:線路描述:線路網關 ,eth0:TEL:TELCOME:x.x.x.x"
41.                         exit 2 
42.                 fi 
43. ":" '{print $2}'` 
44. ":" '{print $3}'` 
45. ":" '{print $4}'` 
46.         fi 
47.  
48. fi 
49. if [ ! -n "$2" ];then
50. "請輸入第二塊網卡參數,如 eth1:tel:telcom:x.x.x.x"
51.         exit 2 
52. else
53. "${2}"|awk -F":" '{print NF}'` 
54. "4" ];then
55. "你輸入的第二塊網卡參數(${2})有誤,請按以下格式重新輸入 設備:線路簡稱:線路描述:線路網關 .如移動線路請輸入 eth1:CM:CMCC:x.x.x.x\n"
56. else
57. ":" '{print $1}'` 
58. "inet addr"|awk '{print $2}'|awk -F ":" '{print $2}'` 
59. "${eth1IP}" ];then
60. "獲取設備IP失敗,請檢查輸入參數是否為 設備:線路簡稱:線路描述:線路網關 ,eth1:CM:CMCC:x.x.x.x"
61.                         exit 2 
62.                 fi 
63. ":" '{print $2}'` 
64. ":" '{print $3}'` 
65. ":" '{print $4}'` 
66.         fi 
67. fi 
68. #獲取默認網關 
69. GW=`cat ${GWFile}|grep "GATEWAY"|awk -F "=" '{print $2}'` 
70. if [ ! -n "${GW}" ];then
71. "獲取默認網關失敗,請檢查 ${GWFile}"
72.         exit 2 
73. fi 
74. echo -e "請確認以下信息:\n"
75. echo -e "第一塊網卡設備名稱: ${eth0Dr}\t"
76. echo -e "第一塊網卡IP: ${eth0IP}\t"
77. echo -e "第一塊綱卡線路簡稱: ${eth0Line}\t"
78. echo -e "第一塊綱卡線路描述: ${eth0LineView}\t"
79. echo -e "第一塊綱卡網關: ${eth0GW}\n"
80.  
81. echo -e "第二塊網卡設備名稱: ${eth1Dr}\t"
82. echo -e "第二塊網卡IP: ${eth1IP}\t"
83. echo -e "第二塊綱卡線路簡稱: ${eth1Line}\t"
84. echo -e "第二塊綱卡線路描述: ${eth1LineView}\t"
85. echo -e "第二塊綱卡網關: ${eth1GW}\n"
86.  
87. echo -e "默認網關: ${GW}\n"
88. #創建路由刷新腳本 
89. echo "#!/bin/bash"
90. echo "ip route flush table ${eth0Line}"
91. echo "ip route flush table ${eth1Line}"
92. echo "ip route add default via ${eth0GW} dev $eth0Dr src $eth0IP table ${eth0Line}"
93. echo "ip rule add from ${eth0IP} table ${eth0Line}"
94. echo "ip route add default via ${eth1GW}  dev $eth1Dr src $eth1IP table ${eth1Line}"
95. echo "ip rule add from ${eth1IP} table ${eth1Line}"
96. chmod 755 ${flushRoute} 
97.  
98. printf "請輸入\"yes\"確認信息並執行以下命令進行修改系統網絡,輸入\"no\"放棄修改系統網絡:"
99. cat ${flushRoute}|grep -v "\#"
100. read key
101. case "${key}" in
102. yes) 
103.         #寫入配置文件 
104. "251    ${eth0Line}     ${eth0LineView}"
105. "252    ${eth1Line}     ${eth1LineView}"
106.         #刷新路由 
107.         ${flushRoute} 
108. local|grep "${flushRoute}"` 
109. "${rclocal}" ];then
110. "${flushRoute}" >> /etc/rc.local
111.         fi 
112. ;; 
113. no) 
114. "以上信息沒有得到你的確認,退出本次網絡修改計劃.\t"
115.         exit 2 
116. ;; 
117. *) 
118. "你的輸入有誤,本次執行已結束,若需繼續,請再次執行."
119. ;; 
120. esac


转载于:https://blog.51cto.com/kc1985/506463