2

Results of research today on this..

REF = How to parse xml file in batch (CMD) NOTE: The comment from jeb to help parse out the port number from 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

Current Result: is the resulting data is being redirected to the log file, but the results of the WGET with the Pipe through using GREP is not capturing to the log.

Have tried: result >> log result 1>> log result2 2>> log result3 3>> log

And is only capturing the result of the ECHO command in the final results.

Starting point: 2014-12-05

Let's figure out why we are not getting the serverport. I have figured this out, but now I need to figure this out and how to get to the first %serverport%. As requested, I have removed FINDSTR and have gone with GREP.

Specifically:

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:"=%

And is because we break out of the script, it is not able to continue to the second instance of Apache Tomcat because of:

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:"=%

So.. Let's see what is going on here..

Step 1, Let's create a DEBUG Batch Script. This will grab the server key for the Windows Service Name. For example, when you run NET STOP tomcat6 or NET START tomcat6 .. We don't want the full registry path, but just the key name. We can use the same construct when trying to determine a folder name in the file system, but this is from the registry.

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

Step 1 results, Let's see what this does..If we take tester.bat > results.txt we see:

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

Step 2, let's see what we get when we start looking for the server port.. NOTE:Each FOR line is all one line.. FOR.. IN.. DO.. FOR.. IN.. DO.. FOR.. IN.. DO.. FOR.. IN.. DO... (yes, that is four of them all in one line)

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

Step 2 results, Let's see what this does..If we take tester.bat > results.txt we see:

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

I only want the first instance of the server port.

This server has two instances of tomcat: port 80 port 8080

It would be simple if it was just this one server, but I have 30 servers I want to report on.

Community
  • 1
  • 1
Leptonator
  • 3,379
  • 2
  • 38
  • 51
  • Sorry - it is not clear. Problem is that it is not capturing the %serverport% variable correctly. This is why I used the example that has two instances of Apache Tomcat (port 80 and port 8080) and are installed at D:\tomcat_1 and D:\Tomcat_2 and the service names are tomcat6_1 and tomcat6_2. Everything else seems fine. – Leptonator Dec 05 '14 at 08:42
  • Just a sidenote: If you're using `grep` in the lower part of the script - why do you even bother using `FINDSTR` in the upper part? `FINDSTR` is [really weird](http://stackoverflow.com/questions/8844868/what-are-the-undocumented-features-and-limitations-of-the-windows-findstr-comman/8844873#8844873) and as @dbenham says "By all means, if you are in a position to use something other than FINDSTR, then that is highly advised". – zb226 Dec 05 '14 at 10:53
  • Can you edit your question and include the line that's captured from `server.xml` to scrape `%serverport%`? Also, you need to move your `:break` code [outside all your `for` code blocks](http://stackoverflow.com/a/8481978/1683264). – rojo Dec 05 '14 at 13:21
  • As requested, I have modified my post to be easier to understand and hopefully it makes more sense. – Leptonator Dec 05 '14 at 17:30
  • 1
    Use the `-m` switch for `grep` and limit your result to one match in your fourth `for` loop, the one with `delims==`. – rojo Dec 05 '14 at 19:02
  • The switch for GREP was on-target.. Thanks! However, I am still getting tripped up on: `SET serverport="8080" protocol` .. really need to remove the quotes and protocol – Leptonator Dec 05 '14 at 19:44
  • Ah. OK. I think this'll work. `FOR /F "skip=1" %%A IN ('..\grep -Eom1 -e "\ – rojo Dec 05 '14 at 20:51
  • @rojo: Tried: which does not work (removed the %% from the variables to test from command line directly) `FOR /F "skip=1" %A IN ('..\grep -Eom1 -e "\ – Leptonator Dec 05 '14 at 21:14
  • Everything seems to be working ok up to the WGET commands.. Have tried to redirect to a log usin >> or 1>> and 2>> and 3>> but is only capturing the first ECHO to the log. – Leptonator Dec 05 '14 at 21:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/66295/discussion-between-leptonator-and-rojo). – Leptonator Dec 05 '14 at 21:22

1 Answers1

1

OK. I am pretty sure that I can create an answer for this. With the nested FOR..IN..DO structures four times, I think that is why I am not able to run the WGET and only the first ECHO works.. Here is where we are at with this code.

REF How to parse xml file in batch (CMD) NOTE: The comment from @jeb to help parse out the port number from 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
    )

Hope this helps somebody.

Community
  • 1
  • 1
Leptonator
  • 3,379
  • 2
  • 38
  • 51