Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tunnel TCP traffic via socat. #socat
#!/bin/bash
PUBLIC_IP4_IFACE=eth2
LISTEN_IFACE=${PUBLIC_IP4_IFACE}
listen_address=$(ip -f inet addr show dev ${LISTEN_IFACE} | grep -Po 'inet \K[\d.]+')
listen_port=${1}
target_host=${2}
target_port=${3}
if [ -z "${listen_port}" ]; then
echo "${0} <listen-port> <target-host> <target-port>" && exit 1;
fi
if [ -z "${target_host}" ]; then
echo "${0} <listen-port> <target-host> <target-port>" && exit 1;
fi
if [ -z "${target_port}" ]; then
echo "${0} <listen-port> <target-host> <target-port>" && exit 1;
fi
# Test that ports is actually numbers
[[ ${listen_port} -eq ${listen_port} ]]
[[ ${target_port} -eq ${target_port} ]]
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}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.