Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Clean up log file from batch script

Posted on 2015-01-08
6
251 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 84

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
A quick guide on how to use Group Policy to create a custom power plan and set it active on Windows 7.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
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.

856 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