有没有简单方法通过命令行列出系统中所有可用的tnsnames?

每当需要查找tnsname时,我只需使用 tnsping 命令搜索 tnsnames.ora 文件,然后在文本编辑器中打开它以进行扫描。然后,随后运行 tnsping [tnsname] 来检查连接运行状况。我在想是否有像 tnslist或tns -l 这样的简单命令列出所有tnsname,但我找不到。

最佳答案

假设您有一个tnsnames.ora文件:

DB01 =
    (DESCRIPTION =
        (FAILOVER=off)
        (LOAD_BALANCE=off)
          (ADDRESS = (PROTOCOL = TCP)(HOST = db01-vip)(PORT = 1521))
         (CONNECT_DATA =
             (SERVER = DEDICATED)
             (SERVICE_NAME = mydb1)
         )
     )

DB02 =
    (DESCRIPTION =
        (FAILOVER=off)
        (LOAD_BALANCE=off)
          (ADDRESS = (PROTOCOL = TCP)(HOST = db02-vip)(PORT = 1531))
         (CONNECT_DATA =
             (SERVER = DEDICATED)
             (SERVICE_NAME = mydb2)
         )
     )

像这样编辑您的.profile或.bash_profile:


ORACLE_HOME=/u01/app/oracle/product/12.1.0.2/dbhome_1; export ORACLE_HOME
...
alias lstns="sed -n '/DESCR/{x;p;d;}; x' $ORACLE_HOME/network/admin/tnsnames.ora | sed "s/=/${s}/""
echo 'lstns : tnsnames.ora listing'


lstns : tnsnames.ora listing


  DB01
  DB02

10-01 17:40