Chinaunix首页 | 论坛 | 博客
  • 博客访问: 386135
  • 博文数量: 112
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 800
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-29 13:41
文章分类

全部博文(112)

文章存档

2020年(1)

2018年(10)

2017年(27)

2016年(18)

2015年(31)

2014年(25)

分类: 系统运维

2016-07-04 23:54:23

此脚本分为两部分,一是批量创建xshell的session;二是批量修改配置文件;


点击(此处)折叠或打开

  1. #!/bin/bash
  2. IP="([0-9]{1,3}\.){3}[0-9]{1,3}"
  3. PORT="([0-9]{5})"
  4. CSV_FILE="create_xshell.csv"
  5. #create xshell session
  6. create_xshell ()
  7. {
  8. local XSH=`find . -maxdepth 1 -type f -name "*.xsh"`
  9. local ROLE=('' '' gl map1 map2 map3)
  10. #local XSH_IP=`echo "$XSH"|grep -Eo "$IP"`
  11. local XSH_VALUE=`find . -maxdepth 1 -type f -name "*.xsh"|wc -l`
  12. if [[ "${XSH##*.}" = "xsh" ]] && [[ $XSH_VALUE -eq 1 ]];then
  13.    echo "$XSH file is exist!!"
  14. else
  15.    echo "*.xsh is not exist!!"
  16.    exit 1
  17. fi
  18. #
  19. if [[ -f "$CSV_FILE" ]];then
  20.     echo "$CSV_FILE is exist!!"
  21. else
  22.     echo "$CSV_FILE is not exist!!"
  23.     exit 5
  24. fi
  25. #
  26. dos2unix $CSV_FILE
  27. #sed 's/\\r//g' -i $CSV_FILE
  28. #fi
  29. while read line
  30. do
  31. local GAME_INFO=($(echo $line|awk -F"," '{print $1,$2,$3,$4,$5,$6}'))
  32. if [[ -n $line ]];then
  33. # echo "$line" |sed 's/\\r//g'
  34. local LINE_FRONT=`echo $line |grep -o "^#"`
  35.     if [ "$LINE_FRONT" = "#" ];then
  36.         continue
  37.     else
  38.         for ((i=2;i<=5;i++))
  39.         do
  40.                 cp ${XSH} ${GAME_INFO[0]}-${GAME_INFO[1]}-${GAME_INFO[$i]}-${ROLE[$i]}.xsh
  41.                 sed -r "/Host/s/$IP/${GAME_INFO[$i]}/" -i ${GAME_INFO[0]}-${GAME_INFO[1]}-${GAME_INFO[$i]}-${ROLE[$i]}.xsh
  42.         done
  43.         mkdir -p session/${GAME_INFO[0]} && mv ${GAME_INFO[0]}-${GAME_INFO[1]}*.xsh session/${GAME_INFO[0]}
  44.     fi
  45. else
  46.     echo "$line is not null!"
  47.     exit 1
  48. fi
  49. done <"$CSV_FILE"
  50. }
  51. #create base config
  52. create_base_config ()
  53. {
  54. local CONFIG_ZIP=`ls |grep -E "[0-9]{4}.zip"`
  55. if `ls |grep -qE "[0-9]{4}.zip"`;then
  56.     echo "config zip is exist!"
  57.     unzip $CONFIG_ZIP -d .
  58. else
  59.     echo "config zip is not exist!"
  60.     exit 2
  61. fi
  62. local BASE_CONFIG_FILE=(BaseServerConfig.ini DBConfig.ini GMConfig.tab WorldIndex.ini)
  63. local NO_CONFIG_FILE=(DBShareConfig.ini GameToggleConfig.ini ExtServerConfig.ini MultiGroupConfig.ini GameConfig.ini ServerIndex.ini)
  64. while read line;
  65. do
  66. local BASE_CONFIG=(`echo $line|awk -F',' '{print $1,$3,$4,$5,$6,$7,$8,$9,$10,$11}'`)
  67. local CONFIG_NUM=`ls |grep -E "*.ini|*.tab"|wc -l`
  68. local LINE_FRONT=`echo $line |grep -o "^#"`
  69. if [ "$LINE_FRONT" = "#" ];then
  70.     continue
  71. else
  72.     if [[ "$CONFIG_NUM" -eq 10 ]];then
  73.         echo "alter ${BASE_CONFIG_FILE[0]} ..."
  74. # grep -A3 "Server6" ${BASE_CONFIG_FILE[0]}|sed -i "/IP/s/$IP/${BASE_CONFIG[1]}/g" ${BASE_CONFIG_FILE[0]}
  75.         sed -ri "/Server6/{n;N;n;s/$IP/${BASE_CONFIG[1]}/;}" ${BASE_CONFIG_FILE[0]}
  76. # grep -EA3 "Server0|Server4" ${BASE_CONFIG_FILE[0]}|sed -i "/IP/s/$IP/${BASE_CONFIG[2]}/g" ${BASE_CONFIG_FILE[0]}
  77.         sed -ri "/Server0/{n;N;n;s/$IP/${BASE_CONFIG[2]}/;};/Server4/{n;N;n;s/$IP/${BASE_CONFIG[2]}/;}" ${BASE_CONFIG_FILE[0]}
  78. # grep -EA3 "Server1|Server3" ${BASE_CONFIG_FILE[0]}|sed -i "/IP/s/$IP/${BASE_CONFIG[3]}/g" ${BASE_CONFIG_FILE[0]}
  79.         sed -ri "/Server1/{n;N;n;s/$IP/${BASE_CONFIG[3]}/;};/Server3/{n;N;n;s/$IP/${BASE_CONFIG[3]}/;}" ${BASE_CONFIG_FILE[0]}
  80. # grep -EA3 "Server2|Server5" ${BASE_CONFIG_FILE[0]}|sed -i "/IP/s/$IP/${BASE_CONFIG[4]}/g" ${BASE_CONFIG_FILE[0]}
  81.         sed -ri "/Server2/{n;N;n;s/$IP/${BASE_CONFIG[4]}/;};/Server5/{n;N;n;s/$IP/${BASE_CONFIG[4]}/;}" ${BASE_CONFIG_FILE[0]}
  82. # grep -EA1 "DBCenter|GLServer" ${BASE_CONFIG_FILE[0]}|sed -i "/IP/s/$IP/${BASE_CONFIG[5]}/g" ${BASE_CONFIG_FILE[0]}
  83.         sed -ri "/DBCenter/,/Server0/s/$IP/${BASE_CONFIG[5]}/" ${BASE_CONFIG_FILE[0]}
  84. # grep -A1 "AUServer" ${BASE_CONFIG_FILE[0]}|sed -i "/IP/s/$IP/${BASE_CONFIG[6]}/" ${BASE_CONFIG_FILE[0]}
  85.         sed -ri "/AUServer/{n;s/$IP/${BASE_CONFIG[6]}/;}" ${BASE_CONFIG_FILE[0]}
  86. # grep -A1 "Billing" ${BASE_CONFIG_FILE[0]}|sed -i "/IP/s/$IP/${BASE_CONFIG[7]}/" ${BASE_CONFIG_FILE[0]}
  87.         sed -ri "/Billing/{n;s/$IP/${BASE_CONFIG[7]}/;}" ${BASE_CONFIG_FILE[0]}
  88.         sed -ri "/Billing/{N;n;s/${PORT}/${BASE_CONFIG[9]}/;}" ${BASE_CONFIG_FILE[0]}
  89.         echo "alter ${BASE_CONFIG_FILE[1]} ..."
  90. # grep -A4 "System" ${BASE_CONFIG_FILE[1]}|sed -ri "/DBUser/s/(dbcenter)[0-9]{4}/\1"$ID"/" ${BASE_CONFIG_FILE[1]}
  91.         sed -ri "/System/{N;n;n;n;s/(dbcenter)[0-9]{4}/\1${BASE_CONFIG[0]}/;}" ${BASE_CONFIG_FILE[1]}
  92. # grep -A2 "DBLog" ${BASE_CONFIG_FILE[1]}|sed -ri "/DBUser/s/(dbcenter)[0-9]{,4}/\1"${BASE_CONFIG[8]}"/" ${BASE_CONFIG_FILE[1]}
  93.         sed -ri "/\[DBLog\]/{N;n;s/(dbcenter)[0-9]{1,2}/\1${BASE_CONFIG[8]}/;}" ${BASE_CONFIG_FILE[1]}
  94.         echo "alter ${BASE_CONFIG_FILE[2]} ..."
  95.         sed -ri "s/^[0-9]{4}/"${BASE_CONFIG[0]}"/" ${BASE_CONFIG_FILE[2]}
  96.         echo "alter ${BASE_CONFIG_FILE[3]} ..."
  97.         sed -ri "/GLServerID/s/[0-9]{4}/"${BASE_CONFIG[0]}"/" ${BASE_CONFIG_FILE[3]}
  98.         mkdir -p config/${BASE_CONFIG[0]} && cp ${BASE_CONFIG_FILE[*]} ${NO_CONFIG_FILE[*]} config/${BASE_CONFIG[0]}
  99.         zip config/${BASE_CONFIG[0]}.zip ${BASE_CONFIG_FILE[*]} ${NO_CONFIG_FILE[*]}
  100.     else
  101.         echo "base config file number is wrong!!"
  102.         exit 3
  103.     fi
  104. fi
  105.     
  106. done<"$CSV_FILE"
  107. }
  108. #choice
  109. cat<<EOF
  110. select num:
  111. 1.create xshell;
  112. 2.create Base Config;
  113. 3.exit
  114. EOF
  115. #
  116. read -p "please select num:" answer
  117. case $answer in
  118.         1)
  119.         create_xshell
  120.         ;;
  121.         2)
  122.         create_base_config
  123.         ;;
  124.         3)
  125.         exit
  126.         ;;
  127.         *)
  128.         echo "please enter:[1|2|3]"
  129.         ;;
  130. esac







create_xshell.csv内容如下:
#2001 hostname 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 192.168.1.5 192.168.1.6 192.168.1.7 2 10001
#2002 hostname 192.168.2.1 192.168.2.2 192.168.2.3 192.168.2.4 192.168.2.5 192.168.2.6 192.168.2.7 2 10002
#3001 hostname 192.168.3.1 192.168.3.2 192.168.3.3 192.168.3.4 192.168.3.5 192.168.3.6 192.168.3.7 2 10003
#3002 hostname 192.168.4.1 192.168.4.2 192.168.4.3 192.168.4.4 192.168.4.5 192.168.4.6 192.168.4.7 2 10004
#2001 hostname 192.168.5.1 192.168.5.2 192.168.5.3 192.168.5.4 192.168.5.5 192.168.5.6 192.168.5.7 2 10005
阅读(4621) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~