Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Check unused ports in a given range and assign an open one

Shell Programming and Scripting


Tags
korn shell script, linux 6.0, netstat, port number.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-07-2017
general_franco general_franco is offline
Registered User
 
Join Date: Nov 2016
Last Activity: 26 March 2017, 7:45 PM EDT
Posts: 34
Thanks: 15
Thanked 0 Times in 0 Posts
Check unused ports in a given range and assign an open one

Hi. I need to add code to my KSH script to automatically assign an open port number from a pre-defined range to an Oracle listener.

Should I use:

Code:
lsof -i

or

Code:
netstat -vatn

or something else?

Thanks.
Sponsored Links
    #2  
Old Unix and Linux 01-08-2017
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 27 April 2017, 9:04 PM EDT
Location: NM
Posts: 11,027
Thanks: 515
Thanked 1,047 Times in 970 Posts
Managing Oracle Database Port Numbers

Indicates that oracle does this already. So, if you need to use a range of ports that is not a default, you would have to reconfigure oracle port management. First. Then what you describe will occur automatically without your intervention.
Sponsored Links
    #3  
Old Unix and Linux 01-08-2017
general_franco general_franco is offline
Registered User
 
Join Date: Nov 2016
Last Activity: 26 March 2017, 7:45 PM EDT
Posts: 34
Thanks: 15
Thanked 0 Times in 0 Posts
Hi Jim. I'd better give a bit more info. I'm generating a new listener file for a gateway from a script so I have to assign a port. There will be multiple listeners running on different ports on that same host. If I choose one that's already in use the new listener obviously won't start. The range I'll choose this from is not the important part, I'll simply stick to the default range defined for Oracle. I just need to know the best way to determine if the port I choose is in use already. Thanks.

---------- Post updated at 11:53 AM ---------- Previous update was at 10:42 AM ----------

So this should work, right? Or am I missing something?


Code:
PORT=$(($RANDOM%50+1521))
RC=$(netstat -vatn | grep $PORT)
[[ -z $RC  ] ] && print "Port is free"

    #4  
Old Unix and Linux 01-08-2017
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 27 April 2017, 9:04 PM EDT
Location: NM
Posts: 11,027
Thanks: 515
Thanked 1,047 Times in 970 Posts
You need a few changes in your script:

Code:
PORT=$(($RANDOM%50+1521))
RC=$(netstat -vatn | grep $PORT)
[[ -z  "$RC" ]] && print "Port is free"

or

Code:
PORT=$(($RANDOM%50+1521))
netstat -vatn | grep $PORT &&  echo "Port is free"

I just do not see why you are doing this manually, but whatever. If you have issues, consider this port selection something to check first.
The Following User Says Thank You to jim mcnamara For This Useful Post:
general_franco (01-10-2017)
Sponsored Links
    #5  
Old Unix and Linux 01-09-2017
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 27 April 2017, 11:56 AM EDT
Location: Lancashire, UK
Posts: 3,080
Thanks: 1,287
Thanked 590 Times in 534 Posts
Probably a daft question, but if you are starting a process to listen on a port, how will the client know which port to connect to?

Apologies for being daft ..... Linux




Robin
Sponsored Links
    #6  
Old Unix and Linux 01-09-2017
general_franco general_franco is offline
Registered User
 
Join Date: Nov 2016
Last Activity: 26 March 2017, 7:45 PM EDT
Posts: 34
Thanks: 15
Thanked 0 Times in 0 Posts
Because I'm creating the related tnsnames entry on the DB host at the same time. It's all automated through a GUI with the push of a button. Everything has to be done for the end user who knows nothing about Oracle.

@Jim, that's also why it's not done manually. My script has to choose a free port on a specified host in order to create a new listener file from scratch and then start it. There will be multiple listeners running simultaneously and each has to have it's own unique port. Hope that clarifies it.
Sponsored Links
    #7  
Old Unix and Linux 01-10-2017
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 27 April 2017, 11:56 AM EDT
Location: Lancashire, UK
Posts: 3,080
Thanks: 1,287
Thanked 590 Times in 534 Posts
Can I assume that you are publishing tnsnames.ora so that anyone can read it? If so, why are you wanting to set up a listener on a new port?

The Oracle listener can support connections to multiple databases on the same host, all using the default port 1521. The database is coded in the connection request from the client whether you use the default port or any other port, unless your listener has a 'default if not specified' type definition. It would seem far better to use the listener and add connection details for each database. That way, the client never needs to worry about finding out which port to connect on, it just specifies the database name.

What am I missing here? If you are after hardening your server so that an intruder doesn't know which port to attack, then you have the same problem for your clients in that they will have to have a way to find out.

Can you explain the reasons why you want multiple listening processes? After the connection is established, the listener keeps out the way of the communication, so it's not a bottleneck to performance.



Thanks, in advance,
Robin
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
based on range assign a value Diya123 Shell Programming and Scripting 3 08-05-2011 03:38 PM
locking down unused or unwanted ports in AIX admaix AIX 1 07-27-2010 09:38 AM
Sheel Scripting to lock 2 TCP unused ports in solaris and linux sreeramr30 Shell Programming and Scripting 2 11-01-2009 11:48 PM
filtering a range of ports out of a netstat output marcpascual Shell Programming and Scripting 1 09-30-2008 09:36 PM
Check open ports every ... MorchiuS Shell Programming and Scripting 2 08-03-2005 04:17 PM



All times are GMT -4. The time now is 06:21 AM.