Help Generate new port base on the last port but not in used by other application


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help Generate new port base on the last port but not in used by other application
# 1  
Old 10-01-2017
Help Generate new port base on the last port but not in used by other application

Hi Expert,

Anybody can figure it out on how to generate new port base on my last port let say my last port var1=124 and increment for new port 125,126 but this new two ports need to look at first if this port is not in used by any service, if the port is in used add 1 to new port and if in used again add 1 and soon. let say if the 126 port is in used, the generated output would be 125,128
or
125
128

Here my test simulation script:

Code:
cat /opt/lastports
124

Code:
cat /opt/usedports  #simulation I will used netstat -lntu to look used port
21
22
23
110
111
120
126
127

Code:
#!/bin/sh

clear
echo ""
LASTPORT=$(cat /opt/port/lastports)  #Get the last port
NEWTMPPORT=$(( $LASTPORT + 1 ))  #Get the last port add 1 and sum

#PORTCHECKER=$(netstat -lntu |tail -n +3 |awk '{print $4}' |rev |cut -f1 -d: |rev |sort -u |grep $NEWTMPPORT) # look new temporary port to the list
PORTCHECKER=$(cat /opt/port/usedports | grep $NEWTMPPORT)

if [ "$PORTCHECKER" == "$NEWTMPPORT" ]; then  #if new port is equal to port checker then assign other port
NEWPORT=$(( $NEWTMPPORT + 1 ))

echo "LASTPORT    is $LASTPORT"          #this not importat for checking
echo "NEWTMPPORT  is $NEWTMPPORT"          #this not importat
echo "PORTCHECKER is $PORTCHECKER"          #this not importat
echo "NEWPORT     is $NEWPORT"          #this not importat

else
echo "$NEWPORT Port is available!"
fi

for i in `eval echo {$NEWTMPPORT..$NEWPORT}`
do
echo $i
done


Last edited by lxdorney; 10-01-2017 at 12:19 PM..
# 2  
Old 10-01-2017
These are the well known ports from IANA. Basically it means if you use one of these
adhoc you run the risk of breaking something - now or next week. Just because a port is unused right now does not mean it won't get used again soon.

Is there a reason you have to use low number ports?

Try something like this:
Code:
LASTPORT=32767
ss | awk ' /\*/ {print $6 } > portlist.txt

getport {
   result="FAIL"
   for (( i=8193; i<$LASTPORT; i++))
   do
         grep -q "$i" portlist.txt && continue
         result="$i"
         break
   done
   echo "$result"
}
# usage
port=$( getport )
if [ "$port" = "FAIL" ] ; then
   echo "no ports available"
   exit
fi
# use the port number here

You could also create an array (port based) on the awk output and search through the array rather than using grep. Use the mapfile command, which creates an array named MAPFILE with every port number in it. You can also use associative arrays to speed up your search. None of this is really useful except when you are creating lots of ports.
# 3  
Old 10-01-2017
Quote:
Originally Posted by jim mcnamara
These are the well known ports from IANA. Basically it means if you use one of these
adhoc you run the risk of breaking something - now or next week. Just because a port is unused right now does not mean it won't get used again soon.

Is there a reason you have to use low number ports?

Try something like this:
Code:
LASTPORT=32767
ss | awk ' /\*/ {print $6 } > portlist.txt

getport {
   result="FAIL"
   for (( i=8193; i<$LASTPORT; i++))
   do
         grep -q "$i" portlist.txt && continue
         result="$i"
         break
   done
   echo "$result"
}
# usage
port=$( getport )
if [ "$port" = "FAIL" ] ; then
   echo "no ports available"
   exit
fi
# use the port number here

You could also create an array (port based) on the awk output and search through the array rather than using grep. Use the mapfile command, which creates an array named MAPFILE with every port number in it. You can also use associative arrays to speed up your search. None of this is really useful except when you are creating lots of ports.
No is just a sample port Sir
# 4  
Old 10-01-2017
Oops - IANA ports link

http://www.mosinu.com/docs/port_numbers

Duh.
# 5  
Old 10-01-2017
I got error message to the scripts. and what
Code:
i=8193

stand for?

Code:
 sh test1.sh
test1.sh: line 2: unexpected EOF while looking for matching `''
test1.sh: line 21: syntax error: unexpected end of file

Thanks
# 6  
Old 10-02-2017
Quote:
Originally Posted by lxdorney
Hi Expert,
... ... ...
Here my test simulation script:

Code:
cat /opt/lastports
124

Code:
cat /opt/usedports  #simulation I will used netstat -lntu to look used port
21
22
23
110
111
120
126
127

Code:
#!/bin/sh
 ... ... ...
LASTPORT=$(cat /opt/port/lastports)  #Get the last port
NEWTMPPORT=$(( $LASTPORT + 1 ))  #Get the last port add 1 and sum

#PORTCHECKER=$(netstat -lntu |tail -n +3 |awk '{print $4}' |rev |cut -f1 -d: |rev |sort -u |grep $NEWTMPPORT) # look new temporary port to the list
PORTCHECKER=$(cat /opt/port/usedports | grep $NEWTMPPORT)
 ... ... ...

You show us that data exists in the files /opt/usedports and /opt/lastports and then you show us a script that reads from /opt/ports/usedports and /opt/ports/lastports. Shouldn't your script be reading from files that exist and have data in them?

Quote:
Originally Posted by lxdorney
I got error message to the scripts. and what
Code:
i=8193

stand for?

Code:
 sh test1.sh
test1.sh: line 2: unexpected EOF while looking for matching `''
test1.sh: line 21: syntax error: unexpected end of file

