Solved

Clean up log file from batch script

Posted on 2015-01-08
6
262 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
[X]
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
  • 4
6 Comments
 
LVL 85

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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

729 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