Skip to content

Instantly share code, notes, and snippets.

@ir4y
Last active December 25, 2015 20:49
Show Gist options
  • Select an option

  • Save ir4y/7038318 to your computer and use it in GitHub Desktop.

Select an option

Save ir4y/7038318 to your computer and use it in GitHub Desktop.
#!/bin/bash
Guest_name=exmaple.com
Host_ports=("7626" "8080" "8008")
Guest_ipaddr=192.168.0.1
Guest_ports=("22" "8001" "8008")
if [ "${1}" = "${Guest_name}" ]; then
if [ "${2}" = "start" ]; then
for ((i=0;i<${#Host_ports[@]};i++)); do
Host_port=${Host_ports[${i}]}
Guest_port=${Guest_ports[${i}]}
iptables -t nat -A PREROUTING -i eth1 -p tcp --dport ${Host_port} -j DNAT \
--to ${Guest_ipaddr}:${Guest_port}
iptables -I FORWARD -d ${Guest_ipaddr}/32 -p tcp -m state --state NEW \
-m tcp --dport ${Guest_port} -j ACCEPT
done
elif [ "${2}" = "stopped" ]; then
for ((i=0;i<${#Host_ports[@]};i++)); do
Host_port=${Host_ports[${i}]}
Guest_port=${Guest_ports[${i}]}
iptables -t nat -D PREROUTING -i eth1 -p tcp --dport ${Host_port} -j DNAT \
--to ${Guest_ipaddr}:${Guest_port}
iptables -D FORWARD -d ${Guest_ipaddr}/32 -p tcp -m state --state NEW \
-m tcp --dport ${Guest_port} -j ACCEPT
done
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment