Skip to content

Instantly share code, notes, and snippets.

@emiller
Last active December 24, 2015 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emiller/6768317 to your computer and use it in GitHub Desktop.
Save emiller/6768317 to your computer and use it in GitHub Desktop.
Simple utility script that allows you to route incoming TCP traffic on one port to another.
#!/bin/bash
#
# port-route -- route one port's traffic to another.
#
# Simple utility that wraps `iptables` to enable routing TCP
# traffic of one port to another.
#
# Usage:
#
# port-route <up|down|show> <source port> <destination port>
#
# Examples:
#
# port-route up 23 22 <-- route incoming telnet traffic to ssh
# port-route up 80 22 <-- route incoming HTTP traffic to ssh
# port-route down 80 22 <-- remove the HTTP -> ssh routing
#
# @author emiller
# @date 2013-09-30
#
function usage() {
echo "usage: `basename $0` <up|down|show> [src port] [dest port]"
[ -z "$1" ] || echo $1
exit 1
}
act=$1
src=$2
dst=$3
if [[ ! -z "$act" && "$act" != "show" ]]; then
[ -z "$src" ] && usage "missing source port"
[ -z "$dst" ] && usage "missing destination port"
fi
case $act in
up)
echo "enabling port route $src -> $dst"
opts="-A PREROUTING"
;;
down)
echo "disabling port route $src -> $dst"
opts="-D PREROUTING"
;;
show)
sudo iptables -t nat --list
exit
;;
*)
usage
;;
esac
sudo iptables -t nat $opts -p tcp --dport $src -j REDIRECT --to-ports $dst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment