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:

cat /opt/lastports
124
cat /opt/usedports  #simulation I will used netstat -lntu to look used port
21
22
23
110
111
120
126
127
#!/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

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:

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

Oops - IANA ports link

Duh.

I got error message to the scripts. and what

i=8193

stand for?

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

Thanks

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?

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:

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

to:

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

Jim's code assumes that /bin/sh on your system is a shell that understands the syntax:

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!

Thanks to all, heres my logic

#!/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