Call hostname from TNS login

I am working on a script that will have to determine which node db is being used by the local application. I tried to use this as a chance to force myself to learn awk / sed and run a test script to verify the claims. It works with a copy of the tnsnames.ora file, which I moved to the home folder where the script is located. Here is a valid tnsnames.ora line:

(
   DESCRIPTION = (
   ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP) (Host=iplab)(Port=1521))
   ) 
   (CONNECT_DATA=(SID=spurs1)) 
)

After doing some research and getting an awk expression to output the tns entry in $ host, I came up with the script below, but it does not work.

#!/bin/ksh
db=spurs

host=$(awk -v db=$db "/${db}/ {for(i=1; i<=5; i++) {getline; print}}" tnsnames.ora)
echo $host
host= $host | sed 's/Host\s=\s\([a-z]+[0-9]?\)/\1/'
echo $host

When I run this awk expression, I get the following:

(DESCRIPTION = (ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP) (Host=hostname)(Port=1521))) (CONNECT_DATA=(SID=spurs1)) )
./tns.ksh: line 6: (DESCRIPTION: not found
(DESCRIPTION = (ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP) (Host=hostname)(Port=1521))) (CONNECT_DATA=(SID=spurs1)) )

, , , , sed $host , 0 1 Host =. ( $host sed, , ,

+3
6

, , ( HOST ):

echo $TNS_STRING | sed 's/.HOST//g' | sed 's/).//g' | sed 's/=//g' | sed 's/\ s *//g'

0

$host , , , ..

host=$(awk -v db=$db "/${db}/ {for(i=1; i<=5; i++) {getline; print}}" tnsnames.ora)
echo "$host"

( ) $(...)

host=$(echo "$host" | sed 's/Host\s=\s\([a-z]+[0-9]?\)/\1/')
echo "$host"

( ), sed

host=$(echo $host | sed 's/.*Host=//; s/).*$//)
echo "$host"

$host dbl-, , . , , upto host=, ) char.

, tnsnames.ora, , , , .

, .

0

tnsping : tnsping, , :

host=$(
  tnsping $db | while read line; do
    if [[ "$line" == *HOST* ]]; then
      s=${line#*HOST=}; s=${s%%)*}; echo "$s"; break
    fi
  done
)
0

:

db=spurs
host=$(sed '/^(/,/^)/!d;/^(/{h;d};H;/^)/!d;g;/'"$db"'/!d;s/.*Host=\([^)]*\).*/\1/' tnsnames.ora)
0

: tnsping $db | grep HOST | cut -d\ -f 14 | sed 's/).*//g'

0

:

OIFS=$IFS;
IFS="(";
tns=`tnsping TNS_ALIAS`
tns_arr=($tns);
tns_info=(`(for ((i=0; i<${#tns_arr[@]}; ++i)); do  echo "${tns_arr[$i]/)/}"; done)| grep 'HOST\|PORT'|sed 's/)//g'|sed 's/ //g'`)
for ((i=0; i<${#tns_info[@]}; ++i)); do eval "export ${tns_info[$i]}"; done
echo "host:" $HOST
echo "port:" $PORT
IFS=$OIFS;
-2

All Articles