0

I'm writing a container manager script which depends on nspawn, and the container is using a Virtual Ethernet Connection(-n opt).

but it looks like that nspawn only support port mapping for single port at this time

How can i map multiple ports from the guest machine to the host machine?

nspawn doc

my script

1 Answer 1

0

-p support multiple times in one single command, so i use a loop to add ports one by one

Here's my code

set port_range $_flag_port
                if echo $port_range | grep -qs -
                    set -e port_mapping_tcp
                    set -e port_mapping_udp
                    set -e port_mapping
                    set port_counter 0
                    for port_arrary in (seq (echo $port_range | awk -F "-" '{print $1}') (echo $port_range | awk -F "-" '{print $2}'))
                        set port_counter (math $port_counter+1)
                        set port_mapping_tcp[$port_counter] "-ptcp:$port_arrary"
                        set port_mapping_udp[$port_counter] "-pudp:$port_arrary"
                    end
                else
                    if echo $port_range | grep -qs ,
                        set -e port_mapping_tcp
                        set -e port_mapping_udp
                        set -e port_mapping
                        set port_counter 0
                        for port_arrary in (echo $port_range | string split ,)
                            set port_counter (math $port_counter+1)
                            set port_mapping_tcp[$port_counter] "-ptcp:$port_arrary"
                            set port_mapping_udp[$port_counter] "-pudp:$port_arrary"
                        end
                    else
                        set port_mapping_tcp "-ptcp:$_flag_port"
                        set port_mapping_udp "-pudp:$_flag_port"
                    end
                end
                sudo systemd-nspawn --resolv-conf=off $port_mapping_tcp $port_mapping_udp -bnq -D $container

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .