Skip to content

Instantly share code, notes, and snippets.

@nmmmnu
Last active November 13, 2018 14:54
Show Gist options
  • Save nmmmnu/b8cd0d22885f00361892f9bf66233cd6 to your computer and use it in GitHub Desktop.
Save nmmmnu/b8cd0d22885f00361892f9bf66233cd6 to your computer and use it in GitHub Desktop.
LXC port forwarding hook
#!/bin/sh
redirect()
{
OP=$1
NETDEV=$2
S_ADDR=$3
S_PORT=$4
D_ADDR=$5
D_PORT=$6
if [[ $OP == "start" ]]
then
iptables -t nat -A PREROUTING -p tcp -i $NETDEV -d $S_ADDR --dport $S_PORT -j DNAT --to-destination $D_ADDR:$D_PORT
iptables -I FORWARD -p tcp -d $D_ADDR --dport $D_PORT -j ACCEPT
else
iptables -t nat -D PREROUTING -p tcp -i $NETDEV -d $S_ADDR --dport $S_PORT -j DNAT --to-destination $D_ADDR:$D_PORT
iptables -D FORWARD -p tcp -d $D_ADDR --dport $D_PORT -j ACCEPT
fi
}
NETDEV=ens192
# vm1 lxc pre-start
# vm1 lxc post-stop
# lxc.hook.pre-start = /var/lib/lxc/vm1/hook.sh
# lxc.hook.post-stop = /var/lib/lxc/vm1/hook.sh
OP_NAME=$1
OP=$3
# ------------------------------------------
NAME=vm1
if [[ $OP_NAME == "$NAME" ]]
then
if [[ $OP == "pre-start" ]]
then
redirect start $NETDEV 77.77.77.82 2022 192.168.122.10 22
fi
if [[ $OP == "post-stop" ]]
then
redirect stop $NETDEV 77.77.77.82 2022 192.168.122.10 22
fi
fi
# ------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment