Last active
November 3, 2017 19:46
-
-
Save damekr/7abf15670963eabaa9daf744c849c266 to your computer and use it in GitHub Desktop.
Script to redirect port on host to diffetent ip
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 | |
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