We help IT Professionals succeed at work.

help to enhance batch script to output to html with colors

749 Views
Last Modified: 2017-05-08
I'm given hundreds of outputs from Cisco switches / routers so instead of
 manually checking them for compliance to hardening standards, need
 a script to gen reports.  So,

 I'm given  hundreds of Cisco "show run" outputs in the form device1.txt,
device2.txt, deviceX.txt...,  all saved into one common folder
 where deviceX  is the hostname of the switch/router/device.

Pls correct the syntax/logic of my script below if it's incorrect.

Have batch script below but to make it look professional, ideally the
outputs can be in table form & colored in red for certain items:

=================== batch script : ==========================

echo off

for %%f in (*.txt) do call:FindTextInFile "enable secret 5" "%%f"
for %%f in (*.txt) do call:FindTextInFile "service password-encryption" "%%f"
for %%f in (*.txt) do call:FindTextInFile "security passwords min-length 8" "%%f"
for %%f in (*.txt) do call:FindTextInFile "privilege level 15" "%%f"
for %%f in (*.txt) do call:FindTextInFile "exec-timeout 5 0" "%%f"
REM ...  repeat for other parameters ...


:FindTextInFile
findstr /i /m "%~1" "%~2"
if %errorlevel%==0 (
echo "%~1 is enabled, ==> Compliant") >> %%f.html

goto End

:End
echo "%~1 is non Compliant") >> %%f.html   REM put this line in red text

Open in new window

Comment
Watch Question

Author

Commented:
also need to output a line at the top of the html output that
"Security Hardening Compliance report for deviceX  on YYYY/MM/DD "
where deviceX is the device hostname
Dan CraciunIT Consultant
CERTIFIED EXPERT

Commented:
<Not a solution>

Is this a business requirement to use VBS or Batch?
In this age, Powershell is everywhere in the Windows world. And not only is Powershell code way more readable than Batch, but it has dedicated libraries for HTML export.

For ex: https://technet.microsoft.com/en-us/library/ff730936.aspx

HTH,
Dan
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
It is indeed much easier with PowerShell, but feasible with a cnd batch, as long as we do not have to care about too much formatting ...
dbruntonQuid, Me Anxius Sum?  Illegitimi non carborundum.
CERTIFIED EXPERT

Commented:
You need ANSI sequences if you want colour.

Now this is where it gets tricky.  ANSI hasn't been supported since ... well a long time ago.  And possibly now (yep, Windows 10 is going that way).

However there may be some alternatives ...

You could try ansicon  http://qastuffs.blogspot.co.nz/2011/02/how-to-install-ansicon-for-cucumber-to.html (You'll probably need ver 1.52 if you go this way.  Now as for the syntax for ansicon you might be flying by yourself as I need some sleep ...

Also go to https://gist.github.com/mlocati/fdabcaeb8071d5c75a2d51712db24011#file-win10colors-cmd and download the cmd file and run it.  You'll get some nice colours on the screen.

Experiment.
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
dbrunton, please read again: html with colors. So the colour should be HTML code, no ANSI sequence for the console output ;-).

Author

Commented:
Anyone can help amend the above batch script to insert the html characters & color?
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
If you are looking only for a HTML fragment:
:FindTextInFile
findstr /i /m "%~1" "%~2" >nul && (
  >> %%f.htm echo "<font color=#000000>%~1 is enabled, ==> Compliant</font>"
) || (
  >> %%f.htm echo "<font color=#ff0000>%~1 is non Compliant</font>"
)

Open in new window

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Can you provide a robust sample set of TXT input files for testing, with examples of the various errors, etc?  That would make testing a lot easier here, and a more accurate result...

~bp
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Do you want each HTML report file to be a separate file, or would you want a single output file with the filename as a column in it - since you mentioned wanting table output just trying to understand what your requirement is?  Having hundreds of output HTML files doesn't seem to make sense, but you may be thinking something different than I am.

~bp
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
That's a good point, in particular since the resulting file names will be of style    filename.txt.html.
And referencing back to the loop var %%f in FindTextInFile is bad style, in particular since %1 is the same as %%f there.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Here is a shot at a single basic HTML report from your searches, and the resulting output file I got during a test here, let me know if this is close to what you were thinking...

@echo off

rem Define folders and files to work with
set BaseDir=B:\EE\EE29014986\Files
set ReportFile=B:\EE\EE29014986\Report.html

rem Delete report file if exists
if exist "%ReportFile%" del "%ReportFile%"

rem Write HTML header info to report file
call :HtmlBegin

rem Process files looking for desired tags
for %%f in ("%BaseDir%\*.txt") do call :FindTextInFile "enable secret 5" "%%~f"
for %%f in ("%BaseDir%\*.txt") do call :FindTextInFile "service password-encryption" "%%~f"
for %%f in ("%BaseDir%\*.txt") do call :FindTextInFile "security passwords min-length 8" "%%~f"
for %%f in ("%BaseDir%\*.txt") do call :FindTextInFile "privilege level 15" "%%~f"
for %%f in ("%BaseDir%\*.txt") do call :FindTextInFile "exec-timeout 5 0" "%%~f"
REM ...  repeat for other parameters ...

rem Write HTML closing info to report file
call :HtmlEnd

rem End mainline here (don't drop into subroutines)
exit /b


:FindTextInFile
  rem Look for this tag in this file, report result in HTML table
  echo ^<tr^>>>"%ReportFile%"
  echo ^<td^>%~2^</td^>>>"%ReportFile%"
  findstr /i /m "%~1" "%~2">NUL
  if %errorlevel% == 0 (
    echo ^<td^>%~1^</td^>>>"%ReportFile%"
    echo ^<td^>Compliant^</td^>>>"%ReportFile%"
  ) else (
    echo ^<td^>%~1^</td^>>>"%ReportFile%"
    echo ^<td class="warning"^>Non-Compliant^</td^>>>"%ReportFile%"
  )
  echo ^</tr^>>>"%ReportFile%"
  exit /b

:HtmlBegin
  rem HTML document heade
  echo ^<!doctype html^>>>"%ReportFile%"
  echo ^<html^>>>"%ReportFile%"
  echo ^<head^>>>"%ReportFile%"
  echo ^<title^>Cisco Status Report^</title^>>>"%ReportFile%"

  rem Inline CSS styles
  echo ^<style^>>>"%ReportFile%"

  rem Change BODY font
  echo body {>>"%ReportFile%"
  echo font-family: Verdana,Geneva,sans-serif;>>"%ReportFile%"
  echo }>>"%ReportFile%"

  rem Change H1 color
  echo h1 {>>"%ReportFile%"
  echo color: blue;>>"%ReportFile%"
  echo }>>"%ReportFile%"

  rem Add border to table and cells
  echo table, th, td {>>"%ReportFile%"
  echo border: 1px solid gray;>>"%ReportFile%"
  echo }>>"%ReportFile%"

  rem Add padding to table cells
  echo th, td {>>"%ReportFile%"
  echo padding: 5px;>>"%ReportFile%"
  echo }>>"%ReportFile%"

  rem Add red color to warning class for table cells
  echo .warning {>>"%ReportFile%"
  echo color: red;>>"%ReportFile%"
  echo }>>"%ReportFile%"

  rem End of inline CSS stules
  echo ^</style^>>>"%ReportFile%"

  rem Close HEAD, start BODY
  echo ^</head^>>>"%ReportFile%"
  echo ^<body^>>>"%ReportFile%"
  echo ^<h1^>Cisco Status Report - %DATE%^</h1^>>>"%ReportFile%"

  rem Establish table for results
  echo ^<table^>>>"%ReportFile%"
  echo ^<tr^>>>"%ReportFile%"
  echo ^<th^>File^</th^>>>"%ReportFile%"
  echo ^<th^>Setting^</th^>>>"%ReportFile%"
  echo ^<th^>Status^</th^>>>"%ReportFile%"
  echo ^</tr^>>>"%ReportFile%"
  exit /b

:HtmlEnd
  rem Close basic HTML elements
  echo ^</table^>>>"%ReportFile%"
  echo ^</body^>>>"%ReportFile%"
  echo ^</html^>>>"%ReportFile%"
  exit /b

Open in new window

~bp



Report.html

Author

Commented:
Bill, I'm looking at outputting to hundreds of html files, not one single combined html output ie
config of device1.txt  ==> device1.html
config of device2.txt  ==> device2.html
...   all the deviceX.txt are in one folder ...


Attached are the list of text strings I'm looking for in the hundreds of Cisco config files to
check for compliance.
CisHardeng.docx
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
My simple modification of your code in #a42086444 should do then. No tabular display.

Author

Commented:
Qlemo,   so #ff0000  would make the font red color ?

>> %%f.htm echo "<font color=#ff0000>%~1 is non Compliant</font>"

Author

Commented:
Possible to insert tabs so that the output text "Compliant" & "non-Compliant" are aligned vertically

Author

Commented:
echo color: red;>>"%ReportFile%"
Appears that Bill's script make the font red using a different method


   rem Add border to table and cells
   echo table, th, td {>>"%ReportFile%"
   echo border: 1px solid gray;>>"%ReportFile%"
Presume the above codes from Bill is creating tables to contain the text
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
Yes, #ff0000 is red. It means "red = 255, green = 0, blue = 0" in RGB scale.

Yes, Bill is using a different approach - a CSS (cascading style sheet), which allows for separating format and content by defining and modifying styles you can use in the text area. You e.g. describe the look of a table once, and then each table you create in the HTML document has that look.
Bill's approach for building real HTML tables is best here if you need tabular display, but it needs some setup in the batch. And honestly. you asked for colours not tabular display in your question, and strictly seen it is answered.
I'll leave it to Bill to modify his code to use different files instead of a single report.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
I am traveling this week so won't be able to make changes until next week.  Interestingly I started to work a solution with individual files but decided that probably was wrong and adjusted to a single output file. If you want to wait a few days I can break apart when I return.

~bp
Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.