Algorithm for default port forwarding calculator

Discussion in 'Services - Questions & Answers (Q&A)' started by f00fup, Jun 13, 2018.

  1. f

    f00fup New Member


    I'm trying to create a bash script on a Linux host that brings up a Perfect-Privacy VPN connection then uses the default port forwarding to run a listening service. Unfortunately, it's not clear to me how the default port forwarding ports are calculated by the port forwarding calculator. Can you share the algorithm and a way to calculate the default ports in a script?

  2. MixMaster

    MixMaster Member

    I use the following bash construct in an OpenVPN up-script. It's certainly not pretty but does the job:

    p1="`echo "$ifconfig_local_ip" |sed -e 's/.*\.\([0-9]*\)\.\([0-9]*\)$/\1/'`"
    p2="`echo "$ifconfig_local_ip" |sed -e 's/.*\.\([0-9]*\)$/\1/'`"
    port="`printf "1%04d" "$(( (($p1 % 0x10) * 256) + $p2 ))"`"
    Last edited: Jun 18, 2018
  3. f

    f00fup New Member

    That worked. Thank you so much for your help.