troubleshooting Question

get oracle database port

Avatar of bibi92
bibi92Flag for France asked on
Oracle DatabaseShell Scripting
10 Comments1 Solution65 ViewsLast Modified:
Hello,

I try to get oracle database port, the below script works if there is only one database by server.
How can I modify it for several databases :
#!/bin/sh

export ORAENV_ASK=NO
for BASE in $(ps -ef | grep pmon | grep -v "grep" | grep -v "awk" | grep -v "ASM" | awk -F "pmon_"  '{print$2} ')
do
export ORACLE_SID=$BASE
. oraenv $BASE


 ps -eaf | grep -v -E "grep|ps" | grep -i "tnslsnr" >/dev/null 2>&1
        if [ 0 -eq $? ]
        then
                TNSLSNRPID=$(ps -eaf | grep -v -E "grep|ps" | grep -i "tnslsnr" |\
                awk '{
                        print $2;
                }')
                export PORT_NAME=$(lsof -p $TNSLSNRPID 2>&1 |\
                awk '{
                        if ( "(LISTEN)" == $NF )
                        {
                                printf( "%s\n", substr( $(NF-1), 3) );
                        }
                }')
                port=$(awk '{
                        if ( index( $0, ENVIRON["PORT_NAME"]) )
                        {
                                if ( index( $0, "/tcp") )
                                {
                                        printf( "%s\n", substr( $2, 0, index( $2, "/")-1) );
                                }
                        }
                }' /etc/services)
              
        else
                ps -eaf | grep -v -E "grep|ps" | grep -i "tnslsnr"
fi				
${ORACLE_HOME}/bin/sqlplus -s / as sysdba << !
set pagesize 0
set echo off
set feed off
set LINES 2000
DECLARE
DELIMITER   VARCHAR2(1);
V_ORACLE_SID VARCHAR2 (256);
V_ORACLE_HOME VARCHAR2 (2000);
V_SPFILE VARCHAR2 (1000);
V_IP VARCHAR2 (30);
V_HOST VARCHAR2 (230);
BEGIN
DELIMITER := '/';
DBMS_SYSTEM.GET_ENV ('ORACLE_SID', V_ORACLE_SID );
DBMS_OUTPUT.PUT_LINE (V_ORACLE_SID||';'||V_IP||';'||'${port}');
END;
/
!

done
	

Thank you

Best regards
ASKER CERTIFIED SOLUTION
johnsone
Senior Oracle DBA

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros