Click to See Complete Forum and Search --> : Batch script with condition?


prepek2000
September 2nd, 2011, 01:39 AM
Hi guys!

I would like to create batch script for doing bunch of databases TNSPING with certain condition.

I have created one batch file aca.bat and put in there this:

tnsping test1.bb.com
tnsping test2.bb.com
tnsping test1.bb.com
.
.
.

then i have executed it from cmd prompt like this aca.bat >>tnsping.log

and in tnsping.log file i have different output reply for each of above databases


D:\DATA\sta8983\Desktop>tnsping test1.bb.com

TNS Ping Utility for 32-bit Windows: Version 10.1.0.2.0 - Production on 01-SEP-2011 11:20:39

Copyright (c) 1997, 2003, Oracle. All rights reserved.

Used parameter files:


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=test.bb.com)(Port=1639))(CONNECT_DATA=(SERVICE_NAME=test1.bb.com)))
OK (300 msec)

D:\DATA\sta8983\Desktop>tnsping test2.bb.com

TNS Ping Utility for 32-bit Windows: Version 10.1.0.2.0 - Production on 01-SEP-2011 11:20:39

Copyright (c) 1997, 2003, Oracle. All rights reserved.

Used parameter files:


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=server.bb.com)(Port=1837))(CONNECT_DATA=(SERVICE_NAME=test2.bb.com)))
TNS-12541: TNS:no listener

D:\DATA\sta8983\Desktop>tnsping test3.bb.com

TNS Ping Utility for 32-bit Windows: Version 10.1.0.2.0 - Production on 01-SEP-2011 11:20:39

Copyright (c) 1997, 2003, Oracle. All rights reserved.

Used parameter files:


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=manage.bb.com)(Port=1740))(CONNECT_DATA=(SID=test3.bb.com)))
TNS-12545: Connect failed because target host or object does not exist

So my question is it possible to create batch job which would going to put in log every dataabse which tnsping didnt finished with message OK at the end? I need to create that kind of list.

Thank you!

Eri523
September 2nd, 2011, 08:36 AM
It's not really complicated if your tnsping tool returns a value to the OS that indicates whether any error has occurred. I don't know that particular program but many such tools do that. They return a value of 0 in case of success, anything else means some sort of error. You may find something about that in the tnsping documentation, but these return codes are often undocumented, so it's worth a try even if you don't find something about that.

Exploiting the return code, you can first redirect the tnsping output to a temporary log file and then append that temporary file to your actual log file only in case of an error. Somewhat like this:


tnsping >temp.log
if errorlevel 1 type temp.log >>tnsping.log


If tnsping doesn't indicate success/failure that way the whole thing becomes much more complicated...