Skip to content

Instantly share code, notes, and snippets.

@kadel
Created September 19, 2017 11:40
Show Gist options
  • Select an option

  • Save kadel/24d62079c522c1db851dc6bc302db43e to your computer and use it in GitHub Desktop.

Select an option

Save kadel/24d62079c522c1db851dc6bc302db43e to your computer and use it in GitHub Desktop.
vpsfree kvm ports
#!/bin/bash
# used some from advanced script to have multiple ports: use an equal number of guest and host ports
# /etc/libvirt/hooks/qemu
# Update the following variables to fit your setup
Guest_name='kvm'
Guest_ipaddr='' //MODIFY
Host_ipaddr='' //MODIFY
Guest_port=( '80' '443' '22' )
Host_port=( '80' '443' '2222' )
length=$(( ${#Host_port[@]} - 1 ))
if [ "${1}" = "${Guest_name}" ]; then
if [ "${2}" = "stopped" ] || [ "${2}" = "reconnect" ]; then
for i in `seq 0 $length`; do
iptables -t nat -D PREROUTING -d ${Host_ipaddr} -p tcp --dport ${Host_port[$i]} -j DNAT --to ${Guest_ipaddr}:${Guest_port[$i]}
iptables -D FORWARD -d ${Guest_ipaddr}/32 -p tcp -m state --state NEW -m tcp --dport ${Guest_port[$i]} -j ACCEPT
iptables -t nat -D PREROUTING -d ${Host_ipaddr} -p udp --dport ${Host_port[$i]} -j DNAT --to ${Guest_ipaddr}:${Guest_port[$i]}
iptables -D FORWARD -d ${Guest_ipaddr}/32 -p udp -m state --state NEW -m udp --dport ${Guest_port[$i]} -j ACCEPT
done
fi
if [ "${2}" = "start" ] || [ "${2}" = "reconnect" ]; then
for i in `seq 0 $length`; do
iptables -t nat -A PREROUTING -d ${Host_ipaddr} -p tcp --dport ${Host_port[$i]} -j DNAT --to ${Guest_ipaddr}:${Guest_port[$i]}
iptables -I FORWARD -d ${Guest_ipaddr}/32 -p tcp -m state --state NEW -m tcp --dport ${Guest_port[$i]} -j ACCEPT
iptables -t nat -A PREROUTING -d ${Host_ipaddr} -p udp --dport ${Host_port[$i]} -j DNAT --to ${Guest_ipaddr}:${Guest_port[$i]}
iptables -I FORWARD -d ${Guest_ipaddr}/32 -p udp -m state --state NEW -m udp --dport ${Guest_port[$i]} -j ACCEPT
done
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment