Last active
February 21, 2020 09:28
-
-
Save bkonetzny/aebb7bba6c6a2dcaab9fd8dbdc56736b to your computer and use it in GitHub Desktop.
Tunnel a TCP request through a remote server
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 | |
SSH_HOST=YOUR-REMOTE-SSH-SERVER-HOST | |
SSH_PORT=YOUR-REMOTE-SSH-SERVER-PORT | |
LOCAL_HOST=127.0.0.1 | |
LOCAL_PORT=8888 | |
read -p 'Target Host/IP: ' TARGET_HOST | |
read -p 'Target Port: ' TARGET_PORT | |
IPTABLES_RULE="OUTPUT -p tcp -d $TARGET_HOST --dport $TARGET_PORT -j DNAT --to-destination $LOCAL_HOST:$LOCAL_PORT" | |
function finish { | |
sudo iptables -t nat -D $IPTABLES_RULE | |
echo "Deleted iptables entry for $TARGET_HOST:$TARGET_PORT" | |
} | |
trap finish EXIT | |
sudo sysctl net.ipv4.ip_forward=1 | |
sudo iptables -t nat -A $IPTABLES_RULE | |
echo "Added iptables entry for $TARGET_HOST:$TARGET_PORT" | |
echo "Connecting to ssh server $SSH_HOST:$SSH_PORT" | |
ssh -N -L $LOCAL_PORT:$TARGET_HOST:$TARGET_PORT $SSH_HOST -p $SSH_PORT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment