Solved

Clean up log file from batch script

Posted on 2015-01-08
6
249 Views
Last Modified: 2015-01-22
Hi Experts.  I need to clean up a log file from a trouble-shooting tool.  It looks like a long question but it really is just details and examples.

Existing code
@echo off
setlocal

set /p "Computer=Please enter computer name: "
if "%Computer%" EQU "" (
  echo No computer name entered, ending.
  exit /b
)


set logfile=\\w2k3nas01\public\FG_Diagnostic\%computer%.log

echo ----[Diagnostic Run Time]----> %logfile%
echo %date:~4,2%/%date:~7,2%/%date:~10,4%>> %logfile%
echo %time:~0,2%:%time:~3,2%>> %logfile%
echo. >> %logfile%

echo ----[Computer Online Verification]---->>"%logfile%" 
ping.exe -4 -n 2 %Computer% | find.exe /i "TTL" >NUL
if errorlevel 1 (
	echo %Computer% isn't online or pingable >>"%logfile%" 
	start notepad %logfile%
	exit /b 1
)

echo ----[Computer Info]---->> %logfile%
echo %Computer% >> %logfile%
ping -n 2 %computer%>> %logfile%
echo. >> %logfile%

echo ----[Forward nslookup of IP '%Computer%']---->> %logfile%
nslookup.exe %Computer% >>"%LogFile%" 2>&1
set IP=
for /f "tokens=2 delims=[]" %%a in ('ping.exe -n 2 %computer% ^| find.exe /i "Pinging %computer%"') do set IP=%%a

echo ----[Pinging by name '%computer%']---->> %logfile%
ping.exe -n 2 %computer% >>"%LogFile%"
if "%IP%"=="" (
	>>"%LogFile%" echo Could not determine the IP.
) else (
	echo. >> %logfile%
	echo ----[Reverse nslookup of IP '%IP%']---->> %logfile%
	nslookup.exe %IP% >>"%LogFile%" 2>&1	
)

start notepad %logfile%
:eof

Open in new window


Which yields:


----[Diagnostic Run Time]----
01/08/2015
14:30
 
----[Computer Online Verification]----
----[Computer Info]----
csv7shared13

Pinging csv7shared13 [172.24.80.192] with 32 bytes of data:
Reply from 172.24.80.192: bytes=32 time<1ms TTL=128
Reply from 172.24.80.192: bytes=32 time<1ms TTL=128

Ping statistics for 172.24.80.192:
    Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
 
----[Forward nslookup of IP 'csv7shared13']----
Non-authoritative answer:

Server:  server
Address:  172.24.80.6

Name:    csv7shared13
Address:  172.24.80.192

----[Pinging by name 'csv7shared13']----

Pinging csv7shared13 [172.24.80.192] with 32 bytes of data:
Reply from 172.24.80.192: bytes=32 time<1ms TTL=128
Reply from 172.24.80.192: bytes=32 time<1ms TTL=128

Ping statistics for 172.24.80.192:
    Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
 
----[Reverse nslookup of IP '172.24.80.192']----
Server:  server
Address:  172.24.80.6

Name:    csv4shared5
Address:  172.24.80.192

What I want it to look like:

----[Diagnostic Run Time]----
01/08/2015
14:30
 
----[Computer Online Verification]----
----[Computer Info]----
csv7shared13

Pinging csv7shared13 [172.24.80.192] with 32 bytes of data:
Reply from 172.24.80.192: bytes=32 time<1ms TTL=128
Reply from 172.24.80.192: bytes=32 time<1ms TTL=128

----[Forward nslookup of IP 'csv7shared13']----

Name:    csv7shared13
Address:  172.24.80.192

----[Pinging by name 'csv7shared13']----

Pinging csv7shared13 [172.24.80.192] with 32 bytes of data:
Reply from 172.24.80.192: bytes=32 time<1ms TTL=128
Reply from 172.24.80.192: bytes=32 time<1ms TTL=128
 
----[Reverse nslookup of IP '172.24.80.192']----

Name:    csv4shared5
Address:  172.24.80.192

Let me know if this can be done.  Thanks experts for all your help!
0
Comment
Question by:samiam41
  • 4
6 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 40538684
Can't test it properly at the moment, but this should come close:
@echo off
setlocal

set /p "Computer=Please enter computer name: "
if "%Computer%" EQU "" (
  echo No computer name entered, ending.
  exit /b
)

set logfile=\\w2k3nas01\public\FG_Diagnostic\%computer%.log

 >"%logfile%" echo ----[Diagnostic Run Time]----
>>"%logfile%" echo %date:~4,2%/%date:~7,2%/%date:~10,4%
>>"%logfile%" echo %time:~0,2%:%time:~3,2%
>>"%logfile%" echo.

>>"%logfile%" echo ----[Computer Online Verification]----
ping.exe -4 -n 2 %Computer% | find.exe /i "TTL" >NUL
if errorlevel 1 (
	>>"%logfile%" echo %Computer% isn't online or pingable
	start "" notepad.exe "%logfile%"
	exit /b 1
)

>>"%logfile%" echo ----[Computer Info]----
>>"%logfile%" echo %Computer%
ping.exe -n 2 %computer% | findstr.exe /i "Pinging TTL" >>"%logfile%" 
>>"%logfile%" echo.

>>"%logfile%" echo ----[Forward nslookup of IP '%Computer%']----
for /f "skip=4 delims=" %%a in ('nslookup.exe %Computer%') do >>"%logfile%" echo %%a
set IP=
for /f "tokens=2 delims=[]" %%a in ('ping.exe -n 2 %computer% ^| find.exe /i "Pinging %computer%"') do set IP=%%a

>>"%logfile%" echo ----[Pinging by name '%computer%']----
ping.exe -n 2 %computer% | findstr.exe /i "Pinging TTL" >>"%logfile%" 
if "%IP%"=="" (
	>>"%LogFile%" echo Could not determine the IP.
) else (
	echo. >> %logfile%
	echo ----[Reverse nslookup of IP '%IP%']---->> %logfile%
	for /f "skip=4 delims=" %%a in ('nslookup.exe %IP%') do >>"%logfile%" echo %%a
)

start "" notepad.exe "%logfile%"

Open in new window

0
 
LVL 9

Author Comment

by:samiam41
ID: 40538695
Hey oBdA!

Thank you for the information.  I am going to use your format with this new script and going forward so I appreciate your patience as I ask questions.

Testing your suggestion now.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40538825
You already have an answer there by the looks but you may also want to consider using a ( before the first

(echo ----[Diagnostic Run Time]----

and then an extra ) at the end of your logged bit:

      echo ----[Reverse nslookup of IP '%IP%']---->> %logfile%
      for /f "skip=4 delims=" %%a in ('nslookup.exe %IP%') do echo %%a
)
)>"%logfile%"

and redirect once the whole lot to your log file and remove all references to >>%logfile%.

If you want to display something to the screen in your script in the middle you can use echo Whatever > CON at it goes to the screen instead of the log

Steve
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 9

Author Comment

by:samiam41
ID: 40544357
oBdA, it works like a charm.

Steve, I appreciate the information and suggestion.  I will have to test that out but I won't be able to do that now.  I'm still trying to figure out this scripting thing and I don't want to hold up this question as I try to figure out your suggestion.  Unless anyone objects, I will close this one out and open a new question which should allow you to post that information and I can figure it out.

Thanks for everyone's help and time with this question!
0
 
LVL 9

Author Closing Comment

by:samiam41
ID: 40544376
Clean looking log file that will make trouble-shooting much easier.  Thank you!
0
 
LVL 9

Author Comment

by:samiam41
ID: 40564727
Greetings!  If anyone has any time and would like some points, I would appreciate your help.

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_28601914.html
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
l2tp tunnel from pc to router 14 73
cant install rsat on win 7 13 43
smartboard connectivity and software 6 34
Difference bet. 3G v 4G RAM on Win7 32-bit 5 51
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum editing capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now