From b8f42ab1f6dc17bbb352bcc3a12020d47429df00 Mon Sep 17 00:00:00 2001 From: Vasudeva Kamath Date: Thu, 14 Jul 2016 17:52:31 +0530 Subject: Handle source nating when interface has more IP Some time interface can have 2 ip address handle this case, though I feel only first IP will be considered by iptables. --- port_forward | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/port_forward b/port_forward index 4a3c9d1..4842b3c 100755 --- a/port_forward +++ b/port_forward @@ -35,8 +35,10 @@ setup_portforwarding () { iptables -A FORWARD -p "$protocol" -d ${DESTINATION%%\:*} --dport $PORT -j ACCEPT # Returning packet should have gateway IP - iptables -t nat -A POSTROUTING -s ${DESTINATION%%\:*} -o \ - $IN_INTERFACE -j SNAT --to ${IN_IP%%\/*} + for ip in ${IN_IP} ; do + iptables -t nat -A POSTROUTING -s ${DESTINATION%%\:*} -o \ + $IN_INTERFACE -j SNAT --to ${ip%%\/*} + done } @@ -82,7 +84,7 @@ DESTINATION="$4" # Get the incoming interface IP. This is used for SNAT. IN_IP=$(ip addr show $IN_INTERFACE|\ - perl -nE '/inet\s(.*)\sbrd/ and print $1') + perl -nE '/inet\s(.*)\sscope/ and say $1' | tr '\n' ' ') if [ -n "$CLEAR_RULES" ]; then -- cgit v1.2.3