Thanks
Despite what you said, ports below 1024 are called privileged ports and should not be used for "general purpose" transactions. If you are looking for a temporary (or ephemeral) port, you should use higher numbered ports. Jim's code doesn't attempt to use ports outside of the range 8193 through 32766.

Try changing the 2nd line in Jim's suggested script from:
Code:
ss | awk ' /\*/ {print $6 } > portlist.txt

to:
Code:
ss | awk ' /\*/ {print $6 }' > portlist.txt

Jim's code assumes that /bin/sh on your system is a shell that understands the syntax:
Code:
for (( var=initial_val; var < test_value; var++ ))

which might or might not be true. As you have been told before, you are likely to get suggestions that won't work on your system if you don't tell us that operating system and shell you're using!
# 7  
Old 10-02-2017
Thanks to all, heres my logic
Code:
#!/bin/sh
CHECKTHIS=$(cat /opt/curport)
CHECKTHISPORT=$CHECKTHIS
func_checker () {
USEDPORTS=$(netstat -ntpl |awk '{print $4}' |rev |cut -f1 -d: |rev |sort -u |head -n -2 |grep $CHECKTHISPORT)
if [ "$CHECKTHISPORT" == "$USEDPORTS" ]; then
CHECKTHISPORT=`expr $CHECKTHISPORT + 1`
func_checker
else
   echo "$CHECKTHISPORT is available!"
fi
}
func_checker

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

How to find port number wwn of particular port on dual port HBA,?

please find the below o/p for your reference bash-3.00# fcinfo hba-port HBA Port WWN: 21000024ff295a34 OS Device Name: /dev/cfg/c2 Manufacturer: QLogic Corp. Model: 375-3356-02 Firmware Version: 05.03.02 FCode/BIOS Version: BIOS: 2.02; fcode: 2.01;... (3 Replies)
Discussion started by: sb200
3 Replies

2. Solaris

Cabling and adapters to communicate to service processor serial port from Windows PC with USB port.

Hello, I have an unloaded T5140 machine and want to access the ILOM for the first time and subsequently the network port after that., and then load Solaris 10 the final January 2011 build. The first part is what confuses me -the cabling. I am coming from a Windows machine (w/appropriate... (5 Replies)
Discussion started by: joboy
5 Replies

3. Solaris

How to enable Serial port on ILOM, when Network Port is enabled in parallel

Hi Everyone, In my environment, I have few T5220. On the iLOM Management Card, I have both Network and Serial port are cabled, I don't have any issues while I try to connect using Network Management port, but when I try to connect the serial port for the same server which is actually connected... (3 Replies)
Discussion started by: bobby320
3 Replies

4. Shell Programming and Scripting

command to know the application running of the port

Hi, is there any command to findout that which application is using the particular port. or whether any port is occupied with the specfic process id ? (4 Replies)
Discussion started by: mail2sant
4 Replies

5. Linux

MQ application port 1414

Hi All, I have two MQ (port) 1414 established (going out) $ netstat -an | grep 1414 tcp 0 0 0.0.0.0:1414 0.0.0.0:* LISTEN tcp 0 0 20.76.1.1:32855 142.8.1.3:1414 ESTABLISHED tcp 0 0... (2 Replies)
Discussion started by: itik
2 Replies

6. Programming

how to write application for 32 com port

Dear Sir, i m going to use NP5610-16 moxa device for multiport serial communication. i m using fedora-core 6 o.s. after installation it will detect serial ports as /dev/ttyr0,/dev/ttyr1...ttyr32. there are total 32 com ports. now i want to write application which monitor all serial ports and... (6 Replies)
Discussion started by: amitpansuria
6 Replies

7. UNIX for Dummies Questions & Answers

What application is using the port

Hello im using sunos im trying to lock down application that taking my port when Im doing "netstat -in | grep 8080" Is gives me the indication that the port is taken but no indication who is taken the port. How can I find out who is taking my port? Thanks allot (7 Replies)
Discussion started by: umen
7 Replies

8. IP Networking

How can I check what port addresses used the application

Hello Mentors! I am a new here in the furom, i hope somebody can understand my problem. Basically we have an application here called unigraphics and being installed per station and the setup is look like this. 1. installed unigraphics UGNX3 version on every station 2. some are installed in... (1 Reply)
Discussion started by: eykyn17
1 Replies

9. UNIX for Advanced & Expert Users

which port to write my server application?

I want to write a server application that would accept HTTP requests from client. The server would be on a machine that has no connection to the INTERNET. The clients that would be posting their HTTP requests would be doing so through webbrowser .Thus it would be sort of intranet application.... (0 Replies)
Discussion started by: rraajjiibb
0 Replies
Login or Register to Ask a Question