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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check unused ports in a given range and assign an open one
# 1  
Old 01-08-2017
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.
# 2  
Old 01-08-2017
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.
# 3  
Old 01-08-2017
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 01-09-2017
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.
This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 01-09-2017
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 ..... Smilie




Robin
# 6  
Old 01-09-2017
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.
# 7  
Old 01-10-2017
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
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

based on range assign a value

Hello, I have a file with multiple columns of which the first two columns are like a1_144601_144650 ABC_yellow_144608_16785 a1_144651_144700 ABC_yellow_144608_16785 a1_144701_144751 ABC_yellow_144608_16785 So Based on column 1 (red values) I need to check if its falling in... (3 Replies)
Discussion started by: Diya123
3 Replies

2. AIX

locking down unused or unwanted ports in AIX

We have a system and we have modified the /etc/ind.conf and the /etc/services and the /etc/rc.tcpip file to turn off specific applications. I need to know what is the correct procedure for locking down unused ports that still appear to be in a listen mode even... (1 Reply)
Discussion started by: admaix
1 Replies

3. Solaris

Open ports in solaris 10

hi guys, may i know the exact steps to open a port in solaris.i have some rough idea - which is adding the port number in /etc/services. but i am not sure the correct conventions, steps or any other steps. kindly advise.thanks guys ! (1 Reply)
Discussion started by: cromohawk
1 Replies

4. Shell Programming and Scripting

Sheel Scripting to lock 2 TCP unused ports in solaris and linux

My requirement is I need to write a program in shell scripting to check 2 TCP unused unique port numbers in SOLARIS and I have to lock the same ports so that it will not be used in any other new process and the same port numbers should be used and locked in the LINUX machine to communicate... (2 Replies)
Discussion started by: sreeramr30
2 Replies

5. Solaris

open ports solaris 8

Hello, I have a number of Solaris 8 Sun servers that have open ports that I cannot identify. I see some with 1013-1023 (which are reserved ports according to the IANA. Lsof does not identify these. I rebooted the server and they went off, but this morning I saw they were all back on again. Any... (1 Reply)
Discussion started by: csgonan
1 Replies

6. UNIX for Dummies Questions & Answers

open ports solaris 8

Hello, I have a number of Solaris 8 Sun servers that have open ports that I cannot identify. I see some with 1012-1020 (which are reserved ports according to the IANA. Lsof does not identify these. One server has all these on and one server just has 1017. *.1023 ... (3 Replies)
Discussion started by: csross
3 Replies

7. Shell Programming and Scripting

filtering a range of ports out of a netstat output

i'd like to grep a range of ports on a netstat -nt output, localaddress, say :1 to :1023. how do i do it via sed/awk/grep? Thanks, Marc (1 Reply)
Discussion started by: marcpascual
1 Replies

8. Shell Programming and Scripting

Check open ports every ...

Hello, i need a script (bash type maybe?..), which would check open ports on 127.0.0.1 and then compare open ports with "registered/allowed" port list and try to kill the program who uses unregistered ports. It would be great that script would be started lets say every 5 or 10 minutes. You see i... (2 Replies)
Discussion started by: MorchiuS
2 Replies

9. Cybersecurity

closing open ports

/* Linux Slackware */ Nmap shows the following ports open on the gateway. 21/tcp ftp 22/tcp ssh 23/tcp telnet 25/tcp smtp 37/tcp time 80/tcp http 113/tcp auth 515/tcp printer 587/tcp submission 1024/tcp kdm 6000/tcp x11 ------------------------------- i would like to close as... (10 Replies)
Discussion started by: LowOrderBit
10 Replies
Login or Register to Ask a Question