Forked from drmalex07/socat-forward-tcp4-to-tcp6.sh
Created
November 7, 2024 09:40
-
-
Save eldorplus/6010734c15ce9463a6d16122b20928bf to your computer and use it in GitHub Desktop.
Tunnel TCP traffic via socat. #socat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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