#!/bin/bash set -eE -o pipefail trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR do-forward() { cmd=$1; shift for port; do /sbin/iptables -t nat $cmd PREROUTING -i eth0 -p tcp -m tcp --dport $port -j DNAT --to-destination 10.8.0.4:$port done } ports=(25 143) case $1 in start) do-forward -A ${ports[@]} ;; stop) do-forward -D ${ports[@]} ;; *) echo "$0: error: expected 1 argument of start or stop" exit 1 ;; esac