Skip navigation.
Home

Shell Script

Restarting WiFi on OpenWRT if the Link Goes Away

I put my WRT54G into a noisy electrical environment, and it seems to cause the wifi to fade out a couple times a week, sometimes permanently. (I did this after using a USB adapter on Windows proved too unstable.)

After some experimentation, this script below seems to do a reasonable job of keeping it up. Save it, and put it into the crontab. (You have to install the crond package.) The gateway is at 192.168.1.254.

#! /bin/sh

# Checks if the wifi conn is up.  If not, it tries to restart
# the wifi.  If that fails, then reboot.

if ping -c 1 192.168.1.254 > /dev/null
then
        echo nothing > /dev/null
else
        ifdown wifi
        ifup wifi
        killall wifi
        wifi
        /etc/init.d/S41wpa
        sleep 30

Novice's Notebook

This is a repository of "novice" articles, written with the intent of driving more traffic to the site, and getting more ad clicks. It's pretty crass, I know, but the information may be very useful. Some of the content is adapted from the diy notes, and other notebooks, which are a bit rougher than these.

Most of these articles are not authoritative, because they're based on what I'm learning, as I'm learning it.

OpenWRT Router IP Exposure Script

If you have an OpenWRT router (any router that can run the software can be converted), you can do a little quasi-dynamic-DNS trick. This is useful if you don't really care enough to set up Dyn DNS, and you have a web server setup.

The main disadvantage is that you don't have a DNS record. The main advantage is that the updates don't need to propagate through DNS, so if you have an app that relies on talking to your LAN, you can quickly detect any changes.

First, save this as /usr/bin/checkmyip

#!/bin/sh

past_ip="first"
[ -f /tmp/myip ] &current_ip=`ifconfig | grep P-t-P | tr ':' ' ' | awk '{print $3;}'`

if [ ${past_ip} != ${current_ip} ] ; then
exec wget -q -O /tmp/myip.html http://yourdomain.com/networkname/wrt.php?ip=${current_ip} 2>&1 &

Set UID C fragment

I'll be getting back to the regular code in a bit. For now, here's a tiny code fragment I'm using, now recorded here for posterity. I'm in a little shock - this is the only C code I use anymore.

Sometimes, you need to run a script as root. This is a little bit of C code that does that. The program compiles, and you chmod it to setuid, and chown it root.

#define SCRIPT "/usr/local/buildserver/setup_web"
main(argc, argv)
char **argv;
{
    setuid(0);
    seteuid(0);
    execv(SCRIPT, argv);
}

Yikes! I didn't know I still had old K&R C lying around. I thought that went out of fashion at the same time as Michael Jackson's "Bad" album.

The main() should be: main(int argc, char *argv[])

It's also uncool to pass argv to the script. Setuid proxies can filter the arguments so that the script exposes a limited interface to root. Maybe this library could help impose some order.

Syndicate content