Solved

Clean up log file from batch script

Posted on 2015-01-08
6
245 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
This Micro Tutorial will teach you how to the overview of Microsoft Security Essentials. This is a free anti-virus software that guards your PC against viruses, spyware, worms, and other malicious software. This will be demonstrated using Windows…
This Micro Tutorial will teach you how to change your appearance and customize your Windows 7 interface to your unique preference. This will be demonstrated using Windows 7 operating system.

743 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

15 Experts available now in Live!

Get 1:1 Help Now