Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am sitting here with an issue: I need to switch the source interface for iptables' NAT rule at a given time. I want to basically do soemthing like this - caution: concept code:

#!/bin/bash
if [ it is 11.30pm ]; then
    iptables CHANGE FIRST RULE IN FORWARD CHAIN AND MAKE ORIGINAL -o wlan0 INTO -o ppp0
fi

A same script needs to run at 8AM to change that backwards - wlan0 to ppp0. I am setting up my current nat with an init.d script:

#!/bin/bash

### BEGIN INIT INFO
# Provides:          subnet
# Required-Start:    ifplugd
# Required-Stop:     $ifplugd
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Brings up the NAT system
# Description:       Initializes the NAT routing (SUBnet)
### END INIT INFO

# This is rather simple, as we have the script already...
[ "$1" != "" || "$1" != "start" ] && exit 0;

echo -n .
/sbin/iptables -A FORWARD -o wlan0 -i eth0 -s 192.168.200.0/24 -m conntrack --ctstate  NEW -j ACCEPT

echo -n .
/sbin/iptables -A FORWARD -m conntrack -ctstate ESTABLISHED,RELATED -j ACCEPT

echo -n .
/sbin/iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE

echo
echo "[ OK! ] NAT is now initialized with wlan0->eth0."

This works nicely during boot, but I want the source interface to change, so I have continious internet.

At some point, the ppp0 device might not be present, so the script would also need to check for its presence, and then switch. I will be using a udev rule - most probably - to run sakis3g --connect once the device is connected.

Does anyone have any idea how to realize that? I am new to cronjobs, and I cant wrap my head really aorund IPTABLES, I am glad I got to put that init.d script together :)

share|improve this question

1 Answer 1

iptables doesn't control on which interface the packet is going out. It simply allows the packet to go out that interface and changes its source address.

Adding rules for both interfaces should be enough for NAT to work on both interfaces.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.