Jump to content

Build Your Own Network Diagnostic Report


Recommended Posts

I love to share with you my work which based on other people's work although much of it are from my own creativity.

When I said "report", I really mean so. It is a formatted HTML page.

And what I am referring to as "network" can be WAN or LAN, external IP (or host name) or internal IP (or computer name).

I built my own Network Diagnostic Report generator!

It serves my company purpose, as an I.T. Executive, I need such a report that tell me which shops are offline and which shops are not, actually more than that.

First, you need tool to ping a particular port.

PAPING - a Google project, comes handy as a freeware.

Next, for the generation of formatted HTML page as final report, we need:

CSVFIX - a SourceForge project, comes handy as a freeware, too.

CSVFILEVIEW - a CSV to HTML converter (and viewer) from NirSoft, also a freeware itself.

FART - Find And Replace Text, a command-line tool that tweak a bit the HTML output generated by the converter.

My code divided into two sections:

@echo off

set fnm=HOSTNAME.txt

set lnm=RESULT.txt

del %lnm%

if exist HOSTNAME.txt set fnm=HOSTNAME.txt

if exist C:\NT\HOSTNAME.txt set fnm=C:\NT\HOSTNAME.txt

if exist %fnm% goto SALES

echo.

echo Cannot find %fnm%

echo.

Pause

goto :EOF

:SALES

for /f "tokens=1,2 delims=," %%i in (%fnm%) do call :SUB_1 %%i %%j

goto :DVR

:SUB_1

echo Testing %1 for SALES

set state=ONLINE

paping %1 -p 3050 -c 1

if errorlevel 1 set state=**OFFLINE**

echo %2,3050,%state% >> %lnm%

goto :EOF

:DVR

for /f "tokens=1,2 delims=," %%i in (%fnm%) do call :SUB_2 %%i %%j

goto :VNC

:SUB_2

echo Testing %1 for DVR

set state=ONLINE

paping %1 -p 9696 -c 1

if errorlevel 1 set state=**OFFLINE**

echo %2,9696,%state% >> %lnm%

goto :EOF

:VNC

for /f "tokens=1,2 delims=," %%i in (%fnm%) do call :SUB_3 %%i %%j

goto :GEN

:SUB_3

echo Testing %1 for VNC

set state=ONLINE

paping %1 -p 5900 -c 1

if errorlevel 1 set state=**OFFLINE**

echo %2,5900,%state% >> %lnm%

goto :EOF

:GEN

GENERATE.BAT

I actually need to scan three different ports, 3050 (Firebird) and 5900 (RealVNC) related to PC, while 9696 related to CCTV recorder.

Sample of HOSTNAME.txt are:

"subdomain1.dyndns.org","New York"

"subdomain2.dyndns.org","London"

"subdomain3.dlinkddns.com","Paris"

The C:\NT\HOSTNAME.txt has higher precedence than the HOSTNAME.txt resides in the local directory. This is optional.

The following section are generation and formatting of HTML page as the final report:

@echo off

del RESULT_5.txt

del RESULT_4.txt

del RESULT_3.txt

del RESULT_2.txt

del RESULT_U.htm

del RESULT.htm

del TIMESTAMP.txt

csvfix sort -f 1:AS,2:AN -ibl -o RESULT_2.txt RESULT.txt

csvfix flatten -k 1 -o RESULT_3.txt RESULT_2.txt

csvfix sequence -n 1 -i 1 -f 1 -o RESULT_4.txt RESULT_3.txt

echo %date% %time% > TIMESTAMP.txt

copy RESULT_4.txt+TIMESTAMP.txt RESULT_5.txt

csvfileview /load RESULT_5.txt /shtml RESULT_U.htm

type RESULT_U.htm>RESULT.htm

fart RESULT.htm "Text File Report" "Network Diagnostic Report - NT Shop Sdn Bhd"

fart RESULT.htm "www.nirsoft.net/" "www.ntshop.com.my"

fart RESULT.htm "Created by using" " "

fart RESULT.htm "CSVFileView" "www.ntshop.com.my"

fart RESULT.htm "Column 1" "No."

fart RESULT.htm "Column 2" "Store"

fart RESULT.htm "Column 3" "Sales"

fart RESULT.htm "Column 4" "Status"

fart RESULT.htm "Column 5" "VNC"

fart RESULT.htm "Column 6" "Status"

fart RESULT.htm "Column 7" "DVR"

fart RESULT.htm "Column 8" "Status"

del RESULT_5.txt

del RESULT_4.txt

del RESULT_3.txt

del RESULT_2.txt

del RESULT_U.htm

del TIMESTAMP.txt

start RESULT.htm

As you might already noticed, CSVFIX is very powerful command-line tool for handling CSV files.

I use it to sort hostname ascendingly, then port number descendingly.

I even use it to combine multiple rows that having the same hostname but just different port number and ping result.

Finally, I use it to add sequential number to the first column of the CSV file.

Then, I generated a timestamp to another file (to be concatenated to the bottom of the report).

CSVFILEVIEW is used to convert the CSV file to HTML file, in non-verbose mode.

Next, I save the HTML file from Unicode format to ANSI format (or otherwise FART cannot search and replace double-byte strings).

And now, I use FART to replace the original name and title of the HTML file with my company name and report title.

Show it.

All these are done in a command-line window.

I have only tested them on Windows 7. I will test them on Windows XP tomorrow in office.

Please let me know if there is any defects.

And feel free to share it.

RESULT.htm

Link to comment
Share on other sites


It seems to me like an overcomplex way.

The batch can be much simplified, quick example:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set fnm=HOSTNAME.txt
set lnm=RESULT.txt
del %lnm%
if exist HOSTNAME.txt set fnm=HOSTNAME.txt
if exist C:\NT\HOSTNAME.txt set fnm=C:\NT\HOSTNAME.txt
if not exist %fnm% goto :error

set SALES=3050
set DVR=9696
set VNC=5900

FOR %%A IN ( SALES DVR VNC ) DO (
ECHO %%A
for /f "tokens=1,2 delims=," %%i in (%fnm%) do call :do_paping %%i %%j %%A
)

::GENERATE.BAT

GOTO :EOF

:do_paping
set state=ONLINE
echo Testing %1 for %3
paping %1 -p !%3! -c 1
if errorlevel 1 set state=**OFFLINE**
echo %2,!%3!,%state% >> %lnm%
goto :EOF

:error
echo.
echo Cannot find %fnm%
echo.
Pause
goto :EOF

And most probably you can even remove the if errorlevel by using either of || or &&.

jaclaz

Link to comment
Share on other sites

Look like you wanted to reinvent angry ip scanner or zenmap and both are opensource.

Not really. I was Angry IP Scanner user for the past three months.

It prints IP addresses, ports and ping status with title and timestamp.

But I wanted more, a customized and yet presentable report. When I mean presentable, it does not (only) print machine-readable IP addresses, I want to have more friendlier name like store outlet location which is more human-readable. When I mean customization, I want to have my company title on top of the page, with hyperlink to my company Web site, etc.

Angry IP Scanner is great. But a command-line tool like PAPING coupled with my HTML formatting scripts will build a report tailored-specific to our needs.

Thank you for your input. I appreaciate that. I like to hear when people make replies.

Link to comment
Share on other sites

It seems to me like an overcomplex way.

The batch can be much simplified, quick example:

You're a genius, the coding skill level is way much higher than mine.

When you see my way overcomplex, and you went on to simplify it ---- it means overcomplex, too, for beginner like me. Just kidding!

Really, thank you for your input. Much of the quality software and technology are from the West on the earth, I will be continuously learning new skill from you and people like you.

:thumbup

Link to comment
Share on other sites

jaclaz

And now I realized that I made something silly: I use quotation for code snippet. I should insert as code snippet. That is why I had always been wondering why everyone of you have the initiative to make coloring to the syntax and identifier of the code.

Link to comment
Share on other sites

Much of the quality software and technology are from the West on the earth,

Not really, there are excellent programmers in the East as well. :thumbup

What you may learn from the West (though I presume that there are similar approaches in the East) is ;) :

http://en.wikipedia.org/wiki/Occam's_razor

And:

Make everything as simple as possible, but not simpler.

Here :whistle: :

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set Htm_title=Network Diagnostic Report - NT Shop Sdn Bhd
set Htm_href="http://www.ntshop.com.my"
set Htm_txt=www.ntshop.com.my

set fnm=HOSTNAME.txt
set lnm=RESULT.HTM

del %lnm%

(
ECHO ^<^!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"^>
ECHO ^<html^>^<head^>^<title^>%Htm_title%^</title^>^</head^>
ECHO ^<body^>^<h3^>%Htm_title%^</h3^>
ECHO ^<br^>^<h4^> ^<a href=%Htm_href% target="newwin"^>%Htm_txt%^</a^>^</h4^>^<p^>^<table border="1" cellpadding="5"^>^<tr bgcolor="E0E0E0"^>
ECHO ^<th^>No.^<th^>Store^<th^>Sales^<th^>VNC^<th^>DVR
)>>%lnm%

if exist HOSTNAME.txt set fnm=HOSTNAME.txt
if exist C:\NT\HOSTNAME.txt set fnm=C:\NT\HOSTNAME.txt

::if not exist %fnm% goto :error

set SALES=3050
set DVR=9696
set VNC=5900
set Counter=0

for /f "tokens=1,2 delims=," %%A in (%fnm%) do (
Set /A Counter+=1
set Line=^<tr^>^<td bgcolor=#FFFFFF^>!Counter!^<td^>%%B
FOR %%C IN ( SALES DVR VNC ) DO (
call :do_paping %%A %%B %%C
)
ECHO !Line!>>%lnm%

)

(
ECHO ^</table^>
ECHO ^</body^>^</html^>
)>>%lnm%

GOTO :EOF

:do_paping
set state=ONLINE
echo Testing %1 for %3
paping.exe %1 -p !%3! -c 1 || set state=**OFFLINE**
SET Line=!Line!^<td^>%state%
goto :EOF

:error
echo.
echo Cannot find %fnm%
echo.
Pause
goto :EOF

The expected result is a simple .htm table, let's simply build it....

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

Not really, there are excellent programmers in the East as well. :thumbup

You may well mean India and China. Let alone Korea and Japan.

What you may learn from the West (though I presume that there are similar approaches in the East) is ;) :

http://en.wikipedia.org/wiki/Occam's_razor

I studied the Holy Scriptures long time ago. (Although it was originated in the Middle East)

Don't count me in as one of you because of Biblical studies, but because of my I.T. background, a loyal fan to Microsoft (only) products and services.

When the philosophy mentions "simplicity", the first thing that comes to my mind is Microsoft new logo with its Windows 8 UI. It is not funny, however, as sooner or later I will be embracing this new family of OS from Microsoft.

And:

Make everything as simple as possible, but not simpler.

Here :whistle: :

...... let's simply build it....

My adrenaline is rising up. :yes:

Tell you what, I will come back anytime in the future and take this (piece of code) for granted. THIS IS YOUR THREAD! You taught me back. :D

Link to comment
Share on other sites

your off to a great start, but when you throw in some emailing scripts, to let you know when a site is down, then you will have stepped up your game in coding

Thank you for your praise. But I do not quite understand your slang, did you mean scripting and coding are two different things?

Link to comment
Share on other sites

what i mean is, it's one thing to make some cool batch file that does all the reporting.

it's another to create a batch file that runs all the time, and emails you when a site is down.

Oh, I see.

E-mail part is tricky, have to specify SMTP server, login details and alternative port number, etc.

You know that I was looking for something like that, instead of e-mailing, why not texting (or sending SMS) directly to my cell phone (or handphone, mobile phone).

And, AFAIK, some modems that I have seen allow notification through e-mails if the link is down, so are some DVRs. But this is more a hardware-approach.

So, net_user do you have that very batch that does the things you told me: ping site and send notification e-mails? I am not requesting one, but just curious to know if you already have one.

Link to comment
Share on other sites

you have to do this in steps.

first step...google vbs email script, there is a vbs script out there that can log you on to your gmail and send emails...even to cell numbers

start with that first

edit****

once you have the vbs script configured...you have to call it from the command line via cscript

;)

Edited by net_user
Link to comment
Share on other sites

google vbs email script, there is a vbs script out there that can log you on to your gmail and send emails...even to cell numbers

once you have the vbs script configured...you have to call it from the command line via cscript

;)

Thanks for the hints. It will be very useful if I found one.

But why should one call a VBS script via CSCRIPT?

Link to comment
Share on other sites

the vbs script contains all the configurations to logon to a gmail account,

you create a batch file that calls the vbs script , with a certian subject line and attach a txt file, (mail server is down in newyork)

here is a batch file....

cscript c:\sendemail.vbs 1234567890@txt.att.net "" "mailserver is down"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...