Skip to content

Instantly share code, notes, and snippets.

@blackandred
Created March 22, 2017 05:52
Show Gist options
  • Save blackandred/4813f1cd1d7d1a595015bb893b602eb1 to your computer and use it in GitHub Desktop.
Save blackandred/4813f1cd1d7d1a595015bb893b602eb1 to your computer and use it in GitHub Desktop.
#1: Redirect traffic from port to port (local) #2: Route all incoming traffic to other host preserving the information about requester's IP address
#!/bin/bash
function get_gateway_interface() {
route|grep default|awk '{print $8}'
}
# Redirect all incoming traffic to the other port
function redirect_port_to_port() {
# $1 from port
# $2 to port
iptables -t nat -A PREROUTING -i $(get_gateway_interface) -p tcp --dport $1 -j REDIRECT --to-port $2
}
function redirect_port_to_other_host() {
# $1 port
# $2 target IP address
gatewayInterface=$(get_gateway_interface)
iptables -t nat -A DOCKER -i $gatewayInterface -p tcp --dport $1 -j DNAT --to-destination "$2:$1"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment