How to extract port number in listener.ora

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dosjediwannabe
Posts: 6
Joined: 13 Feb 2013 22:05

How to extract port number in listener.ora

#1 Post by dosjediwannabe » 13 Feb 2013 22:22

I have the following in ksh, i.e. bash script it is working fine,

Code: Select all

LISTENER_FILE=`cat $ORACLE_HOME/network/admin/listener.ora | grep "PORT"`

  EXISTING_PORT=${LISTENER_FILE##*=}
  EXISTING_PORT=${EXISTING_PORT%%\)*}

  if [ -z "$LISTENER_PORT" ] && [ -z "$EXISTING_PORT" ]
  then
    echo
    echo "***Enter LISTENER_PORT (only applicable to new listener) : "
  else
    echo
    echo "***Current LISTENER_PORT =$EXISTING_PORT"
    echo "***Press <ENTER> to retain the Current value : "
  f



the content of listener.ora is as follow:

Code: Select all

# listener.ora Network Configuration File: C:\app\chleng\product\11.2.0\dbhome_1\NETWORK\ADMIN\listener.ora
# Generated by Oracle configuration tools.

#Default LISTENER.ORA file

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = TIMHALL)
      (SID_NAME = TIMHALL)
    )
    (SID_DESC =
      (GLOBAL_DBNAME = Oracle8)
      (SID_NAME = ORCL)
    )
  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = bn-william)(PORT = 1521))
    )
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
    )
  )

ADR_BASE_LISTENER = C:\app\william




so how do I convert the above code so that I can extract the port number?

thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to extract port number in listener.ora

#2 Post by foxidrive » 13 Feb 2013 22:50

Code: Select all

@echo off
for /f "tokens=7 delims=)= " %%a in ('find "(PORT =" ^<listener.ora') do echo %%a
pause

dosjediwannabe
Posts: 6
Joined: 13 Feb 2013 22:05

Re: How to extract port number in listener.ora

#3 Post by dosjediwannabe » 14 Feb 2013 01:22

Code: Select all

@echo off

set LISTENER_FILE=%ORACLE_HOME%\network\admin\listener.ora

rem for /f "tokens=7 delims=)= " %%a in ('find "(PORT =" ^<listener.ora') do echo %%a

for /f "tokens=7 delims=)= " %%a in ('find "(PORT =" ^<%LISTENER_FILE%') do (
echo %%a
set EXISTING_PORT=a
echo %EXISTING_PORT%
)


pause



here' the result

Code: Select all


D:\william\bugfixes\29340>extract_listener_port.bat
1521
a
Press any key to continue . . .


My purpose is to store the port number in this case 1521 in a variable called existing_port so that I can do further processing. thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to extract port number in listener.ora

#4 Post by foxidrive » 14 Feb 2013 01:39

Try that:

Code: Select all

@echo off
set LISTENER_FILE="%ORACLE_HOME%\network\admin\listener.ora"

for /f "tokens=7 delims=)= " %%a in ('find "(PORT =" ^<%LISTENER_FILE%') do (
echo %%a
set EXISTING_PORT=%%a
)
echo %EXISTING_PORT%
pause

dosjediwannabe
Posts: 6
Joined: 13 Feb 2013 22:05

Re: How to extract port number in listener.ora

#5 Post by dosjediwannabe » 14 Feb 2013 02:16

hi firstly thanks for the suggestion, I managed to get that portion working.

Code: Select all


@echo off

set LISTENER_FILE=%ORACLE_HOME%\network\admin\listener.ora

rem for /f "tokens=7 delims=)= " %%a in ('find "(PORT =" ^<listener.ora') do echo %%a

for /f "tokens=7 delims=)= " %%a in ('find "(PORT =" ^<%LISTENER_FILE%') do (
      rem echo %%a
      set EXISTING_PORT=%%a
)

rem echo %EXISTING_PORT%

if (%LISTENER_PORT%)==() if (%EXISTING_PORT%)==() (
   echo "***Enter LISTENER_PORT (only applicable to new listener) : ") else (
         echo "***Current LISTENER_PORT =%EXISTING_PORT%"
       echo "***Press <ENTER> to retain the Current value : "
)

rem found in xls_db.bat start


if not exist %EXISTING_PORT% (
     set ACTUAL_PORT=%LISTENER_PORT%
     echo "456"
  ) else (
     echo "789"
     set ACTUAL_PORT=%EXISTING_PORT%     
  )

echo  %ACTUAL_PORT%

rem found in xls_db.bat end 
pause


my output for the above code is:

Code: Select all


D:\william_backup\bugfixes\29340>extract_listener_port.bat
"***Current LISTENER_PORT =1521"
"***Press <ENTER> to retain the Current value : "
"456"
ECHO is off.
Press any key to continue . . .


but problem is above output is wrong because since %EXISTING_PORT% is set it should logically go to the else statement and not the if portion, could you tell me what's wrong with my code?

thanks a lot!

dosjediwannabe
Posts: 6
Joined: 13 Feb 2013 22:05

Re: How to extract port number in listener.ora

#6 Post by dosjediwannabe » 14 Feb 2013 03:36

Code: Select all

@echo off

set LISTENER_FILE=%ORACLE_HOME%\network\admin\listener.ora

rem for /f "tokens=7 delims=)= " %%a in ('find "(PORT =" ^<listener.ora') do echo %%a

for /f "tokens=7 delims=)= " %%a in ('find "(PORT =" ^<%LISTENER_FILE%') do (
      rem echo %%a
      set EXISTING_PORT=%%a
)

rem echo %EXISTING_PORT%


 if (%LISTENER_PORT%)==() if (%EXISTING_PORT%)==() (
      echo "***Enter LISTENER_PORT (only applicable to new listener) : ") else (
            echo "***Current LISTENER_PORT =%EXISTING_PORT%"
          echo "***Press <ENTER> to retain the Current value : "
    )

rem found in xls_db.bat start


if (%EXISTING_PORT%)==()    (
     set ACTUAL_PORT=%LISTENER_PORT%     
  ) else (        
        set ACTUAL_PORT=%EXISTING_PORT%
  )

echo  "ACTUAL_PORT %ACTUAL_PORT%"

rem found in xls_db.bat end 
pause



when I run the above code, the output is as follow:

Code: Select all


D:\william\bugfixes\29340>extract_listener_port.bat
"***Current LISTENER_PORT =1521"
"***Press <ENTER> to retain the Current value : "
 "ACTUAL_PORT 1521"
Press any key to continue . . .


i wasn't able to press enter to retain the current value, what should I add in order for me to enter to retain the current value?

thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to extract port number in listener.ora

#7 Post by foxidrive » 14 Feb 2013 04:12

dosjediwannabe wrote:

Code: Select all

 if (%LISTENER_PORT%)==() if (%EXISTING_PORT%)==() (
      echo "***Enter LISTENER_PORT (only applicable to new listener) : ") else (
            echo "***Current LISTENER_PORT =%EXISTING_PORT%"
          echo "***Press <ENTER> to retain the Current value : "
    )



Firstly, a tip: Avoid using () in comparisons - use "" such as this:

Code: Select all

 if "%LISTENER_PORT%"=="" if "%EXISTING_PORT%"=="" (


One reason is that parentheses will break code, and the other reason is the double quotes will protect the statements when long filename elements and poison characters are in the string.

In this line the closing parenthesis breaks your code. If you have to echo special characters then you need to escape them.
echo "***Enter LISTENER_PORT ^(only applicable to new listener^)


I can't see anywhere that you are getting input to use Enter.

dosjediwannabe
Posts: 6
Joined: 13 Feb 2013 22:05

Re: How to extract port number in listener.ora

#8 Post by dosjediwannabe » 14 Feb 2013 22:30

if my configuration is as follow

Code: Select all

LISTENER =
  (DESCRIPTION_LIST =(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=sg-chlengnb1)(PORT=1521)))
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
    )
  )



what should be the correct way to extract the port number? thanks

existing code

Code: Select all


@echo off

set LISTENER_FILE=%ORACLE_HOME%\network\admin\listener.ora

echo "LISTENER_FILE %LISTENER_FILE%"
rem for /f "tokens=7 delims=)= " %%a in ('find "(PORT =" ^<listener.ora') do echo %%a

for /f "tokens=7 delims=)= " %%a in ('find "(PORT =" ^<%LISTENER_FILE%') do (
      echo %%a
      set EXISTING_PORT=%%a
)

echo "EXISTING_PORT %EXISTING_PORT%"


 if "%LISTENER_PORT%"=="" if "%EXISTING_PORT%"=="" (
      echo "***Enter LISTENER_PORT (only applicable to new listener) : ") else (
            echo "***Current LISTENER_PORT =%EXISTING_PORT%"
          echo "***Press <ENTER> to retain the Current value : "
    )

rem found in xls_db.bat start


if (%EXISTING_PORT%)==()    (
     set ACTUAL_PORT=%LISTENER_PORT%     
  ) else (        
        set ACTUAL_PORT=%EXISTING_PORT%
  )

echo  "ACTUAL_PORT %ACTUAL_PORT%"


rem found in xls_db.bat end 
rem pause




output for above code

Code: Select all


D:\william_backup\bugfixes\29340>extract_listener_port.bat
"LISTENER_FILE C:\app\chleng\product\11.2.0\dbhome_1\network\admin\listener.ora"

"EXISTING_PORT 1521"
"***Current LISTENER_PORT =1521"
"***Press <ENTER> to retain the Current value : "
 "ACTUAL_PORT 1521"

D:\william_backup\bugfixes\29340>



notice echo %%a is not executed.

thanks a lot!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to extract port number in listener.ora

#9 Post by foxidrive » 14 Feb 2013 22:56

It seems to be working as your code makes it work.
What do you want to do with the port?


Note that this is an AND and both conditions need to be equal to "" for it to do anything.

if "%LISTENER_PORT%"=="" if "%EXISTING_PORT%"=="" (

dosjediwannabe
Posts: 6
Joined: 13 Feb 2013 22:05

Re: How to extract port number in listener.ora

#10 Post by dosjediwannabe » 15 Feb 2013 08:42

actually what is unresolved is that the previous code is not able to resolve PORT number if

the following happens

Code: Select all

LISTENER =
  (DESCRIPTION_LIST =(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=bn-william)()(PORT=1521)))
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
    )
  )






it works if the following happens

Code: Select all

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = bn-william)(PORT = 1521))
    )
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
    )
  )



Notice the code below is able to extract PORT number if it is PORT = 1521 but not when PORT=1521, i.e. %EXISTING_PORT% = 1521 if PORT = 1521, but %EXISTING_PORT% is empty if PORT=1521

Code: Select all

@echo off

set LISTENER_FILE=%ORACLE_HOME%\network\admin\listener.ora

rem for /f "tokens=7 delims=)= " %%a in ('find "(PORT =" ^<listener.ora') do echo %%a

for /f "tokens=7 delims=)= " %%a in ('find "(PORT =" ^<%LISTENER_FILE%') do (
echo %%a
set EXISTING_PORT=a
echo %EXISTING_PORT%
)


What I want to do with the port:

To extract the port number, if there's already an existing port number, system will not ask the user to enter port number. If there's no existing port, then user is able to enter port number.

thanks a lot for your assistance

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to extract port number in listener.ora

#11 Post by foxidrive » 15 Feb 2013 22:12

This is more robust - as long as PORT doesn't appear elsewhere in the listener.ora file.

It will ask for a port number if it doesn't find one.

Code: Select all

@echo off
set LISTENER_FILE=%ORACLE_HOME%\network\admin\listener.ora
set "port="
for /f "delims=" %%a in ('find "(PORT" ^<"%LISTENER_FILE%"') do set "port=%%a"

if not defined port goto :enter_port

set "port=%port:*PORT=%"
for /f "delims==() " %%a in ("%port%") do set port=%%a
echo The port was found on %port%
goto :End

:enter_port
set /p "port=The port wasn't detected, please enter a port #: "

echo port is %port%

:end
pause

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to extract port number in listener.ora

#12 Post by foxidrive » 19 Feb 2013 05:45

Was this useful?

Post Reply