MSFN Forum: Build Your Own Network Diagnostic Report - MSFN Forum

Jump to content


  • 2 Pages +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Build Your Own Network Diagnostic Report Using command-line tools and batch scripts. Rate Topic: -----

#1 User is offline   FlierMate 

  • End User
  • Pip
  • Group: Members
  • Posts: 68
  • Joined: 12-November 11
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 02 December 2012 - 08:23 AM


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:

Quote


@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:

Quote


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

Quote


@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.

Attached File(s)




#2 User is offline   allen2 

  • Not really Newbie
  • PipPipPipPipPipPipPip
  • Group: Members
  • Posts: 1,736
  • Joined: 13-January 06

Posted 02 December 2012 - 10:50 AM

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

#3 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,433
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 02 December 2012 - 12:19 PM

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

#4 User is offline   FlierMate 

  • End User
  • Pip
  • Group: Members
  • Posts: 68
  • Joined: 12-November 11
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 03 December 2012 - 07:01 AM

View Postallen2, on 02 December 2012 - 10:50 AM, said:

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.

#5 User is offline   FlierMate 

  • End User
  • Pip
  • Group: Members
  • Posts: 68
  • Joined: 12-November 11
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 03 December 2012 - 07:14 AM

View Postjaclaz, on 02 December 2012 - 12:19 PM, said:

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

#6 User is offline   FlierMate 

  • End User
  • Pip
  • Group: Members
  • Posts: 68
  • Joined: 12-November 11
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 03 December 2012 - 08:16 AM

View Postjaclaz, on 02 December 2012 - 12:19 PM, said:

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.

#7 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,433
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 03 December 2012 - 09:20 AM

View PostFlierMate, on 03 December 2012 - 07:14 AM, said:

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:

Albert Einstein said:

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

This post has been edited by jaclaz: 03 December 2012 - 09:21 AM


#8 User is offline   FlierMate 

  • End User
  • Pip
  • Group: Members
  • Posts: 68
  • Joined: 12-November 11
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 03 December 2012 - 09:55 AM

View Postjaclaz, on 03 December 2012 - 09:20 AM, said:

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


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

View Postjaclaz, on 03 December 2012 - 09:20 AM, said:

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.

View Postjaclaz, on 03 December 2012 - 09:20 AM, said:

And:

Albert Einstein said:

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

#9 User is offline   net_user 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 371
  • Joined: 02-June 05
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 06 December 2012 - 05:00 PM

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

#10 User is offline   FlierMate 

  • End User
  • Pip
  • Group: Members
  • Posts: 68
  • Joined: 12-November 11
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 10 December 2012 - 07:26 AM

View Postnet_user, on 06 December 2012 - 05:00 PM, said:

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?

#11 User is offline   net_user 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 371
  • Joined: 02-June 05
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 10 December 2012 - 09:50 AM

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.

#12 User is offline   FlierMate 

  • End User
  • Pip
  • Group: Members
  • Posts: 68
  • Joined: 12-November 11
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 10 December 2012 - 09:59 AM

View Postnet_user, on 10 December 2012 - 09:50 AM, said:

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.

#13 User is offline   net_user 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 371
  • Joined: 02-June 05
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 10 December 2012 - 10:10 AM

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
;)

This post has been edited by net_user: 10 December 2012 - 10:15 AM


#14 User is offline   FlierMate 

  • End User
  • Pip
  • Group: Members
  • Posts: 68
  • Joined: 12-November 11
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 10 December 2012 - 10:57 AM

View Postnet_user, on 10 December 2012 - 10:10 AM, said:

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?

#15 User is offline   net_user 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 371
  • Joined: 02-June 05
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 10 December 2012 - 11:02 AM

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"

#16 User is offline   FlierMate 

  • End User
  • Pip
  • Group: Members
  • Posts: 68
  • Joined: 12-November 11
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 10 December 2012 - 11:07 AM

View Postnet_user, on 10 December 2012 - 11:02 AM, said:

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


I learnt something new about CSCRIPT.
Alright, I know what you meant right now.
Thank you.

#17 User is offline   net_user 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 371
  • Joined: 02-June 05
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 10 December 2012 - 11:12 AM

once you have the emailing part down,

now you create the batch file/programming, how ever you want....to ping your stuff
when it gets a failed pinging...have it call the batch file..with the cscrpits commands in it.

#18 User is offline   FlierMate 

  • End User
  • Pip
  • Group: Members
  • Posts: 68
  • Joined: 12-November 11
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 10 December 2012 - 11:20 AM

View Postnet_user, on 10 December 2012 - 11:12 AM, said:

when it gets a failed pinging...have it call the batch file..with the cscrpits commands in it.


This part I know. I have the complete picture right now.

BTW, does the script work for you well? Accurate? Do you think should increase the timeout values for slow connections (so that it won't consider it a dead host)?

#19 User is offline   net_user 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 371
  • Joined: 02-June 05
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 10 December 2012 - 11:24 AM

it works perfect for me....it all depends on your situation really
how good is your vpn..ect ect...do you get any dropoffs?
how often do you need to check?

dude...you said your a I.T. Exec
ask yourself those questions and program and create to your needs

#20 User is offline   FlierMate 

  • End User
  • Pip
  • Group: Members
  • Posts: 68
  • Joined: 12-November 11
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 10 December 2012 - 11:29 AM

View Postnet_user, on 10 December 2012 - 11:24 AM, said:

it works perfect for me....it all depends on your situation really


I have answered my own question, increase timeout values and return as alive host for slower connections will work in my situation.

Thank you once again.

Share this topic:


  • 2 Pages +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2013 msfn.org
Privacy Policy