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 :)