Zack Williams | 4bd2dbc | 2016-03-10 12:50:02 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | SHELL="/bin/bash" |
| 4 | |
| 5 | NIC=$( route|grep default|awk '{print $NF}' ) |
| 6 | |
| 7 | NAME="${1}" |
| 8 | OP="${2}" |
| 9 | SUBOP="${3}" |
| 10 | ARGS="${4}" |
| 11 | |
| 12 | add_port_fwd_rule() { |
| 13 | DPORT=$1 |
| 14 | VM=$2 |
| 15 | TOPORT=$3 |
| 16 | |
| 17 | VMIP=$( getent ahosts $VM|head -1|awk '{print $1}' ) |
| 18 | iptables -t nat -C PREROUTING -p tcp -i $NIC --dport $DPORT -j DNAT --to-destination $VMIP:$TOPORT |
| 19 | if [ "$?" -ne 0 ] |
| 20 | then |
| 21 | iptables -t nat -A PREROUTING -p tcp -i $NIC --dport $DPORT -j DNAT --to-destination $VMIP:$TOPORT |
| 22 | fi |
| 23 | } |
| 24 | |
| 25 | if [ "$OP" = "start" ] || [ "$OP" = "reload" ] |
| 26 | then |
| 27 | iptables -t nat -F |
| 28 | |
| 29 | {% for vm in head_vm_list -%} |
| 30 | {% if vm.forwarded_ports is defined -%} |
| 31 | {% for port in vm.forwarded_ports -%} |
| 32 | add_port_fwd_rule {{ port.ext }} {{ vm.name }} {{ port.int }} |
| 33 | {% endfor -%} |
| 34 | {% endif -%} |
| 35 | {% endfor -%} |
| 36 | |
| 37 | # Also flush the filter table before rules re-added |
| 38 | iptables -F |
| 39 | fi |
| 40 | |