识别相关tnsnames的位置并回显到控制台 [英] identifying the location of the relevant tnsnames and echoing to the console

查看:59
本文介绍了识别相关tnsnames的位置并回显到控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

In a previous post, I was working on using VBS to list all of the drivers installed on a machine. I am particularily interested in the Oracle drivers.

现在,我想接下来捕获适用的tnsnames.ora文件的内容.在我们的环境中的实践中,假设一台机器可能同时安装了32位和64位驱动程序,那么我最多希望有3个tnsnames.ora文件.然后,如果有为%TNS_ADMIN%定义的系统变量,我希望32位admin文件夹中有1个tnnamesora文件,63位文件夹中有1个tnnamesora文件,而%TNS_ADMIN%文件夹中有1个tnnamesora文件.在我的机器上,我的%TNS_ADMIN%系统变量设置为C:\ Windows \ TNS,并且一旦设置,我就不需要在32位和64位位置使用单独的tbnsnames.ora文件. %TNS_ADMIN%位置中的一个(如果已定义)将由eiether驱动程序使用.在我的PC上,32位和64位tnsnames.ora文件的位置分别是:

Now, I'd like to next capture the contents of the applicable tnsnames.ora files. In practice in our environment, I would expect up to 3 tnsnames.ora fuiles with the assumption that a machine might have both the 32 bit and 64 bit drivers installed. I would then expect 1 tnnamesora file in the 32 bit admin folder, 1 in the 63 bit folder and one in the %TNS_ADMIN% folder, if there is a system variable defined for %TNS_ADMIN%. On my machine, my %TNS_ADMIN% system variable is set to C:\Windows\TNS and once set, I need not have a separate tbnsnames.ora file in the 32 and 64 bit locations. The one in the %TNS_ADMIN% location if defined, will be used by eiether driver. On my PC the 32 and 64 bit tnsnames.ora file locations are :

C:\Oracle\product\11203_32bit\CLIENT_1\NETWORK\ADMIN
C:\Oracle\product\11203_64bit\CLIENT_1\NETWORK\ADMIN

我想做的是使用VBS来识别这些位置,以读取注册表,然后使用上一篇文章的代码作为起点,在每个文件上使用TYPE命令将内容回显到控制台.

What I would like to do is to identify these locations using VBS to read the registry and then use the TYPE command on each file to echo the contents to the console using the code of my previous post as a starting point.

我一直在注册表中闲逛,试图找到相关的键或键链来获取此信息.有了这些信息,我也许就能弄清楚代码了.谢谢.

I've been poking around in the registry trying to find the relevant keys or chain of keys to get this info. With that info, I may be able to figure out the code.. Thanks.

REM Run this file with the following command:
REM cscript  ListDriversV2.vbs | clip

WScript.Echo "------------------------------------------------"

'Get Server Name
Set wshNetwork = WScript.CreateObject( "WScript.Network" )
strComputerName = wshNetwork.ComputerName
WScript.Echo "Computer Name: " & strComputerName

'List 64 bit drivers
WScript.Echo "------------------------------------------------"
WScript.Echo "List 64 bit drivers:"
WScript.Echo 

Const HKEY_LOCAL_MACHINE = &H80000002

strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers"
objRegistry.EnumValues HKEY_LOCAL_MACHINE, strKeyPath, arrValueNames, arrValueTypes

For i = 0 to UBound(arrValueNames)
    strValueName = arrValueNames(i)
    objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue    
    Wscript.Echo arrValueNames(i) & " -- 64 Bit " & strValue
Next

'List 32 bit drivers
WScript.Echo "------------------------------------------------"
WScript.Echo "List 32 bit drivers:"
WScript.Echo 

strKeyPath = "SOFTWARE\Wow6432Node\ODBC\ODBCINST.INI\ODBC Drivers"
objRegistry.EnumValues HKEY_LOCAL_MACHINE, strKeyPath, arrValueNames, arrValueTypes

For i = 0 to UBound(arrValueNames)
    strValueName = arrValueNames(i)
    objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue    
    Wscript.Echo arrValueNames(i) & " -- 32 Bit " & strValue
Next


'List Oracle Environment variables

WScript.Echo "------------------------------------------------"
WScript.Echo "List Oracle Environment variables:"
WScript.Echo 

Set objShell = WScript.CreateObject("WScript.Shell")
strTnsAdmin = objShell.Environment("SYSTEM").Item("TNS_ADMIN")
WScript.Echo "TNS_ADMIN=" & strTnsAdmin

strOracleHome = objShell.Environment("SYSTEM").Item("ORACLE_HOME")
WScript.Echo "ORACLE_HOME=" & stroracleHome


WScript.Echo "------------------------------------------------"
WScript.Echo "List All System Environment variables:"
WScript.Echo 

Set objEnv = objShell.Environment("SYSTEM")

For Each strVar in objEnv
  WScript.Echo strVar
Next

WScript.Echo "------------------------------------------------"
WScript.Echo "List All User Environment variables:"
WScript.Echo 

Set objEnv = objShell.Environment("User")

For Each strVar in objEnv
  WScript.Echo strVar
Next


WScript.Echo "------------------------------------------------"
'How do I dynamically determine this Oracle 64 bit tnsnames location location from the registry?
strPath = "E:\oracle_12101_64bit\product\12101_64bit\CLIENT_1\NETWORK\ADMIN\"

WScript.Echo "Dump " & strPath
WScript.Echo 

strFileContent = LoadStringFromFile(strPath)

WScript.Echo LoadStringFromFile(strPath)
WScript.Echo strPath

WScript.Echo "------------------------------------------------"
'How do I dynamically determine this Oracle 32 bit tnsnames location location from the registry?
strPath = "E:\Oracle\product\11.2.0\client_32Bit\NETWORK\ADMIN\TNSNAMES.ORA"
WScript.Echo "Dump " & strPath
WScript.Echo 

strFileContent = LoadStringFromFile(strPath)

WScript.Echo LoadStringFromFile(strPath)
WScript.Echo strPath

WScript.Echo "------------------------------------------------"
strPath = strTnsAdmin & "\tnsnames.ora"
WScript.Echo "Dump " & strPath
WScript.Echo 

strFileContent = LoadStringFromFile(strPath)

WScript.Echo LoadStringFromFile(strPath)
WScript.Echo strPath

Function LoadStringFromFile(filename)
    Const fsoForReading = 1
    Const fsoForWriting = 2
    Dim fso, f
    Set fso = CreateObject("Scripting.FileSystemObject")

    If fso.FileExists(filename) Then
        Set f = fso.OpenTextFile(filename, fsoForReading)
        LoadStringFromFile = f.ReadAll
        f.Close
    Else
        LoadStringFromFile = ""
    End if

End Function

推荐答案

根据Oracle在这些位置中搜索tnsnames.ora,分别. sqlnet.ora:

According Oracle these locations are searched for tnsnames.ora, resp. sqlnet.ora:

  1. 当前路径(与正在运行的客户端应用程序关联)
  2. 为会话定义的环境变量TNS_ADMIN
  3. 为系统定义的环境变量TNS_ADMIN
  4. Windows注册表项HKLM\SOFTWARE\ORACLE\KEY_{ORACLE_HOME_NAME}\TNS_ADMIN(用于64位)或HKLM\SOFTWARE\Wow6432Node\ORACLE\KEY_{ORACLE_HOME_NAME}\TNS_ADMIN(用于32位)
  5. %ORACLE_HOME%\network\admin
  1. current path (associated with the running client application)
  2. Environment variable TNS_ADMIN defined for the session
  3. Environment variable TNS_ADMIN defined for the system
  4. Windows Registry Key HKLM\SOFTWARE\ORACLE\KEY_{ORACLE_HOME_NAME}\TNS_ADMIN (for 64 bit) or HKLM\SOFTWARE\Wow6432Node\ORACLE\KEY_{ORACLE_HOME_NAME}\TNS_ADMIN (for 32 bit)
  5. %ORACLE_HOME%\network\admin

但是,我不确定每个应用程序/驱动程序/版本是否都遵循此列表.该列表由Oracle提供,与版本9i有关. 我想您可以通过VBScript来查询这些文件夹.

However, I am not sure whether each application/driver/version follows this list. This list was provided by Oracle related to version 9i. I think you will manage it to query these folders by VBScript.

如果环境变量未设置ORACLE_HOME,则必须查询注册表HKLM\SOFTWARE\ORACLE\KEY_{ORACLE_HOME_NAME}\ORACLE_HOME(对于64位)或HKLM\SOFTWARE\Wow6432Node\ORACLE\KEY_{ORACLE_HOME_NAME}\ORACLE_HOME(对于32位)

If ORACLE_HOME is not set by Environment variable, you have to query the Registry HKLM\SOFTWARE\ORACLE\KEY_{ORACLE_HOME_NAME}\ORACLE_HOME (for 64 bit) or HKLM\SOFTWARE\Wow6432Node\ORACLE\KEY_{ORACLE_HOME_NAME}\ORACLE_HOME (for 32 bit)

对于ORACLE_HOME_NAME,您必须导航到Oracle bin文件夹(可通过%PATH%环境变量找到)并打开文件oracle.key.这是一个仅包含ORACLE_HOME_NAME值的简单文本文件,例如 OraClient11g_home1 .

For ORACLE_HOME_NAME you have to navigate to your Oracle bin folder (to be found via %PATH% Environment variable) and open file oracle.key. This is a simple text file containing only the ORACLE_HOME_NAME value, e.g. OraClient11g_home1.

但是,通常在HKLM\SOFTWARE\ORACLE下面只有一个Oracle Home,因此查找和读取文件oracle.key可能是一个过大的杀伤力.

However, typically there is only one Oracle Home underneath HKLM\SOFTWARE\ORACLE, so seeking and reading file oracle.key might be an overkill.

更新

当我在使用Oracle Client 11.2的计算机上运行测试时,得到以下命令:

When I run a test on my machine (with Oracle Client 11.2) I get following order:

  1. 环境变量TNS_ADMIN
  2. HKLM\SOFTWARE\ORACLE\KEY_{Oracle_Home_Name}\TNS_ADMIN,分别. HKLM\SOFTWARE\Wow6432Node\ORACLE\KEY_{Oracle_Home_Name}\TNS_ADMIN-> 仅当未设置TNS_ADMIN环境变量时.
  3. %ORACLE_HOME%\network\admin
  4. 当前目录(可以与您的应用程序所在的目录不同)
  5. 应用程序所在的文件夹
  1. Environment variable TNS_ADMIN
  2. HKLM\SOFTWARE\ORACLE\KEY_{Oracle_Home_Name}\TNS_ADMIN, resp. HKLM\SOFTWARE\Wow6432Node\ORACLE\KEY_{Oracle_Home_Name}\TNS_ADMIN -> Only if TNS_ADMIN Environment variable is not set.
  3. %ORACLE_HOME%\network\admin
  4. Current directory (which can be different to directory where your application is located)
  5. Folder where your application is located

要进行深入分析,您必须搜索tnsnames.orasqlnet.oraldap.ora.可以通过它们中的每一个来解析Oracle数据库名称,即,即使tnsnames.orasqlnet.ora不存在,也可以建立连接.

For a deep analysis you have to search for tnsnames.ora, sqlnet.ora and ldap.ora. The Oracle database name can be resolved through each of them, i.e. a connection may be established even when tnsnames.ora and sqlnet.ora do not exist.

这篇关于识别相关tnsnames的位置并回显到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