检查Web服务器并且不从同一服务器上的配置文件解析端口80和端口8080

时间:2014-12-04 22:59:36

标签: windows apache tomcat batch-file wget

研究结果今天就此......

REF = How to parse xml file in batch (CMD) 注意:来自jeb的注释帮助解析xml中的端口号

SET results=%CD%\somelog.log

IF EXIST %results% DEL %results%

REM NOTE: THIS IS ALL ONE LINE!!!!  ALL THE WAY DOWN TO THE PAUSE STATMENT.
FOR /F "usebackq tokens=1" %%q in (`reg query \\some_server\HKLM\SYSTEM\CurrentControlSet\services ^| find /i "tomcat"`) DO
FOR /F "usebackq tokens=3" %%a in (`reg query \\some_server\HKLM\SYSTEM\CurrentControlSet\services\%%~nxq /v ImagePath`) DO
FOR /F "tokens=1-2,* delims=:\" %%1 IN ("%%a") DO
FOR /F tokens^=1^,2^ delims^=^" %%A IN ('..\grep -m 1 "\<Connector port\=" \\some_server\%%1$\%%2\conf\server.xml') DO
ECHO "CHECKING WEB PAGE CONTENT FROM URL http://some_server:%%B/server_page.htm" 1>> %results% &&
..\WGET -q "http://some_server:%%B/server_page.htm" -O - | ..\GREP "Configuration framework is not loaded" 2>> %results% &&
..\WGET -q "http://some_server:%%B/server_page.htm" -O - | ..\GREP "Successfully acquired a database connection" 3>> %results% 

pause

当前结果:是结果数据被重定向到日志文件,但WGET与管道通过使用GREP的结果没有捕获到日志。

尝试过: 结果&gt;&gt;日志 结果1&gt;&gt;日志 result2 2&gt;&gt;日志 result3 3&gt;&gt;日志

并且只在最终结果中捕获ECHO命令的结果。

起点: 2014-12-05

让我们弄清楚为什么我们没有获得服务器端口。我已经想到了这一点,但现在我需要弄清楚这一点以及如何到达第一个%serverport%。根据要求,我已经删除了FINDSTR并且已经使用了GREP。

具体做法是:

REM >>  here is the problem: if we want to break out for the first port
SET serverport=%%B && GOTO :BREAK)
    :break
    SET serverport=%serverport: protocol=%
    SET serverport=%serverport:"=%

因为我们突破了脚本,因为以下原因无法继续使用Apache Tomcat的第二个实例:

FOR /F "tokens=1-2* delims==" %%A IN ('..\grep "\<Connector port\=" %tpath%') DO (
    SET serverport=%%B
    goto :break
    )
    :break
    SET serverport=%serverport: protocol=%
    SET serverport=%serverport:"=%

所以..让我们看看这里发生了什么......

第1步,让我们创建一个DEBUG批处理脚本。这将获取Windows服务名称的服务器密钥。例如,当您运行NET STOP tomcat6或NET START tomcat6时..我们不想要完整的注册表路径,而只需要密钥名称。在尝试确定文件系统中的文件夹名称时,我们可以使用相同的构造,但这是来自注册表。

echo test 1

FOR /F "usebackq tokens=1" %%q in (`reg query \\some_server\HKLM\SYSTEM\CurrentControlSet\services ^| find /i "tomcat"`) DO echo %%~nxq

echo end of test 1

第1步结果让我们看看这是做什么的。如果我们选择tester.bat&gt; results.txt我们看到:

D:\WORK\Scripts\test>echo test 1 
test 1

D:\WORK\Scripts\test>FOR /F "usebackq tokens=1" %q in (`reg query \\some_server\HKLM\SYSTEM\CurrentControlSet\services | find /i "tomcat"`) DO echo %~nxq 

D:\WORK\Scripts\test>echo tomcat6_1 
tomcat6_1

D:\WORK\Scripts\test>echo tomcat6_2 
tomcat6_2

D:\WORK\Scripts\test>echo end of test 1 
end of test 1

第2步,让我们看看当我们开始寻找服务器端口时会得到什么。 注意:每条FOR线都是一条线.. FOR .. IN .. DO .. FOR .. IN .. DO .. FOR .. IN .. DO .. FOR .. IN .. DO ...(是的,其中四个都在一行中)

echo test 2

FOR /F "usebackq tokens=1" %%q in (`reg query \\some_server\HKLM\SYSTEM\CurrentControlSet\services ^| find /i "tomcat"`) DO
FOR /F "usebackq tokens=3" %%a in (`reg query \\some_server\HKLM\SYSTEM\CurrentControlSet\services\%%~nxq /v ImagePath`) DO
FOR /F "tokens=1-2,* delims=:\" %%1 IN ("%%a") DO
FOR /F "tokens=1-2* delims==" %%A IN ('..\grep "\<Connector port\=" \\some_server\%%1$\%%2\conf\server.xml') DO (SET serverport=%%B)
echo end of test 2

echo we are done

第2步结果,让我们看看它的作用。如果我们选择tester.bat&gt; results.txt我们看到:

D:\WORK\Scripts\test>echo test 2 
test 2

D:\WORK\Scripts\test>FOR /F "usebackq tokens=1" %q in (`reg query \\some_server\HKLM\SYSTEM\CurrentControlSet\services | find /i "tomcat"`) DO FOR /F "usebackq tokens=3" %a in (`reg query \\some_server\HKLM\SYSTEM\CurrentControlSet\services\%~nxq /v ImagePath`) DO FOR /F "tokens=1-2,* delims=:\" %1 IN ("%a") DO FOR /F "tokens=1-2* delims==" %A IN ('..\grep "\<Connector port\=" \\some_server\%1$\%2\conf\server.xml') DO (SET serverport=%B ) 

D:\WORK\Scripts\test>FOR /F "usebackq tokens=3" %a in (`reg query \\some_server\HKLM\SYSTEM\CurrentControlSet\services\tomcat6_1 /v ImagePath`) DO FOR /F "tokens=1-2,* delims=:\" %1 IN ("%a") DO FOR /F "tokens=1-2* delims==" %A IN ('..\grep "\<Connector port\=" \\some_server\%1$\%2\conf\server.xml') DO (SET serverport=%B ) 

D:\WORK\Scripts\test>FOR /F "tokens=1-2,* delims=:\" %1 IN ("d:\tomcat_1\bin\tomcat6.exe") DO FOR /F "tokens=1-2* delims==" %A IN ('..\grep "\<Connector port\=" \\some_server\%1$\%2\conf\server.xml') DO (SET serverport=%B ) 

D:\WORK\Scripts\test>FOR /F "tokens=1-2* delims==" %A IN ('..\grep "\<Connector port\=" \\some_server\d$\tomcat_1\conf\server.xml') DO (SET serverport=%B ) 

D:\WORK\Scripts\test>(SET serverport="80" protocol ) 

D:\WORK\Scripts\test>(SET serverport="8443" protocol ) 

D:\WORK\Scripts\test>(SET serverport="8009" protocol ) 

D:\WORK\Scripts\test>FOR /F "usebackq tokens=3" %a in (`reg query \\some_server\HKLM\SYSTEM\CurrentControlSet\services\tomcat6_2 /v ImagePath`) DO FOR /F "tokens=1-2,* delims=:\" %1 IN ("%a") DO FOR /F "tokens=1-2* delims==" %A IN ('..\grep "\<Connector port\=" \\some_server\%1$\%2\conf\server.xml') DO (SET serverport=%B ) 

D:\WORK\Scripts\test>FOR /F "tokens=1-2,* delims=:\" %1 IN ("d:\tomcat_2\bin\tomcat6.exe") DO FOR /F "tokens=1-2* delims==" %A IN ('..\grep "\<Connector port\=" \\some_server\%1$\%2\conf\server.xml') DO (SET serverport=%B ) 

D:\WORK\Scripts\test>FOR /F "tokens=1-2* delims==" %A IN ('..\grep "\<Connector port\=" \\some_server\d$\tomcat_2\conf\server.xml') DO (SET serverport=%B ) 

D:\WORK\Scripts\test>(SET serverport="8080" protocol ) 

D:\WORK\Scripts\test>(SET serverport="8443" protocol ) 

D:\WORK\Scripts\test>(SET serverport="8010" protocol ) 

D:\WORK\Scripts\test>echo end of test 2 
end of test 2

D:\WORK\Scripts\test>echo we are done 
we are done

我只想要服务器端口的第一个实例。

此服务器有两个tomcat实例: 80号港口 8080港口

如果它只是这一台服务器就很简单,但我想要报告30台服务器。

1 个答案:

答案 0 :(得分:1)

行。我很确定我可以为此创建一个答案。使用嵌套的FOR..IN..DO结构四次,我认为这就是为什么我无法运行WGET并且只有第一个ECHO正常工作。这就是我们使用此代码的地方。

参考 How to parse xml file in batch (CMD) 注意:来自@jeb的评论,以帮助从xml中解析出端口号

SET servers=%CD%\monitored_computers.txt
SET results=%CD%\somelog.log

IF EXIST %results% DEL %results%

FOR /f "tokens=2-8 delims=.:/ " %%a IN ("%date% %time: =0%") DO ECHO PROCESS CHECK STARTED %%a/%%b/%%c %%d:%%e:%%f.%%g >> %results%
    FOR /F "tokens=1-2* delims=," %%A IN (%servers%) DO (
        REM NOTE: THIS IS ALL ONE LINE!!!!  ALL THE WAY DOWN TO THE PAUSE STATMENT.
        FOR /F "usebackq tokens=1" %%q in (`reg query \\%%A\HKLM\SYSTEM\CurrentControlSet\services ^| find /i "tomcat"`) DO
        FOR /F "usebackq tokens=3" %%a in (`reg query \\%%A\HKLM\SYSTEM\CurrentControlSet\services\%%~nxq /v ImagePath`) DO
        FOR /F "tokens=1-2,* delims=:\" %%1 IN ("%%a") DO
        FOR /F tokens^=1^,2^ delims^=^" %%H IN ('..\grep -m 1 "\<Connector port\=" \\%%A\%%1$\%%2\conf\server.xml') DO
        ECHO "http://%%A:%%I/some_page.htm" >> %CD%\servers.txt
            pause
    )

希望这有助于某人。