2007年12月29日

m0n0wall pppoe redial - with shellcmd and minicron


My home HiNet ADSL, would be disconnected every 24 hours or so (last time i checked. about 7 or 8 years ago), it got automatically reconnected alright, but i dont like it being disconnected and reconnected when i'm up to something important.

so i wrote a script on my gateway host to redial the pppoe every 5am in the morning.

now with m0n0wall, there's no such a funtion to do the pppoe redial, so i'd like to make one. (pfSense has it!)

There's no 'crontab' in m0n0wall, but 'minicron'. With minicron, we can only tell the system to do the job 'every X seconds', not precisely 'when' to do the job.

after installing the dropbear ssh module for m0n0wall, i tried the following script under /tmp. the intention is to have the system check the current time every 60 seconds, if the current time (hh:mm) matches the preset value (HH:MM), then it's the time to get busy!

# cat /tmp/redial.php
#!/usr/local/bin/php
<? php>
require_once("/etc/inc/functions.inc");
$time=1448; (say 2:48 pm)
$ph = popen("/bin/date \"+%H%M\"","r");
$line = fgets($ph); pclose($ph);
if (chop($line) == $time) {
interfaces_wan_pppoe_down();
interfaces_wan_pppoe_up();
}
?>

The script works as expected. Next is to have it sustained every m0n0wall reboot. With <shellcmd>, put the following section in the configuration file, right before </system> :


<shellcmd>echo "#!/usr/local/bin/php" > /tmp/redial.php</shellcmd>
<shellcmd>echo "<?php>" >> /tmp/redial.php</shellcmd>
<shellcmd>echo "require_once(\"/etc/inc/functions.inc\");" >> /tmp/redial.php</shellcmd>
<shellcmd>echo "\$time=\"1448\";" >> /tmp/redial.php</shellcmd>
<shellcmd>echo "\$ph = popen(\"/bin/date \\\"+%H%M\\\"\",\"r\");" >> /tmp/redial.php</shellcmd>
<shellcmd>echo "\$line = fgets(\$ph); pclose(\$ph);" >> /tmp/redial.php</shellcmd>
<shellcmd>echo "if (chop(\$line) == \$time) {" >> /tmp/redial.php</shellcmd>
<shellcmd>echo " interfaces_wan_pppoe_down();" >> /tmp/redial.php</shellcmd>
<shellcmd>echo " interfaces_wan_pppoe_up();" >> /tmp/redial.php</shellcmd>
<shellcmd>echo "}" >> /tmp/redial.php</shellcmd>
<shellcmd>echo "?>" >> /tmp/redial.php</shellcmd>
<shellcmd>chmod 755 /tmp/redial.php</shellcmd>
<shellcmd>rm /var/run/redial.pid; /usr/local/bin/minicron 60 /var/run/redial.pid /tmp/redial.php</shellcmd>

and "Restore" the modified configuration file, reboot to make it work,
there you go. it's more like an appliance now.

沒有留言: