多环境批处理脚本

version 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
@echo off & SetLocal EnableDelayedExpansion

cd /d %~dp0

:: 检测mysql端口
:: 检查端口,如果冲突则随机端口,重新执行检查
set myport=16603
rem set myport=137
:myportDetect
netstat -aon|findstr "%myport%" > NUL 2>NUL && set myport=%random% && goto myportDetect || echo %myport%
:: 因随机端口后myport的值后,替换端口后面会又空格,执行如下步骤,转换以下,使空格消除。
for /F %%i in ('echo %myport%') do ( set myport=%%i)
echo myport=%myport%

set inipath="%cd%\mysql\mybak.ini"
rem del "%cd%\mysql\my.ini" >nul 2>nul
for /f "delims=" %%a in (.\mysql\mybak.ini) do (
set a=%%a
set a=!a:16603=%myport%!
echo !a!>>"%cd%\mysql\my.ini"
)


:: 检查redis端口
set redisport=17963
rem set redisport=137
:redisportDetect
netstat -aon|findstr "%redisport%" > NUL 2>NUL && set redisport=%random% && goto redisportDetect || echo %redisport%
for /F %%i in ('echo %redisport%') do ( set redisport=%%i)
echo redisport=%redisport%
rem set reconfpath="%cd%\redis\redis.windows-service-bak.conf"
del "%cd%\redis\redis.windows-service.conf" >nul 2>nul
for /f "delims=" %%b in (.\redis\redis.windows-service-bak.conf) do (
set b=%%b
set b=!b:17963=%redisport%!
echo !b!>>"%cd%\redis\redis.windows-service.conf"
)


:: 检查tomcat端口
::: zxzb1 端口
set zxzb1=18785
rem set zxzb1=137
:javaportDetect1
netstat -aon|findstr "%zxzb1%" > NUL 2>NUL && set zxzb1=%random% && goto javaportDetect1 || echo %zxzb1%
for /F %%i in ('echo %zxzb1%') do ( set zxzb1=%%i)
echo zxzb1=%zxzb1%




::: zxzb2 端口
set zxzb2=18388
rem set zxzb2=137
:javaportDetect2
netstat -aon|findstr "%zxzb2%" > NUL 2>NUL && set zxzb2=%random% && goto javaportDetect2 || echo %zxzb2%
for /F %%i in ('echo %zxzb2%') do ( set zxzb2=%%i)
echo zxzb2=%zxzb2%



::: zxzb3 端口
set zxzb3=18489
rem set zxzb3=137
:javaportDetect3
netstat -aon|findstr "%zxzb3%" > NUL 2>NUL && set zxzb3=%random% && goto javaportDetect3 || echo %zxzb3%
for /F %%i in ('echo %zxzb3%') do ( set zxzb3=%%i)
echo zxzb3=%zxzb3%
:: dos命令替换文本内容,存在弊端,对一些特殊服务转义存在问题,只适合简单文档的替换
:: 原理读取文档每一行,检查每一行,存在匹配,替换到新文档,然后新文档覆盖原来的文档
rem set javaconfpath="%cd%\service\conf\server_bak.xml"
del "%cd%\service\conf\server.xml" >nul 2>nul
for /f "delims=" %%c in (.\service\conf\server_bak.xml) do (
set c=%%c
set c=!c:18785=%zxzb1%!
set c=!c:18388=%zxzb2%!
set c=!c:18489=%zxzb3%!
echo !c!>>"%cd%\service\conf\server.xml"
)

:: 检查地图服务端口
set mapport=10000
rem set mapport=137
:mapportDetect
netstat -aon|findstr "%mapport%" > NUL 2>NUL && set mapport=%random% && goto mapportDetect || echo %mapport%
for /F %%i in ('echo %mapport%') do ( set mapport=%%i)
echo mapport=%mapport%

:: 替换env.bat内容
:: 本方法是采用vbs命令方式,对特殊符号转义稍好,原理和dos命令差不多,复杂符号还是需要其他深入配置
del ".\mapser\env.bat" >nul 2>nul
echo set fso=createobject("scripting.filesystemobject") >3.vbs
echo set file=fso.opentextfile("env.bat") >>3.vbs
echo s=file.readall >>3.vbs
echo file.close >>3.vbs
echo s=replace(s,"10000","%mapport%") >>3.vbs
echo set file=fso.createtextfile(".\mapser\env.bat") >>3.vbs
echo file.write s >>3.vbs
echo file.close >>3.vbs
start 3.vbs

:: 替换gdw.conf
del ".\mapser\serv\conf\gdw.conf" >nul 2>nul
echo set fso=createobject("scripting.filesystemobject") >4.vbs
echo set file=fso.opentextfile("gdw.conf") >>4.vbs
echo s=file.readall >>4.vbs
echo file.close >>4.vbs
echo s=replace(s,"10000","%mapport%") >>4.vbs
echo set file=fso.createtextfile(".\mapser\serv\conf\gdw.conf") >>4.vbs
echo file.write s >>4.vbs
echo file.close >>4.vbs
start 4.vbs

:: 修改war config
:zxzbconfig
rem set zxzbconfpath=%cd%\service\webapps\zxzb\WEB-INF\classes\application-dev-bak.properties
rem set vbsfile1=%cd%\service\webapps\zxzb\WEB-INF\classes\vbs1.txt
rem set vbsfile2=%cd%\service\webapps\zxzb\WEB-INF\classes\vbs2.txt
rem set zxzbconf=%cd%\service\webapps\zxzb\WEB-INF\classes\application-dev.properties
del "%cd%\service\webapps\zxzb\WEB-INF\classes\application-dev.properties" >nul 2>nul
echo set fso=createobject("scripting.filesystemobject") >1.vbs
echo set file=fso.opentextfile(".\service\webapps\zxzb\WEB-INF\classes\application-dev-bak.properties") >>1.vbs
echo s=file.readall >>1.vbs
echo file.close >>1.vbs
echo s=replace(s,"16603","%myport%") >>1.vbs
echo set file=fso.createtextfile(".\service\webapps\zxzb\WEB-INF\classes\vbs1.txt") >>1.vbs
echo file.write s >>1.vbs
echo file.close >>1.vbs

echo set fso=createobject("scripting.filesystemobject") >>1.vbs
echo set file=fso.opentextfile(".\service\webapps\zxzb\WEB-INF\classes\vbs1.txt") >>1.vbs
echo s=file.readall >>1.vbs
echo file.close >>1.vbs
echo s=replace(s,"17963","%redisport%") >>1.vbs
echo set file=fso.createtextfile(".\service\webapps\zxzb\WEB-INF\classes\vbs2.txt") >>1.vbs
echo file.write s >>1.vbs
echo file.close >>1.vbs

echo set fso=createobject("scripting.filesystemobject") >>1.vbs
echo set file=fso.opentextfile(".\service\webapps\zxzb\WEB-INF\classes\vbs2.txt") >>1.vbs
echo s=file.readall >>1.vbs
echo file.close >>1.vbs
echo s=replace(s,"10000","%mapport%") >>1.vbs
echo set file=fso.createtextfile(".\service\webapps\zxzb\WEB-INF\classes\application-dev.properties") >>1.vbs
echo file.write s >>1.vbs
echo file.close >>1.vbs
start 1.vbs
ping 127.1 -n 2 >nul
del .\service\webapps\zxzb\WEB-INF\classes\vbs1.txt
del .\service\webapps\zxzb\WEB-INF\classes\vbs2.txt
rem del 1.vbs

rem for /f "tokens=*" %%f in (%zxzbconfpath%) do (
rem endlocal
rem set f=%%f
rem setlocal enabledelayedexpansion
rem set f=!f:16603=%myport%!
rem set f=!f:17963=%redisport%!
rem set f=!f:18785=%zxzb1%!
rem set f=!f:18388=%zxzb2%!
rem set f=!f:18489=%zxzb3%!
rem echo !f! >>"%cd%\service\webapps\zxzb\WEB-INF\classes\application-dev.properties"
rem )

::: 前端urlport修改(只用在web封装版本)
:webconfig
rem set javaconfpath=requestUrl.js
del "..\app\static\js\requestUrl.js" >nul 2>nul
echo set fso=createobject("scripting.filesystemobject") >2.vbs
echo set file=fso.opentextfile("requestUrl.js") >>2.vbs
echo s=file.readall >>2.vbs
echo file.close >>2.vbs
echo s=replace(s,"24486","%zxzb2%") >>2.vbs
echo set file=fso.createtextfile("..\app\static\js\requestUrl.js") >>2.vbs
echo file.write s >>2.vbs
echo file.close >>2.vbs

:: app\static\js\config
del "..\app\static\js\config.js" >nul 2>nul
echo set fso=createobject("scripting.filesystemobject") >>2.vbs
echo set file=fso.opentextfile("config.js") >>2.vbs
echo s=file.readall >>2.vbs
echo file.close >>2.vbs
echo s=replace(s,"10000","%mapport%") >>2.vbs
echo set file=fso.createtextfile("..\app\static\js\config.js") >>2.vbs
echo file.write s >>2.vbs
echo file.close >>2.vbs

::这个作为测试页面web
del ".\service\webapps\zxzb\WEB-INF\classes\templates\static\js\config.js" >nul 2>nul
echo set fso=createobject("scripting.filesystemobject") >>2.vbs
echo set file=fso.opentextfile("config.js") >>2.vbs
echo s=file.readall >>2.vbs
echo file.close >>2.vbs
echo s=replace(s,"10000","%mapport%") >>2.vbs
echo set file=fso.createtextfile(".\service\webapps\zxzb\WEB-INF\classes\templates\static\js\config.js") >>2.vbs
echo file.write s >>2.vbs
echo file.close >>2.vbs

start 2.vbs


rem for /f "delims=" %%d in (requestUrl.js) do (
rem set d=%%d
rem set d=!d:24486=%zxzb2%!
rem echo !c!>>"..\app\static\js\requestUrl.js"
rem )

::::: 安装服务
::: 安装vc服务
::: 原理 查询注册表,根据返回值判断执行不同结果命令,采用判断语句
:2010Detect
cd /d %~dp0
rem set "HKLMU=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
::::: 判断系统是否已经安装了 Visual C++ 2010(包括所有版本)
::::: 有就跳过安装Visual C++ 2010,没有则先安装。
:: 此处采用 if defined 命令返回变量的值,执行不同结果命令,下面{}是vc工具ID,进行匹配,但是版本太多,没有手机完,总成匹配不全
rem reg query %HKLMU%\{196BB40D-1578-3D01-B289-BEFC77A11A1E}>nul 2>nul&&set VC2010=Microsoft Visual C++ 2010 Redistributable X86
rem reg query %HKLMU%\{DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}>nul 2>nul&&set VC2010=Microsoft Visual C++ 2010 Redistributable X64
rem if defined VC2010 (echo %VC2010% 已安装!&goto mysqlserviceDetect >nul) else (goto 2010Install)
::::: 判断系统是否已经安装了 Visual C++ 2010(包括所有版本)
::::: 有就跳过安装Visual C++ 2010,没有则先安装。
:: 此处采用检查注册表路径下有没有特定版本的目录,返回结果,真和假。匹配准确性待测试,个人想到的方式
set "HKLMU=HKLM\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\"
if %errorlevel% ==0 (
echo %vc10% 已安装!& goto 2015Detect
) else (
echo noexsit & goto 2010Install
)
:2010Install
:::::各版本下载地址 http://www.microsoft.com/downloads/details.aspx?FamilyID=766a6af7-ec73-40ff-b072-9112bab119c2&DisplayLang=zh-cn
"%cd%\mysql\vcredist_x64_2010.exe"

:2015Detect
reg query %HKLMU%14.0 >nul && set vc15=Microsoft Visual C++ 2015
if %errorlevel% ==0 (
echo %vc15% 已安装! & goto mysqlserviceDetect
) else (
echo noexsit & goto 2015Install
)

:2015Install
vcredist_x64_2015.exe

::: 安装mysql
:mysqlserviceDetect
cd /d %~dp0
set "mysqlservice=jx_mysql5.6"
sc query "%mysqlservice%">nul
IF ERRORLEVEL 1060 GOTO mysqlInstall
GOTO mysqlexist


:mysqlInstall
"%cd%\mysql\bin\mysqld.exe" -install %mysqlservice% --defaults-file="%cd%\mysql\my.ini"
sc config %mysqlservice% start=auto
net start %mysqlservice%

::: 安装redis
:redisserviceDetect
cd /d %~dp0
set "redisservice=jx_redis3.2.100"
sc query "%redisservice%">nul
IF ERRORLEVEL 1060 GOTO redisInstall
GOTO redisexist

:redisInstall
"%cd%\redis\redis-server" --service-install --service-name %redisservice% "%cd%\redis\redis.windows-service.conf" --loglevel verbose
sc config %redisservice% start=auto
net start %redisservice%
echo ------------------------------------------------------------------------------

::: 安装tomcat
:javaserviceDetect
cd /d %~dp0
set "javaservice=jx_tomcat8.5_jdk8"
sc query "%javaservice%">nul
IF ERRORLEVEL 1060 GOTO javaInstall
GOTO javaexist

:javaInstall
cd service
call "%cd%\bin\service.bat" install %javaservice%
ping 127.0.0.1 -n 5 > nul
sc config %javaservice% start=auto
net start %javaservice%

::: 安装mapserver
:mapserviceDetect
cd /d %~dp0
set "mapservice=jx_mapserv0.20.1"
sc query "%mapservice%">nul
IF ERRORLEVEL 1060 GOTO mapInstall
GOTO mapexist

:mapInstall
instsrv.exe jx_mapserv0.20.1 "%cd%\mapser\start.exe"
ping 127.0.0.1 -n 5 > nul
sc config jx_mapserv0.20.1 start=auto
net start jx_mapserv0.20.1

echo mysqlport=%myport% > READ.txt
echo redisport=%redisport% >> READ.txt
echo javaport1=%zxzb1% >> READ.txt
echo javaport2-http=%zxzb2% >> READ.txt
echo javaport3=%zxzb3% >> READ.txt
echo mapport=%mapport% >> READ.txt
ping 127.0.0.1 -n 60 > nul
goto END

:mysqlexist
mshta vbscript:msgbox("ERROR:%mysqlservice% 服务已存在!!!",64,"异常提醒")(window.close)
GOTO END

:redisexist
mshta vbscript:msgbox("ERROR:%redisservice% 服务已存在!!!",64,"异常提醒")(window.close)
GOTO END

:javaexist
mshta vbscript:msgbox("ERROR:%javaservice% 服务已存在!!!",64,"异常提醒")(window.close)
GOTO END

:END

version 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
::[Bat To Exe Converter]
::
::YAwzoRdxOk+EWAnk
::fBw5plQjdG8=
::YAwzuBVtJxjWCl3EqQJgSA==
::ZR4luwNxJguZRRnk
::Yhs/ulQjdF+5
::cxAkpRVqdFKZSzk=
::cBs/ulQjdF+5
::ZR41oxFsdFKZSDk=
::eBoioBt6dFKZSDk=
::cRo6pxp7LAbNWATEpCI=
::egkzugNsPRvcWATEpCI=
::dAsiuh18IRvcCxnZtBJQ
::cRYluBh/LU+EWAnk
::YxY4rhs+aU+JeA==
::cxY6rQJ7JhzQF1fEqQJQ
::ZQ05rAF9IBncCkqN+0xwdVs0
::ZQ05rAF9IAHYFVzEqQJQ
::eg0/rx1wNQPfEVWB+kM9LVsJDGQ=
::fBEirQZwNQPfEVWB+kM9LVsJDGQ=
::cRolqwZ3JBvQF1fEqQJQ
::dhA7uBVwLU+EWDk=
::YQ03rBFzNR3SWATElA==
::dhAmsQZ3MwfNWATElA==
::ZQ0/vhVqMQ3MEVWAtB9wSA==
::Zg8zqx1/OA3MEVWAtB9wSA==
::dhA7pRFwIByZRRnk
::Zh4grVQjdCyDJGyX8VAjFBpQQQ2MAE+/Fb4I5/jH9fKdoHEUWvEreYPXlLGWJYA=
::YB416Ek+ZW8=
::
::
::978f952a14a936cc963da21a135fa983
@echo off
cd /d %~dp0

call envport.bat

:myportDetect
netstat -aon|findstr LISTENING|findstr "%myport%" > NUL 2>NUL && mshta vbscript:msgbox("ERROR:%myport% 端口已占用!!!",64,"异常提醒")(window.close) || echo %myport%

:redisportDetect
netstat -aon|findstr LISTENING|findstr "%redisport%" > NUL 2>NUL && mshta vbscript:msgbox("ERROR:%redisport% 端口已占用!!!",64,"异常提醒")(window.close) || echo %redisport%

:javaportDetect1
netstat -aon|findstr LISTENING|findstr "%zxzb1%" > NUL 2>NUL && mshta vbscript:msgbox("ERROR:%zxzb1% 端口已占用!!!",64,"异常提醒")(window.close) || echo %zxzb1%

:javaportDetect2
netstat -aon|findstr LISTENING|findstr "%zxzb2%" > NUL 2>NUL && mshta vbscript:msgbox("ERROR:%zxzb2% 端口已占用!!!",64,"异常提醒")(window.close) || echo %zxzb2%

:javaportDetect3
netstat -aon|findstr LISTENING|findstr "%zxzb3%" > NUL 2>NUL && mshta vbscript:msgbox("ERROR:%zxzb3% 端口已占用!!!",64,"异常提醒")(window.close) || echo %zxzb3%

:mapportDetect
netstat -aon|findstr "%mapport%" > NUL 2>NUL && mshta vbscript:msgbox("ERROR:%mapport% 端口已占用!!!",64,"异常提醒")(window.close) || echo %mapport%

::::: 安装服务
::: 安装vc2010
:2010Detect
cd /d %~dp0
set "HKLMU=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
::::: 判断系统是否已经安装了 Visual C++ 2010(包括所有版本)
::::: 有就跳过安装Visual C++ 2010,没有则先安装。
reg query %HKLMU%\{196BB40D-1578-3D01-B289-BEFC77A11A1E}>nul 2>nul&&set VC2010=Microsoft Visual C++ 2010 Redistributable X86
reg query %HKLMU%\{DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}>nul 2>nul&&set VC2010=Microsoft Visual C++ 2010 Redistributable X64

if defined VC2010 (echo %VC2005% 已安装!&goto mysqlserviceDetect >nul) else (goto 2010Install)

:2010Install
:::::各版本下载地址 http://www.microsoft.com/downloads/details.aspx?FamilyID=766a6af7-ec73-40ff-b072-9112bab119c2&DisplayLang=zh-cn
"%cd%\mysql\vcredist_x64_2010.exe"


::: 安装mysql
:mysqlserviceDetect
cd /d %~dp0
set "mysqlservice=jx_mysql5.6"
sc query "%mysqlservice%">nul
IF ERRORLEVEL 1060 GOTO mysqlInstall
GOTO mysqlexist


:mysqlInstall
"%cd%\mysql\bin\mysqld.exe" -install %mysqlservice% --defaults-file="%cd%\mysql\my.ini"
sc config %mysqlservice% start=auto
net start %mysqlservice%

::: 安装redis
:redisserviceDetect
cd /d %~dp0
set "redisservice=jx_redis3.2.100"
sc query "%redisservice%">nul
IF ERRORLEVEL 1060 GOTO redisInstall
GOTO redisexist

:redisInstall
"%cd%\redis\redis-server" --service-install --service-name %redisservice% "%cd%\redis\redis.windows-service.conf" --loglevel verbose
sc config %redisservice% start=auto
net start %redisservice%
echo ------------------------------------------------------------------------------

::: 安装tomcat
:javaserviceDetect
cd /d %~dp0
set "javaservice=jx_tomcat8.5_jdk8"
sc query "%javaservice%">nul
IF ERRORLEVEL 1060 GOTO javaInstall
GOTO javaexist

:javaInstall
cd service
call "%cd%\bin\service.bat" install %javaservice%
ping 192.0.2.2 -n 1 -w 10000 > nul
sc config %javaservice% start=auto
net start %javaservice%
ping 192.0.2.2 -n 1 -w 10000 > nul


:mapserviceDetect
cd /d %~dp0
set "mapservice=jx_mapserv0.20.1"
sc query "%mapservice%">nul
IF ERRORLEVEL 1060 GOTO mapInstall
GOTO mapexist

:mapInstall
instsrv.exe jx_mapserv0.20.1 "%cd%\mapser\start.exe"
ping 192.0.2.2 -n 1 -w 3000 > nul
sc config jx_mapserv0.20.1 start=auto
net start jx_mapserv0.20.1
goto END

:mysqlexist
mshta vbscript:msgbox("ERROR:%mysqlservice% 服务已存在!!!",64,"异常提醒")(window.close)
GOTO END

:redisexist
mshta vbscript:msgbox("ERROR:%redisservice% 服务已存在!!!",64,"异常提醒")(window.close)
GOTO END

:javaexist
mshta vbscript:msgbox("ERROR:%javaservice% 服务已存在!!!",64,"异常提醒")(window.close)
GOTO END

:mapexist
mshta vbscript:msgbox("ERROR:%mapservice% 服务已存在!!!",64,"异常提醒")(window.close)
GOTO END

:END
-------------本文结束感谢您的阅读-------------

本文标题:多环境批处理脚本

文章作者:OperationMAN

发布时间:2021年07月12日 - 15:07

最后更新:2022年06月05日 - 21:06

原始链接:https://kxinter.gitee.io/2021/07/12/多环境批处理脚本/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

坚持原创技术分享,您的支持将鼓励我继续创作!