Skip to content

Instantly share code, notes, and snippets.

@damekr
Last active November 3, 2017 19:46
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 damekr/7abf15670963eabaa9daf744c849c266 to your computer and use it in GitHub Desktop.
Save damekr/7abf15670963eabaa9daf744c849c266 to your computer and use it in GitHub Desktop.
Script to redirect port on host to diffetent ip
#!/bin/bash
function printHelp(){
echo "This tool can be used to redirect tcp port from host on which is being run to another routable ip and port from this host"
echo
echo "USAGE:"
echo "$(basename $0) <destinationIP> <sourcePort> <destinationPort>"
}
function applyRules(){
iptables -A POSTROUTING -j MASQUERADE
iptables -A PREROUTING -p tcp --dport $2 -j DNAT --to-destination $1:$3 -o eth0
iptables -A FORWARD -d $1 -p tcp --dport $3 -j ACCEPT
iptables -A FORWARD -s $1 -p tcp --sport $3 -j ACCEPT
}
function main(){
if [ $# -ne 3 ]; then
echo "ERROR: Not enough arguments given"
printHelp
exit 1
fi
applyRules $@
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment