游戏搭服自动化脚本shell

Posted tdtdttdd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了游戏搭服自动化脚本shell相关的知识,希望对你有一定的参考价值。

技术分享
#!/bin/bash
#10.117.1.199  120.26.198.243  9006;g=6006c=7006  10.168.187.39:8881  10.168.187.39:8882  10.168.187.39:8883  178pop_s10  rm-bp1gy2r82o607w4v8.mysql.rds.aliyuncs.com
#    1              2              3                  4                   5                          6            7                             8


basedir=/data/jzadmin
scripts=$basedir/scripts
rootgamedir=$basedir/game
sourcegame=${PWD}/game
sourcecross=${PWD}/game/Source_cross
sourceworld=${PWD}/game/Source_world
gamelist=${PWD}/create_game_list.txt
tmpdir=${PWD}/tmp
ansiblehost=/etc/ansible/hosts
checklist=$scripts/bak/beifen.txt
checklist2=$scripts/bak/Cross.txt
[ -d $tmpdir ] || mkdir -p $tmp &> /dev/null

createmenu(){
echo -e "\033[32;1m<<=====SUBMENU=====>>\033[35;0m
\033[32;1m1\033[32;0m) CreateGame
\033[32;1m2\033[32;0m) CreateCross
\033[32;1m3\033[32;0m) CreateWorld
\033[32;1m4\033[32;0m) EXIT"
}

login(){
exec_cmd=$1
expect << EOF
                        set timeout -1
                            log_user 0
                        spawn $exec_cmd
                            sleep 1
                        expect "*(yes/no)?" {
                                 send "yes\r"
                        exp_continue
                              } "password:" {
                        send "JGyou(20170206)$\r"
                        }
                        expect "*total size*" 
                        expect eof
EOF
}

login2()
{
_host_ip="$1"
_host_cmd="$2"
tag=$3
expect << EOF
                     set timeout -1
                     log_user $tag
                     spawn ssh [email protected]$_host_ip "$_host_cmd" 
                     expect "*(yes/no)?" {
                            send "yes\r"
                            exp_continue
                      } "password:" {
                     send "JGyou(20170206)$\r"
                     }
                    expect eof
EOF
}

select a in "CreateGame" "CreateCross" "CreateWorld"  "EXIT"
do
        echo "select create:"            
        case $a in
            CreateGame)
                           for uid in `awk {print $7} $gamelist`
                           do
                platform=`echo $uid|cut -d_ -f1`
                                daqushu=`echo $uid|cut -d_ -f2  |cut -d"s" -f2`         
                gamedirSource=${platform}cq_lycqly_source
                                dbip=`awk /\<‘‘‘$uid‘‘‘\>/{print $8} $gamelist`
                array=($(awk /\<‘‘‘$uid‘‘‘\>/{print} $gamelist))
                cwlan_ip=`awk /\<‘‘‘$uid‘‘‘\>/{print $1} $gamelist`        
                cwwan_ip=`awk /\<‘‘‘$uid‘‘‘\>/{print $2} $gamelist`        
                gport=`awk /\<‘‘‘$uid‘‘‘\>/{print $3} $gamelist|cut -d"=" -f2|cut -b 1-4`
                cport=`awk /\<‘‘‘$uid‘‘‘\>/{print $3} $gamelist|cut -d";" -f1`
                for i in {3..5}
                do
                  daqushuN=$(($daqushu+i-3))
                  gamedir=${platform}cq_lycqly_s${daqushuN}
                  sid=${platform}_s${daqushuN}
                  dbname=${platform}cq_s$daqushu
                  lanip=`echo ${array[i]}|cut -d: -f1`
                  gameip=`awk /\<‘‘‘$lanip‘‘‘\>/{print $1} ${ansiblehost}`
                  waiip=`ansible $gameip -m command -a ip addr|grep eth1| sed -n "2,1p" | awk {print $2} | cut -d"/" -f1`
                  port=`echo ${array[i]}|cut -d: -f2`
                  echo -e ">>> $gamedir creating..\c"
                  [ $? -ne 0 ] && echo "copy center_db.cfg to source game failed and exit." && exit 109
                  exec_cmd="rsync -aP --delete  --exclude=lord.cfg  $sourcegame/$platform/$gamedirSource/ [email protected]$lanip:$rootgamedir/$gamedir"
                  login "$exec_cmd"                            
                  cp -af $sourcegame/$platform/$gamedirSource/lord.cfg $tmpdir/lord_${gamedir}.cfg
                  sed -i "/\[lord\]/,/\[Paysys\]/ s/Port=[0-9]\{4\}/Port=$port/" $tmpdir/lord_${gamedir}.cfg
                  sed -i "s/PID_SID=\(.*\)/PID_SID=$sid/" $tmpdir/lord_${gamedir}.cfg
                  sed -i "/\[DBSecure\]/,/\[SCC\]/ s/HostName=rm-.*com$/HostName=$dbip/" $tmpdir/lord_${gamedir}.cfg
                  sed -i "/\[DBSecure\]/,/\[SCC\]/ s/DBName=\(.*\)/DBName=$dbname/" $tmpdir/lord_${gamedir}.cfg 
                  sed -i "/\[WorldServer\]/,/\[CrossServer\]/ s/HostName=[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/HostName=$cwlan_ip/"                   $tmpdir/lord_${gamedir}.cfg
                  sed -i "/\[WorldServer\]/,/\[CrossServer\]/ s/Port=[0-9]\{4\}/Port=$gport/" $tmpdir/lord_${gamedir}.cfg
                  sed -i "/\[CrossServer\]/,+3 s/InternetIp=[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/InternetIp=$cwwan_ip/" $tmpdir/lord_${gamedir}.cfg
                  sed -i "/\[CrossServer\]/,+3 s/Port=[0-9]\{4\}/Port=$cport/" $tmpdir/lord_${gamedir}.cfg
                  exec_cmd="rsync -aP --delete $tmpdir/lord_${gamedir}.cfg [email protected]$lanip:$rootgamedir/$gamedir/lord.cfg"
                  login "$exec_cmd"
                  [ $? -eq 0 ] && echo -e "\r>>> $gamedir created \e[01;32m [ ok ]\e[0m" || exit 2
                  echo "$lanip   $waiip   $port   $gamedir   lord_${platform}_s$daqushuN   ${platform}_s$daqushuN   $dbname   $dbip" >> ${PWD}/gamecontral.txt
#                  gamecontral=($lanip $waiip $port $gamedir lord_${platform}_s$daqushuN ${platform}_s$daqushuN $dbname $dbip)
#                  echo ${gamecontral[*]} >> ${PWD}/gamecontral.txt
                    done
                            done
            createmenu
            ;;
        CreateCross)
            for uid in `awk {print $7} $gamelist`
                        do
                             platform=`echo $uid|cut -d_ -f1`
                             daqushu=`echo $uid|cut -d_ -f2  |cut -d"s" -f2`         
                 crossdir=${platform}cq_croserver_s$daqushu
                             crossdirSource=${platform}cq_croserver_source
                             dbip=`awk /\<‘‘‘$uid‘‘‘\>/{print $8} $gamelist`
                             cwlan_ip=`awk /\<‘‘‘$uid‘‘‘\>/{print $1} $gamelist`        
                             cwwan_ip=`awk /\<‘‘‘$uid‘‘‘\>/{print $2} $gamelist`        
                             gport=`awk /\<‘‘‘$uid‘‘‘\>/{print $3} $gamelist|cut -d"=" -f2|cut -b 1-4`
                             cport=`awk /\<‘‘‘$uid‘‘‘\>/{print $3} $gamelist|cut -d";" -f1`        
                 c2port=`awk /\<‘‘‘$uid‘‘‘\>/{print $3} $gamelist|cut -d"=" -f3`
                             echo -e ">>> Cross $crossdir creating..\c"
                             exec_cmd="rsync -aP --delete  --exclude=lord.cfg $sourcegame/$platform/$crossdirSource/ [email protected]${cwlan_ip}:$rootgamedir/$crossdir"
                             login "$exec_cmd"
                             cp -af $sourcegame/$platform/$crossdirSource/lord.cfg $tmpdir/lord_${crossdir}.cfg
                             sed -i "/\[Paysys\]/,/\[DBSecure\]/ s/PID_SID=\(.*\)/PID_SID=$dbname/" $tmpdir/lord_${crossdir}.cfg
                             sed -i "/\[lord\]/,/\[Paysys\]/ s/Port=[0-9]\{4\}/Port=$cport/" $tmpdir/lord_${crossdir}.cfg
                             sed -i "/\[DBSecure\]/,/\[SCC\]/ s/HostName=rm-.*com$/HostName=$dbip/"                              $tmpdir/lord_${crossdir}.cfg
                             sed -i "/\[DBSecure\]/,/\[SCC\]/ s/DBName=\(.*\)/DBName=$dbname/" $tmpdir/lord_${crossdir}.cfg
                             #World cross config
                             sed -i "/\[WorldServer\]/,/\[CrossServer\]/ s/HostName=[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/HostName=$cwlan_ip/"                             $tmpdir/lord_${crossdir}.cfg
                             sed -i "/\[WorldServer\]/,/\[CrossServer\]/ s/Port=[0-9]\{4\}/Port=$c2port/" $tmpdir/lord_${crossdir}.cfg
                             sed -i "/\[CrossServer\]/,+3 s/InternetIp=[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/InternetIp=$cwwan_ip/"                              $tmpdir/lord_${crossdir}.cfg
                             sed -i "/\[CrossServer\]/,+3 s/Port=[0-9]\{4\}/Port=$cport/" $tmpdir/lord_${crossdir}.cfg
                             exec_cmd="rsync -aP --delete $tmpdir/lord_${crossdir}.cfg [email protected]$cwlan_ip:$rootgamedir/$crossdir/lord.cfg"
                             login "$exec_cmd"
                             [ $? -eq 0 ] && echo -e "\r>>> Cross $dbname created \e[01;32m [ ok ]\e[0m" || exit 2
                 echo "$cwlan_ip   $cwwan_ip   $cport   $crossdir   lord_${platform}cros_s$daqushu   ${platform}cros_s$daqushu   ${platform}cq_s$daqushu   127.0.0.1" >> ${PWD}/gamecontral.txt
#                 crosscontral=($cwlan_ip $cwwan_ip $cport $crossdir lord_${platform}cros_s$daqushu ${platform}cros_s$daqushu ${platform}cq_s$daqushu 127.0.0.1)
#                 echo ${crosscontral[*]} >> ${PWD}/gamecontral.txt
                        done
            createmenu
            ;;
        CreateWorld)
                for uid in `awk {print $7} $gamelist`
                        do
               platform=`echo $uid|cut -d_ -f1`
                           daqushu=`echo $uid|cut -d_ -f2  |cut -d"s" -f2`
                           worlddir=${platform}cq_worldserver_s$daqushu
                           worlddirSource=${platform}cq_worldserver_source
               cwlan_ip=`awk /\<‘‘‘$uid‘‘‘\>/{print $1} $gamelist`
                           cwwan_ip=`awk /\<‘‘‘$uid‘‘‘\>/{print $2} $gamelist`
                           gport=`awk /\<‘‘‘$uid‘‘‘\>/{print $3} $gamelist|cut -d"=" -f2|cut -b 1-4`
                           c2port=`awk /\<‘‘‘$uid‘‘‘\>/{print $3} $gamelist|cut -d"=" -f3`
#               port3="g=${gprot}c=${c2port}"
                           echo -e ">>> World $destdir creating..\c"
                           exec_cmd="rsync -aP --delete   $sourcegame/$platform/$worlddirSource/ [email protected]$cwlan_ip:$rootgamedir/$worlddir"
                           login "$exec_cmd"
                           cp -af $sourcegame/$platform/$worlddirSource/Config/WorldServer.Cfg $tmpdir/WorldServer_${worlddir}.cfg
                           sed -i "s/Ip=[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/Ip=$cwlan_ip/" $tmpdir/WorldServer_${worlddir}.cfg
                           sed -i "s/LogicServerPort=[0-9]\{4\}/LogicServerPort=$gport/" $tmpdir/WorldServer_${worlddir}.cfg
                           sed -i "s/CrossServerPort=[0-9]\{4\}/CrossServerPort=$c2port/" $tmpdir/WorldServer_${worlddir}.cfg
                           exec_cmd="rsync -aP --delete $tmpdir/WorldServer_${worlddir}.cfg [email protected]$cwlan_ip:$rootgamedir/$worlddir/Config/WorldServer.Cfg"
                           login "$exec_cmd"
                           [ $? -eq 0 ] && echo -e "\r>>> World $dbname created\e[01;32m [ ok ]\e[0m" || exit 2
#               worldcontral=($cwlan_ip $cwwan_ip g=${gport}c=${c2port} $worlddir lord_${platform}world_s$daqushu ${platform}world_s$daqushu ${platform}cq_s$daqushu 127.0.0.1)
#               echo ${worldcontral[*]} >> ${PWD}/gamecontral.txt
               echo "$cwlan_ip   $cwwan_ip   g=${gport}c=${c2port}   $worlddir   lord_${platform}world_s$daqushu   ${platform}world_s$daqushu   ${platform}cq_s$daqushu   127.0.0.1" >> ${PWD}/gamecontral.txt
                        
            done
            createmenu
            ;;
        EXIT)
                echo "EXIT"
                exit 0
            ;;
            *)
                echo "\033[31;1mError\033[31;0m"
                createmenu
            ;;
        esac
done
View Code

 

以上是关于游戏搭服自动化脚本shell的主要内容,如果未能解决你的问题,请参考以下文章

shell 脚本 片段

用于确保在任何给定时间仅运行一个 shell 脚本的 shell 片段 [重复]

常用python日期日志获取内容循环的代码片段

从片段调用 Google Play 游戏服务

shell脚本引用expect

dotnet项目执行shell脚本实现简单的自动化部署