Skip to content

PyMoIP scripts

64rulez at gmail edited this page Sep 7, 2021 · 3 revisions

'PyMoIP script' should be present in 'SCRIPTDIR' folder. It requires no customization but needs a "ServerList" file in the same folder (see bellow).

'Start script', 'Stop script', and 'Restart script' should to be present in your home folder and customized as follow :

SCRIPTDIR="PyMoIP_dir"
# Any folder from (your home folder) where to find "PyMoIP_script.sh" script and "ServerList" file.
# Suggested : /home/pi/PyMoIP_dir

BASEDIR="/home/pi/python/PyMoIP"
# Any path where python3 will find "PyMoIP-Gateway" and "PyMoIP-Server" programs
# Suggested : /home/pi/python/PyMoIP

Start script

Purpose :

  • Copy 'banned.json' file to '/tmp' (supposed to be a RAM disk)
  • Start 'PyMoIP-Gateway' program with your custom params in a distinct terminal
  • Start each instance of 'PyMoIP-Server' program with your custom params [defined into 'ServerList' flat file] in a distinct terminal

Notice : the @IP '192.168.168.40' should reflect your host's @IP

Script startvdt.sh contents :

#!/usr/bin/env bash
SCRIPTDIR="PyMoIP_dir"
BASEDIR="/home/pi/python/PyMoIP"
cd
cp "$SCRIPTDIR/banned.json" /tmp/banned.json
cd $BASEDIR
lxterminal -t Gateway -e 'bash -c "python3 -m Gateway --tcp 9000 --ws 9001 -i 192.168.168.40 -c ws://127.0.0.1:8764 -v ws://127.0.0.1:8765"'
sleep 5
cd
bash -c "cd $SCRIPTDIR &&  ./PyMoIP_script.sh start all '$BASEDIR' $1 '$SCRIPTDIR'"

Stop script

Purpose :

  • Stop each instance of 'PyMoIP-Server' program that was started with your custom params [defined into 'ServerList' flat file]
  • Stop 'PyMoIP-Gateway' program
  • Save 'banned.json' file from '/tmp' (supposed to be a RAM disk)

Notice : PyMoIP-Gateway is supposed to use port 8999 as "display server" - should be ajusted if necessary

Script stopvdt.sh contents :

#!/usr/bin/env bash
SCRIPTDIR="PyMoIP_dir"
cd
bash -c "cd $SCRIPTDIR && ./PyMoIP_script.sh stop all"
if [ "1" = "1" ] ; then
  sudo netstat -lpn|grep "tcp "|grep "/python3"|grep ":8999 "|
  while read -r g1 g2 g3 g4 g5 g6 g7
  do
    PID=$(echo "$g7"|cut -d/ -f 1)
    echo "G7=$g7"
    sudo netstat -lpn|grep "tcp "|grep "$PID/python3"
    echo "$0 ... Killing PID=$PID gateway on port 8999 and others."
    sudo kill -9 $PID
  done
fi
cd
cp /tmp/banned.json "$SCRIPTDIR/banned.json"
echo "Done"

Restart script

Script contents :

  • Self explainatory

Script restartvdt.sh contents :

sh stopvdt.sh
sh startvdt.sh

PyMoIP script

Purpose :

  • Start or stop an instance of 'PyMoIP-Server' program with your custom params [defined into 'ServerList' flat file]
  • Start or stop all instances of 'PyMoIP-Server' program with your custom params [defined into 'ServerList' flat file]

(Mostly to be called by startvdt.sh and stopvdt.sh)

Script PyMoIP_script.sh contents :

#!/usr/bin/env bash
echo "$0 $1 $2 $3 $4"
input="ServerList"
while IFS=: read -r f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12
do
  if [ "$1" = "start" ]; then
    if [ "$f1" = "1" ]; then
      if [ "$2" = "$f2" ] || [ "$2" = "all" ]; then
        #printf 'Enable=%s port=%s -a=%s arbo=%s -i=%s ip=%s -s=%s service=%s -?=%s last=%s kk=%s ll=%s\n' "$f1" "$f2" "$f3" "$f4" "$f5" "$f6" "$f7" "$f8" "$f9" "$f10" "$f11" "$f12"
        if [ "$2" = "$f2" ]; then
          cd $3
          if [ "$f9" = "-c" ]; then
            python3 -m Server -p $f2 $f3 $f4 $f5 $f6 $f9 $f10 $f7 "$f8"
          else
            python3 -m Server -p $f2 $f3 $f4 $f5 $f6 $f7 "$f8" $f9
          fi
          RET_PID=$!
          RET_CODE=$?
          echo "Started $0 $f2 RET_PID=$RET_PID RET_CODE=$RET_CODE"
        else
          cd
          #echo "$0 $1 $f2 '$3'"
          echo "$0 ... Launching instance $f8 on port $f2."
          lxterminal -t "$f2 $f8" -e 'bash -c "cd '$4' && '$0' '$1' '$f2' '$3' '$4'"'
          #lxterminal -t "$f2 $f8" -e 'bash -c "cd dir && ./script '$1' '$f2' '$3' '$4'"'
        fi
      fi
    fi
  elif [ "$1" = "stop" ]; then
    if [ "$f1" = "1" ]; then
      if [ "$2" = "$f2" ] || [ "$2" = "all" ]; then
        sudo netstat -lpn|grep "tcp "|grep "/python3"|grep ":$f2 "|
        while read -r g1 g2 g3 g4 g5 g6 g7
        do
          arrG7=(${g7//"/"/ })
          #echo "G7=$g7"
          PID="${arrG7[0]}"
          echo "$0 ... Killing PID=$PID instance $f8 on port $f2."
          sudo kill -9 $PID
        done
      fi
    fi
  else
    echo "$0 : Manage PyMoIP instances"
    echo "Usage : $0 [start|stop] [port|all] [basedir] [scriptdir] <wait>"
  fi
done < "$input"
echo "Sleep 5 ...."
sleep 5
echo "$0 $1 $2 terminé"
if [ "$4" = "wait" ] && [ "$2" != "all" ] ; then
  echo "[ENTER] to close"
  read
fi

ServerList file

Purpose : Keep details of your custom params for each instance of 'PyMoIP-Server'

Each line represents a PyMoIP-Server instance.

  • Instances may be disabled with a 0 as first field [never started/stopped by start/stop scripts]
  • Instances may be enabled with a 1 as first field [started/stopped by start/stop scripts]
  • Instance port is defined at second field and also used by stopvdt.sh script (kill -9)
  • Other fields are directly passed to 'PyMoIP-server'
  • Tailling dot of third field is needed for 'PyMoIP-server'

Sample :

1:8765:-a:Server.arbo_teletel.:-i:127.0.0.1:-s:"Teletel Server":-c:8764
1:8766:-a:Server.arbo.:-i:127.0.0.1:-s:"Test Server":-n
0:8766:-a:Server.arbo_disabled.:-i:127.0.0.1:-s:"Test Server Disabled":-n
Clone this wiki locally