Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Last active November 7, 2024 09:40
Show Gist options
  • Save drmalex07/1b8160f6c1761028dfff6c68d1d38156 to your computer and use it in GitHub Desktop.
Save drmalex07/1b8160f6c1761028dfff6c68d1d38156 to your computer and use it in GitHub Desktop.
Tunnel TCP traffic via socat. #socat
#!/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 TCP6:[${target_host}]:${target_port}
#!/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}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment