#!/bin/bash set -e listen_iface=${1} listen_port=${2} target_host=${3} target_port=${4} # Check non-empty [[ -n "${listen_iface}" ]] [[ -n "${listen_port}" ]] [[ -n "${target_host}" ]] [[ -n "${target_port}" ]] # Check that ports is actually numbers [[ ${listen_port} -eq ${listen_port} ]] [[ ${target_port} -eq ${target_port} ]] listen_address=$(ip -f inet addr show dev ${listen_iface} | grep -Po 'inet \K[\d.]+') [[ -n "${listen_address}" ]] echo " == Forwarding ${listen_address}:${listen_port} -> ${target_host}:${target_port} == " # Socat socat -d -d TCP4-LISTEN:${listen_port},bind=${listen_address},su=nobody,fork,reuseaddr TCP4:${target_host}:${target_port}