We help IT Professionals succeed at work.

get oracle database port

bibi92
bibi92 asked
on
53 Views
Last Modified: 2020-04-20
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
	

Open in new window


Thank you

Best regards
Comment
Watch Question

johnsoneSenior Oracle DBA
CERTIFIED EXPERT

Commented:
Typically, all databases use the same port for the listener.  In my experience, it is pretty rare to be using more than one listener.
Alex [***Alex140181***]Software Developer
CERTIFIED EXPERT

Commented:
I totally agree with johnsone! Whereas it is possible to run multiple Listeners....

Author

Commented:
Yes there are several listeners on servers thank you
David FavorFractional CTO
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
This depends on your OS.

Here's a Ubuntu example looking for all IPs + Ports where dnsmasq is listening...

net16 # netstat -pluten | grep dnsmasq
tcp        0      0 10.101.55.1:53          0.0.0.0:*               LISTEN      0          478044577  832971/dnsmasq      
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      0          21431      1192/dnsmasq        
tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN      0          21427      1192/dnsmasq        
tcp6       0      0 fd42:a94f:9bb3:bfd6::53 :::*                    LISTEN      0          478044581  832971/dnsmasq      
tcp6       0      0 fe80::1c:5bff:feeb:c:53 :::*                    LISTEN      0          478044579  832971/dnsmasq      
tcp6       0      0 ::1:53                  :::*                    LISTEN      0          21429      1192/dnsmasq        
udp        0      0 10.101.55.1:53          0.0.0.0:*                           0          478044576  832971/dnsmasq      
udp        0      0 127.0.0.53:53           0.0.0.0:*                           0          21430      1192/dnsmasq        
udp        0      0 127.0.0.1:53            0.0.0.0:*                           0          21426      1192/dnsmasq        
udp        0      0 0.0.0.0:67              0.0.0.0:*                           0          478044571  832971/dnsmasq      
udp6       0      0 fd42:a94f:9bb3:bfd6::53 :::*                                0          478044580  832971/dnsmasq      
udp6       0      0 fe80::1c:5bff:feeb:c:53 :::*                                0          478044578  832971/dnsmasq      
udp6       0      0 ::1:53                  :::*                                0          21428      1192/dnsmasq        
udp6       0      0 :::547                  :::*                                0          478044573  832971/dnsmasq

Open in new window

Author

Commented:
Thank you the goal is to get oracle database port and by listener.

regards
Alex [***Alex140181***]Software Developer
CERTIFIED EXPERT

Commented:
There is no "oracle database port"!

There is one Listener running on the server, which establishes the connections to the database... In rare cases, there may be more than 1 of those running at the same time on the same server....
Senior Oracle DBA
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
yes sure it's translation issue the goal is to get one or several listener by server thank you

Author

Commented:
several listeners and several databases thank you
johnsoneSenior Oracle DBA
CERTIFIED EXPERT

Commented:
Good luck.  Multiple listeners is a maintenance nightmare.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.