Skip to content

Instantly share code, notes, and snippets.

@loru88
Created March 11, 2019 08:58
Show Gist options
  • Save loru88/ec4ff24cd5f172f23d9dcb3650eaa8fd to your computer and use it in GitHub Desktop.
Save loru88/ec4ff24cd5f172f23d9dcb3650eaa8fd to your computer and use it in GitHub Desktop.
SSH port forwarding for IPs without change them to localhost
ADDRESS_TO_FORWARD = [
"1.2.3.4:8088",
]
CLIENT_HOST = "127.0.0.1"
CLIENT_PORT = 4444
REMOTE_HOSTNAME = "user@host"
ip_tables = []
ssh_forward_to = []
count = 0
for address in ADDRESS_TO_FORWARD:
parts = address.split(':')
ip = parts[0]
port = int(parts[1])
client_port = int(CLIENT_PORT + count)
ip_tables.append("iptables -t nat -A OUTPUT -p tcp --dport %d -d %s -j DNAT --to-destination %s:%d" % (port, ip, CLIENT_HOST, client_port))
ssh_forward_to.append("-L %s:%d:%s" % (CLIENT_HOST, client_port, address))
count += 1
ssh_tunnel = "ssh -4 -o ExitOnForwardFailure=yes -N " + " ".join(ssh_forward_to) + " " + REMOTE_HOSTNAME
print("\n".join(ip_tables))
print(ssh_tunnel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment