OpenWrt Forum Archive

Topic: Need help writing my first script

The content of this topic has been archived on 23 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi guys,

I want my router to send a list of connected wifi clients via UDP message to my home automatization.

These three lines do exatly what I want:

iwinfo wlan0 assoclist > assoclist.txt
iwinfo wlan1 assoclist >> assoclist.txt
nc -u -w1 -c  192.168.1.8 5678 < assoclist.txt

I would like to send the file every other second so my idea is something like this:

#!/bin/sh 

while :
do
iwinfo wlan0 assoclist > assoclist.txt
iwinfo wlan1 assoclist >> assoclist.txt
nc -u -w1 -c  192.168.1.8 5678 < assoclist.txt
sleep 1
done

Now we come to the point where I’m struggling. From what I read in the FAQ I can ether create an init script or an procd init script. Which one is the better choice in my case?

Assuming it’s init script would this work:

#!/bin/sh /etc/rc.common

START=50

start() { 
while :
do
iwinfo wlan0 assoclist > assoclist.txt
iwinfo wlan1 assoclist >> assoclist.txt
nc -u -w1 -c  192.168.1.8 5678 < assoclist.txt
sleep 1
done
}  

Further Questions:
What do I need to do to run the start the script automatically after a reboot?
Will this place significant load on my router (do I need to lengthen the pause)?

(Last edited by Swallowtail on 4 Jan 2018, 13:32)

First, put temporary files under /tmp.  This is a RAM disk with much faster access and it won't wear out your flash chip.

The easiest way to start a script after reboot is through /etc/rc.local.  To make it a full service (having enable / disable / restart options and automatic restart if it crashes), use the init.d script of a simple daemon service like gpsd as an example.

Delete

(Last edited by Swallowtail on 5 Jan 2018, 15:46)

Solved it by writing two scripts:

/etc/loxone_assoclist:

#!/bin/sh

while :
do
    iwinfo wlan0 assoclist > /tmp/assoclist.txt
    iwinfo wlan1 assoclist >> /tmp/assoclist.txt
    nc -u -w1 -c  192.168.1.8 5678 < /tmp/assoclist.txt
    sleep 10
done

/etc/init.d/loxone:

#!/bin/sh /etc/rc.common


START=90
STOP=95

start() {        
        echo start
        /etc/loxone_assoclist &>/dev/null & 
        echo $! > /tmp/lox.pid 
}                 
 
stop() {          
        echo stop
        cat /tmp/lox.pid | xargs kill -9
        rm /tmp/lox.pid
        rm /tmp/assoclist.txt
}

The discussion might have continued from here.