|
|
|
|
移动端

3.3.3 利用Shell脚本简化Linux逻辑节点的搬迁(1)

《运维前线:一线运维专家的运维方法、技巧与实践》第3章数据中心搬迁中的x86自动化运维,本章主要介绍在大型数据中心搬迁的过程中,如何利用自行编制的各类脚本,低成本、高效率又准确地完成大量节点的逻辑搬迁工作。本节为大家介绍利用Shell脚本简化Linux逻辑节点的搬迁。

作者:云技术社区来源:机械工业出版社|2017-04-24 12:54

有奖调研 | 1TB硬盘等你拿 AI+区块链的发展趋势及应用调研


3.3.3 利用Shell脚本简化Linux逻辑节点的搬迁(1)

下面是基于Linux Shell的脚本,主要实现功能为判断应用环境(研发和容灾),执行不同的Shell指令,将字典文件中的旧IP切换为对应的新IP:

  1. #!/bin/bash  
  2. cd /tmp/ipchange  
  3. #dos2unix ipchange.sh  
  4. #dos2unix ipcheck.txt  
  5.  
  6. echo ++++++++++++++++++++++++++++++++++++++++++++++++++++  
  7. echo + Welcome use redhat linux change patameter script +  
  8. echo ++++++++++++++++++++++++++++++++++++++++++++++++++++  
  9.  
  10. read -p "Disaster Recovery Environment(R)|Develop Testing  
  11. Environment(T)|Exit(E):" evar  
  12. # 以上语句显示欢迎页面,同时还显示应用环境菜单,根据操作的选取,可跳转到不同的环境  
  13. zb_ntp1=192.168.16.105                              #(为NTP1服务器赋予指定IP)  
  14. zb_ntp2=192.168.20.73                               #(为NTP2服务器赋予指定IP)  
  15. zb_dns1=192.168.16.105                              #(为主DNS服务器赋予指定IP)  
  16. zb_dns2=192.168.16.106                              #(为辅DNS服务器赋予指定IP)  
  17. zb_yum=                                             #(初始化YUM服务器变量)  
  18. # 以上语句设置灾备环境的所有参数  
  19.  
  20. kfcs_ntp1=192.168.127.103                           #(为NTP1服务器赋予指定IP)  
  21. kfcs_ntp2=                                             #(初始化NTP2服务器变量)  
  22. kfcs_dns1=192.168.127.105                           #(为主DNS服务器赋予指定IP)  
  23. kfcs_dns2=192.168.127.106                           #(为辅DNS服务器赋予指定IP)  
  24. kfcs_yum=                                           #(初始化YUM服务器变量)  
  25. # 以上语句设置开发环境的所有参数  
  26.  
  27. dr(){                                               #(设置灾备环境的主程序)  
  28. ################################  
  29. # Disaster Recovery Environment #  
  30. ################################  
  31. # 保存旧IP信息  
  32. grep -i ipaddr /etc/sysconfig/network-scripts/ifcfg-eth0>/tmp/oldip.txt  
  33. awk -F'{print $2}' /tmp/oldip.txt>/tmp/ipchange/oldip.txt  
  34. rm -f /tmp/oldip.txt  
  35. oldip=`awk '{print $0}' /tmp/ipchange/oldip.txt`  
  36. echo oldip=$oldip  
  37. echo  
  38.  
  39. # 保存旧网关信息  
  40. grep -i gateway  
  41. /etc/sysconfig/network-scripts/ifcfg-eth0>/tmp/oldgatewayip.txt  
  42. awk -F'{print $2}' /tmp/oldgatewayip.txt>/tmp/ipchange/oldgatewayip.txt  
  43. rm -f /tmp/oldgatewayip.txt  
  44. oldgatewayip=`awk '{print $0}' /tmp/ipchange/oldgatewayip.txt`  
  45. echo oldgatewayip=$oldgatewayip  
  46. echo  
  47.  
  48. # 从字典文件中截取新旧IP比对值,提取新IP  
  49. grep "$oldip\>" /tmp/ipchange/ipcheck.txt>/tmp/oldtonewip.txt  
  50. awk '{print $2}' /tmp/oldtonewip.txt>/tmp/ipchange/newip.txt  
  51. mv /tmp/oldtonewip.txt /tmp/ipchange/oldtonewip.txt  
  52. newip=`awk '{print $1}' /tmp/ipchange/newip.txt`  
  53. echo newip=$newip  
  54. echo  
  55.  
  56. # 在字典文件中进行比对,如果找不到IP对应关系则退出  
  57. count=`grep "$oldip\>" /tmp/ipchange/ipcheck.txt |wc –l`  
  58. if [ $count -eq 0 ];then  
  59. echo "no ip matched in ipcheck.txt, will exit in 5 seconds!!!"  
  60. sleep 5  
  61. exit 100  
  62. fi  
  63. grep "$oldip\>" /tmp/ipchange/ipcheck.txt>/tmp/oldtonewip.txt  
  64. awk '{print $2}' /tmp/oldtonewip.txt>/tmp/ipchange/newip.txt  
  65. mv /tmp/oldtonewip.txt /tmp/ipchange/oldtonewip.txt  
  66. newip=`awk '{print $1}' /tmp/ipchange/newip.txt`  
  67. echo newip=$newip  
  68. echo  
  69.  
  70. # 定义新网关  
  71. awk -F. '{print $1"."$2"."$3"."254}'  
  72. /tmp/ipchange/newip.txt>/tmp/ipchange/newgatewayip.txt  
  73. newgatewayip=`awk '{print $1}' /tmp/ipchange/newgatewayip.txt`  
  74. echo newgatewayip=$newgatewayip  
  75. echo  
  76.  
  77. #保存网卡的旧配置,并设置新定义的配置信息  
  78. cd /etc/sysconfig/network-scripts/  
  79. cp ifcfg-eth0 /tmp/ipchange/ifcfg-eth0.old  
  80. sed -e "s/$oldip/$newip/g" ifcfg-eth0>ifcfg-eth0.tmp1  
  81. sed -e "s/$oldgatewayip/$newgatewayip/g" ifcfg-eth0.tmp1>ifcfg-eth0.tmp2  
  82. sed -e "/NETMASK/d" ifcfg-eth0.tmp2>ifcfg-eth0.tmp1  
  83. sed -e "/IPADDR/a\\NETMASK=255.255.255.0" ifcfg-eth0.tmp1>ifcfg-eth0.tmp2  
  84.  
  85. rm -f ifcfg-eth0  
  86. cp ifcfg-eth0.tmp2 /tmp/ipchange/ifcfg-eth0.new  
  87. cp ifcfg-eth0.tmp2 ifcfg-eth0  
  88. rm -f ifcfg-eth0.tmp1  
  89. rm -f ifcfg-eth0.tmp2  
  90.  
  91. cat /etc/sysconfig/network-scripts/ifcfg-eth0  
  92. #################################################################  
  93. # 备份hosts文件  
  94. cp -f /etc/hosts /tmp/ipchange/oldhosts.txt  
  95.  
  96. # 根据字典定义的文件,替换hosts内关联节点对应的IP  
  97. for ip in $(cat /etc/hosts |egrep ^[0-9] |awk '{print $1}'|grep -v 127.0.0.1)  
  98. do  
  99. iprule=$(grep $ip /tmp/ipchange/ipcheck.txt |head -n 1)  
  100. if [ "$iprule" != "" ]  
  101. then  
  102. shipsec=$(echo $iprule |awk '{print $1}' |awk -F'.' '{print $1"."$2"."$3}')  
  103. cdipsec=$(echo $iprule |awk '{print $2}' |awk -F'.' '{print $1"."$2"."$3}')  
  104. echo "shipsec:$shipsec"  
  105. echo "cdipsec:$cdipsec"  
  106. sed -i "s/^$shipsec/$cdipsec/g" /etc/hosts  
  107. else  
  108. echo "WARNING: ip $ip not matched in ipcheck.txt"  
  109. fi  
  110. done  
  111.  
  112. #################################################################  
  113. # 设置NTP信息(通过crontab定时更新)  
  114. crontab -l>/tmp/crontab.old  
  115. if [ -f /var/spool/cron/root ]  
  116. then  
  117. sed -i '/ntpdate/d' /var/spool/cron/root  
  118. fi  
  119. echo "*/5 * * * * /sbin/ntpdate $zb_ntp1">>/var/spool/cron/root  
  120. echo "*/5 * * * * /sbin/ntpdate $zb_ntp2">>/var/spool/cron/root  
  121.  
  122. echo "tinker panic 0">/etc/ntp.conf  
  123. echo "restrict 127.0.0.1">>/etc/ntp.conf  
  124. echo "restrict default kod nomodify notrap">>/etc/ntp.conf  
  125. echo "server $zb_ntp1">>/etc/ntp.conf  
  126. echo "server $zb_ntp2">>/etc/ntp.conf  
  127. echo "keys /etc/ntp/keys">>/etc/ntp.conf  
  128. echo "driftfile /var/lib/ntp/drift">>/etc/ntp.conf  
  129.  
  130. echo "$zb_ntp1">/etc/ntp/ntpservers  
  131. echo "$zb_ntp2">>/etc/ntp/ntpservers  
  132. echo "clock.redhat.com">>/etc/ntp/ntpservers  
  133. echo "clock2.redhat.com">>/etc/ntp/ntpservers  
  134. echo "$zb_ntp1">/etc/ntp/step-tickers  
  135. echo "$zb_ntp2">>/etc/ntp/step-tickers  
  136. echo "clock.redhat.com">>/etc/ntp/step-tickers  
  137. echo "clock2.redhat.com">>/etc/ntp/step-tickers  
  138.  
  139. ntp=`cat /etc/ntp/ntpservers | head -1`  
  140. crontab –l  
  141.  
  142. ##################################################################  
  143. # 设置DNS信息  
  144. cp /etc/resolv.conf /tmp/ipchange/resolv.conf.old  
  145. echo domain cpic.com.cn>/etc/resolv.conf  
  146. echo nameserver $zb_dns1>>/etc/resolv.conf  
  147. echo nameserver $zb_dns2>>/etc/resolv.conf  
  148.  
  149. dns=`sed -e '/nameserver/g' /etc/resolv.conf | awk -F " " '{print $2}'  
  150. /etc/resolv.conf | head -2 | tail -1`  
  151.  
  152. ###################################################################  
  153. # 设置监控平台参数  
  154. mv /home/patrol/startagent.sh /tmp/startagent.sh.old  
  155. echo su - patrol -c \"/home/patrol/Patrol3/PatrolAgent -id  
  156. $newip\">/home/patrol/startagent.sh  
  157. chown patrol:patrol /home/patrol/startagent.sh  
  158. chmod 774 /home/patrol/startagent.sh  
  159.  
  160. ####################################################################  
  161. # 重置关键用户密码  
  162. #echo ???????? | passwd root --stdin     ????????请用实际密码代替  
  163. #echo ******** | passwd patrol --stdin   ********请用实际密码代替  
  164. ###################################################################  
  165. #shutdown computer  
  166. #shutdown -h now                      (shutdown前标注了#,所以此句只标注不执行)  
  167. service network restart  
  168. service ntpd stop  
  169. chkconfig ntpd off  
  170. echo "New IP is $newip"  
  171. echo "New primary DNS is $dns"  
  172. echo "New primary NTP is $ntp"  
  173. echo "Change is successful!"  
  174. }  
  175. # 以上语句在设置完所有配置信息后,需要重启网络服务,以确保新IP能够正常工作。  
  176.  
  177. dt(){                                                #(设置开发环境的主程序)  
  178. ####################################################  
  179. # 保存旧IP信息  
  180. grep -i ipaddr /etc/sysconfig/network-scripts/ifcfg-eth0>/tmp/oldip.txt  
  181. awk -F'{print $2}' /tmp/oldip.txt>/tmp/ipchange/oldip.txt  
  182. rm -f /tmp/oldip.txt  
  183. oldip=`awk '{print $0}' /tmp/ipchange/oldip.txt`  
  184. echo oldip=$oldip  
  185. echo  
  186.  
  187. # 保存旧网关信息  
  188. grep -i gateway  
  189. /etc/sysconfig/network-scripts/ifcfg-eth0>/tmp/oldgatewayip.txt  
  190. awk -F'{print $2}' /tmp/oldgatewayip.txt>/tmp/ipchange/oldgatewayip.txt  
  191. rm -f /tmp/oldgatewayip.txt  
  192. oldgatewayip=`awk '{print $0}' /tmp/ipchange/oldgatewayip.txt`  
  193. echo oldgatewayip=$oldgatewayip  
  194. echo  
  195.  
  196. # 从字典中文件中截取新旧IP比对值,提取新IP  
  197. grep "$oldip\>" /tmp/ipchange/ipcheck.txt>/tmp/oldtonewip.txt  
  198. awk '{print $2}' /tmp/oldtonewip.txt>/tmp/ipchange/newip.txt  
  199. mv /tmp/oldtonewip.txt /tmp/ipchange/oldtonewip.txt  
  200. newip=`awk '{print $1}' /tmp/ipchange/newip.txt`  
  201. echo newip=$newip  
  202. echo  
  203.  
  204. # 在字典文件中比对,如果找不到IP对应关系则退出  
  205. count=`grep "$oldip\>" /tmp/ipchange/ipcheck.txt |wc –l`  
  206. if [ $count -eq 0 ];then  
  207. echo "no ip matched in ipcheck.txt, will exit in 5 seconds!!!"  
  208. sleep 5  
  209. exit 100  
  210. fi  
  211. grep "$oldip\>" /tmp/ipchange/ipcheck.txt>/tmp/oldtonewip.txt  
  212. awk '{print $2}' /tmp/oldtonewip.txt>/tmp/ipchange/newip.txt  
  213. mv /tmp/oldtonewip.txt /tmp/ipchange/oldtonewip.txt  
  214. newip=`awk '{print $1}' /tmp/ipchange/newip.txt`  
  215. echo newip=$newip  
  216. echo  
  217.  
  218. # 定义新网关  
  219. awk -F. '{print $1"."$2"."$3"."254}'  
  220. /tmp/ipchange/newip.txt>/tmp/ipchange/newgatewayip.txt  
  221. newgatewayip=`awk '{print $1}' /tmp/ipchange/newgatewayip.txt`  
  222. echo newgatewayip=$newgatewayip  
  223. echo  
  224.  
  225. #保存网卡的旧配置,并设置新定义的配置信息  
  226. cd /etc/sysconfig/network-scripts/  
  227. cp ifcfg-eth0 /tmp/ipchange/ifcfg-eth0.old  
  228. sed -e "s/$oldip/$newip/g" ifcfg-eth0>ifcfg-eth0.tmp1  
  229. sed -e "s/$oldgatewayip/$newgatewayip/g" ifcfg-eth0.tmp1>ifcfg-eth0.tmp2  
  230. sed -e "/NETMASK/d" ifcfg-eth0.tmp2>ifcfg-eth0.tmp1  
  231. sed -e "/IPADDR/a\\NETMASK=255.255.255.0" ifcfg-eth0.tmp1>ifcfg-eth0.tmp2  
  232.  
  233. rm -f ifcfg-eth0  
  234. cp ifcfg-eth0.tmp2 /tmp/ipchange/ifcfg-eth0.new  
  235. cp ifcfg-eth0.tmp2 ifcfg-eth0  
  236. rm -f ifcfg-eth0.tmp1  
  237. rm -f ifcfg-eth0.tmp2  
  238.  
  239. cat /etc/sysconfig/network-scripts/ifcfg-eth0  
  240. #################################################################  
  241. # 备份hosts文件  
  242. cp -f /etc/hosts /tmp/ipchange/oldhosts.txt  
  243.  
  244. for ip in $(cat /etc/hosts |egrep ^[0-9] |awk '{print $1}'|grep -v 127.0.0.1)  
  245. do  
  246. iprule=$(grep $ip /tmp/ipchange/ipcheck.txt |head -n 1)  
  247. if [ "$iprule" != "" ]  
  248. then  
  249. shipsec=$(echo $iprule |awk '{print $1}' |awk -F'.' '{print $1"."$2"."$3}')  
  250. cdipsec=$(echo $iprule |awk '{print $2}' |awk -F'.' '{print $1"."$2"."$3}')  
  251. echo "shipsec:$shipsec"  
  252. echo "cdipsec:$cdipsec"  
  253. sed -i "s/^$shipsec/$cdipsec/g" /etc/hosts  
  254. else  
  255. echo "WARNING: ip $ip not matched in ipcheck.txt"  
  256. fi  
  257. done  
  258.  
  259. #################################################################  
  260. # 设置NTP信息(通过crontab定时更新)  
  261. crontab -l>/tmp/crontab.old  
  262. if [ -f /var/spool/cron/root ]  
  263. then  
  264. sed -i '/ntpdate/d' /var/spool/cron/root  
  265. fi  
  266. echo "*/5 * * * * /sbin/ntpdate $kfcs_ntp1">>/var/spool/cron/root  
  267. #echo "*/5 * * * * /sbin/ntpdate $kfcs_ntp2">>/var/spool/cron/root  
  268.  
  269. echo "tinker panic 0">/etc/ntp.conf  
  270. echo "restrict 127.0.0.1">>/etc/ntp.conf  
  271. echo "restrict default kod nomodify notrap">>/etc/ntp.conf  
  272. echo "server $kfcs_ntp1">>/etc/ntp.conf  
  273. echo "server $kfcs_ntp2">>/etc/ntp.conf  
  274. echo "keys /etc/ntp/keys">>/etc/ntp.conf  
  275. echo "driftfile /var/lib/ntp/drift">>/etc/ntp.conf  
  276.  
  277. echo "$kfcs_ntp1">/etc/ntp/ntpservers  
  278. echo "$kfcs_ntp2">>/etc/ntp/ntpservers  
  279. echo "clock.redhat.com">>/etc/ntp/ntpservers  
  280. echo "clock2.redhat.com">>/etc/ntp/ntpservers  
  281. echo "$kfcs_ntp1">/etc/ntp/step-tickers  
  282. echo "$kfcs_ntp2">>/etc/ntp/step-tickers  
  283. echo "clock.redhat.com">>/etc/ntp/step-tickers  
  284. echo "clock2.redhat.com">>/etc/ntp/step-tickers  
  285.  
  286. ntp=`cat /etc/ntp/ntpservers | head -1`  
  287.  
  288.  
  289. rontab -l  
  290. ##################################################################  
  291. # 设置DNS信息  
  292. cp /etc/resolv.conf /tmp/ipchange/resolv.conf.old  
  293. > /etc/resolv.conf  
  294. dns=`sed -e '/nameserver/g' /etc/resolv.conf | awk -F " " '{print $2}' /etc/  
  295. resolv.conf | head -2 | tail -1`  
  296.  
  297. ###################################################################  
  298. # 设置监控平台参数  
  299. mv /home/patrol/startagent.sh /tmp/startagent.sh.old  
  300. echo su - patrol -c \"/home/patrol/Patrol3/PatrolAgent -id  
  301. $newip\">/home/patrol/startagent.sh  
  302. chown patrol:patrol /home/patrol/startagent.sh  
  303. chmod 774 /home/patrol/startagent.sh  
  304.  
  305. ####################################################################  
  306. # 重置关键用户密码  
  307. #echo ???????? | passwd root --stdin      ????????请用实际密码代替  
  308. #echo ******** | passwd patrol --stdin    ********请用实际密码代替  
  309. ###################################################################  
  310. #shutdown computer  
  311. #shutdown -h now                      (shutdown前标注了#,所以此句只标注不执行)  
  312. service network restart  
  313. service ntpd stop  
  314. chkconfig ntpd off  
  315. echo "New IP is $newip"  
  316. echo "New primary DNS is $dns"  
  317. echo "New primary NTP is $ntp"  
  318. echo "Change is successful!"  
  319. }  
  320. # 以上语句在设置完所有配置信息后,需要重启网络服务,以确保新IP能够正常工作。  
  321.  
  322. ex() {  
  323. echo "Nothing is changed!"  
  324. exit 0  
  325. }  
  326. # 以上语句为设置CASE语句场景的ex函数,选取E|e时,不执行任何操作,直接退出。  
  327. case "$evar" in  
  328.     R|r)  
  329.         dr  
  330.         ;;  
  331.     T|t)  
  332.         dt  
  333.         ;;  
  334.     E|e)  
  335.        ex  
  336.         ;;  
  337.     *)  
  338. #        echo $"Usage: $0 {R|T|E}"  
  339. #        exit 1  
  340. Esac  
  341. # 以上语句为CASE语句场景,可根据不同的选择执行灾备环境、开发环境的参数调整指令,  
  342. 或者不执行任何操作,直接退出。 


喜欢的朋友可以添加我们的微信账号:

51CTO读书频道二维码


51CTO读书频道活动讨论群:342347198

【责任编辑:book TEL:(010)68476606】

回书目   上一节   下一节
点赞 0
分享:
大家都在看
猜你喜欢

读 书 +更多

SQL实用简明教程(第2版)

SQL(结构化查询语言)是数据库系统的通用语言,利用它可以用几乎同样的语句在不同的数据库系统上执行同样的操作,在数据库系统的开发中有着...

订阅51CTO邮刊

点击这里查看样刊

订阅51CTO邮刊