Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Last active November 7, 2024 09:40
Show Gist options
  • Select an option

  • Save drmalex07/1b8160f6c1761028dfff6c68d1d38156 to your computer and use it in GitHub Desktop.

Select an option

Save drmalex07/1b8160f6c1761028dfff6c68d1d38156 to your computer and use it in GitHub Desktop.

Revisions

  1. drmalex07 revised this gist Feb 20, 2020. 2 changed files with 28 additions and 40 deletions.
    35 changes: 14 additions & 21 deletions socat-forward-tcp4-to-tcp6.sh
    Original file line number Diff line number Diff line change
    @@ -1,30 +1,23 @@
    #!/bin/bash
    set -e

    # Forward to an IPv6 address
    listen_iface=${1}
    listen_port=${2}
    target_host=${3}
    target_port=${4}

    # Change me!
    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
    # 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
    33 changes: 14 additions & 19 deletions socat-forward-tcp4.sh
    Original file line number Diff line number Diff line change
    @@ -1,28 +1,23 @@
    #!/bin/bash
    set -e

    # Change me!
    PUBLIC_IP4_IFACE=eth2
    LISTEN_IFACE=${PUBLIC_IP4_IFACE}
    listen_iface=${1}
    listen_port=${2}
    target_host=${3}
    target_port=${4}

    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
    # 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
  2. drmalex07 revised this gist Feb 20, 2020. 2 changed files with 2 additions and 0 deletions.
    1 change: 1 addition & 0 deletions socat-forward-tcp4-to-tcp6.sh
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@

    # Forward to an IPv6 address

    # Change me!
    PUBLIC_IP4_IFACE=eth2
    LISTEN_IFACE=${PUBLIC_IP4_IFACE}

    1 change: 1 addition & 0 deletions socat-forward-tcp4.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    #!/bin/bash

    # Change me!
    PUBLIC_IP4_IFACE=eth2
    LISTEN_IFACE=${PUBLIC_IP4_IFACE}

  3. drmalex07 revised this gist Feb 20, 2020. 2 changed files with 30 additions and 0 deletions.
    30 changes: 30 additions & 0 deletions socat-forward-tcp4-to-tcp6.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/bin/bash

    # Forward to an IPv6 address

    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 TCP6:[${target_host}]:${target_port}
    File renamed without changes.
  4. drmalex07 revised this gist Jun 26, 2018. 2 changed files with 28 additions and 9 deletions.
    28 changes: 28 additions & 0 deletions socat-forward-tcp.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    #!/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}
    9 changes: 0 additions & 9 deletions socat-ssh.sh
    Original file line number Diff line number Diff line change
    @@ -1,9 +0,0 @@
    #!/bin/bash

    LISTEN_ADDRESS=1.2.3.4
    LISTEN_PORT=10022

    TARGET_HOST=app.localdomain
    TARGET_PORT=22

    socat -d -d TCP4-LISTEN:${LISTEN_PORT},bind=${LISTEN_ADDRESS},su=nobody,fork,reuseaddr TCP4:${TARGET_HOST}:${TARGET_PORT}
  5. drmalex07 revised this gist May 24, 2018. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions socat-ssh.sh
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,6 @@
    #!/bin/bash

    PUBLIC_IP4_ADDRESS=1.2.3.4

    LISTEN_ADDRESS=${PUBLIC_IP4_ADDRESS}
    LISTEN_ADDRESS=1.2.3.4
    LISTEN_PORT=10022

    TARGET_HOST=app.localdomain
  6. drmalex07 revised this gist May 24, 2018. No changes.
  7. drmalex07 created this gist May 24, 2018.
    11 changes: 11 additions & 0 deletions socat-ssh.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    #!/bin/bash

    PUBLIC_IP4_ADDRESS=1.2.3.4

    LISTEN_ADDRESS=${PUBLIC_IP4_ADDRESS}
    LISTEN_PORT=10022

    TARGET_HOST=app.localdomain
    TARGET_PORT=22

    socat -d -d TCP4-LISTEN:${LISTEN_PORT},bind=${LISTEN_ADDRESS},su=nobody,fork,reuseaddr TCP4:${TARGET_HOST}:${TARGET_PORT}