#!/bin/bash

host=ie-entwicklung@10.2.3.104

tunnel() {
  name=$1
  ip=$2
  rPort=$3
  lPort=$4

  echo -n "localhost:$lPort    $name  "
  ssh -nNTL "$lPort:$ip:$rPort" "$host" 2> /dev/null &

  until nc -vz 127.0.0.1 $lPort 2> /dev/null
  do
    echo -n .
    sleep 0.3
  done

  echo "ok"
}

echo ""

tunnel "Trumpf Inverter (http)  " 192.168.1.2   80 7001
tunnel "Trumpf DCDC (http)      " 192.168.1.3   80 7002
tunnel "Emu Meter (http)        " 192.168.1.241 80 7003
tunnel "ADAM (http)             " 192.168.1.242 80 7004
tunnel "AMPT (http)             " 192.168.1.249 8080 7005

tunnel "Trumpf Inverter (modbus)" 192.168.1.2   502 5001
tunnel "Trumpf DCDC (modbus)    " 192.168.1.3   502 5002
tunnel "Emu Meter (modbus)      " 192.168.1.241 502 5003
tunnel "ADAM (modbus)           " 192.168.1.242 502 5004
tunnel "AMPT (modbus)           " 192.168.1.249 502 5005

echo
echo "press any key to close the tunnels ..."
read -r -n 1 -s
kill $(jobs -p)
echo "done"