Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.