How to forward a range of ports (TCP and UDP) script?

Hello!
There is a CentOS VM in it Windows (ip vm=192.168.122.253)
To work, you need to forward a range of ports 6615 6601 for UDP and TCP (just a question: How to set TCP and UDP in one script in one file ?

1st option script file (e.g. /etc/libvirt/hooks/qemu_6601-6615):
spoiler
#!/bin/bash
# used some from advanced script to have multiple ports: use an equal number of guest and host ports
# Update the following variables to fit your setup
Guest_name=wsvm
Guest_ipaddr=192.168.122.253
Host_ipaddr=EXTERNAL IP
Host_port=( '6601-6615' )
Guest_port=( '6601-6615' )

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 policy -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
done
fi
 if [ "${2}" = "start" ] || [ "${2}" = "reconnect" ]; then
 for i in `seq 0 $length`; do
 iptables-t nat -A PREROUTING policy -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
done
fi
fi



and then restart libvirtd
chmod +x /etc/libvirt/hooks/qemu_6601-6615

OR

The 2nd option, the script file (for example qemu_superscript_6601-6615):

spoiler
#!/bin/bash

hostif="eth0"
hostip=$(/sbin/ifconfig "$hostif" | /usr/bin/awk -F: '/inet addr/ {split($2, a, " "); print a[1]}')

# Format: guestname,sourceip,hostpt,guestip,guestpt per string
# sourceip leave field empty for connect from anywhere
datafile="/etc/libvirt/hooks/server_port_map"
iptables='/sbin/iptables'

while IFS=, read-r guestname sourceip hostpt guestip guestpt ; do
if [ -z $sourceip ] ; then
sourceip="0.0.0.0/0"
fi
if [ $1 = $guestname ] ; then
 if [[ $2 == @(stopped|reconnect) ]] ; then
 $iptables-w-t nat -D PREROUTING policy -i $hostif -s $sourceip -d $hostip -p tcp --dport $hostpt -j DNAT --to-destination ${guestip}:${guestpt}
 $iptables-w-D FORWARD-i $hostif -s $sourceip -m state --state NEW-m tcp -p tcp -d $guestip --dport $guestpt -j ACCEPT
fi
 if [[ $2 == @(start|reconnect) ]] ; then
 $iptables-w-t nat -I PREROUTING policy -i $hostif -s $sourceip -d $hostip -p tcp --dport $hostpt -j DNAT --to-destination ${guestip}:${guestpt}
 $iptables-w-I FORWARD-i $hostif -s $sourceip -m state --state NEW-m tcp -p tcp -d $guestip --dport $guestpt -j ACCEPT
fi
fi
done < $datafile



The second file (/etc/libvirt/hooks/server_port_map):
spoiler
wsvm,$remote_ip1,6601-6615,192.168.122.253,6601-6615
do I need to write it $remote_ip1 ? in the script I realized there is this variable. or instead of the external IP to write?


also then
chmod +x /etc/libvirt/hooks/qemu_superscript_6601-6615

---------------------------

Which script is better or can you advise an alternative
July 9th 19 at 10:24
1 answer
July 9th 19 at 10:26
my advice - get familiar with parentheses.

Find more questions by tags LinuxNetwork administrationQEMU