1

First of all, I can make basic use of the linux command line, but have slim to none experience with shell scripting.

What I want to do: I have a router running OpenWRT, a destop pc and an Android phone. When the phone connects to the wifi, I want the router to wake-on-lan my pc.

I have a vague idea on how to do this but need a little help on putting the pieces together:

  • run a script with crontabs every 10 seconds to check if the phone is connected
  • script does: iwinfo wlan0 assoclist to check if my phone's MAC is connected
  • if my phone is connected, send wake-on-lan packet
  • keep a boolean pcHasBeenWokenUp so the script doesn't send WOL packets all the time (because the phone is connected )
  • when the phone disappeared for > 10 minutes: set pcHasBeenWokenUp to false

a. Is this the right direction?

b. Should the script be a bash script or rather something like perl?

c. How can I maintain my pcHasBeenWokenUp variable between the starts of my script?

Some hints would be highly appreciated :)

  • 1
    That's actually a nice idea — but you can't do it with a cronjob. Cron has a minimum granularity of one minute, so you can't schedule things every ten seconds. You'll probably need to write a daemon to do this so as not to tax the router too much. – Alexios Dec 18 '13 at 22:27
2

I believe your simple algorithm would work, but you must realize the router would never know for sure whether the pc is actually awaken or not. If your automation can tolerate such behavior, then you could implement it like follows.

if ! $pcHasBeenWokenUp; 
then 
    iwinfo wlan0 assoclist | grep $MAC &> /dev/null && wakeUpPc && export pcHasBeenWokenUp=true; 
fi

if $pcHasBeenWokenUp; 
then 
    iwinfo wlan0 assoclist | grep -v $MAC &> /dev/null && export pcHasBeenWokenUp=false; 
fi

Please note however, as has been mentioned before, that a cronjob cannot run scripts in intervals of 10 seconds. You could however trick it, by running a job with a greater periodicity, which itself runs a loop executing your task in intervals of 10 seconds (see here).

Also keep in mind this is a very hacky solution, which nonetheless does its job well enough. If, however, you plan on using it as part of a bigger scenario, maybe it would be better to move on to daemons.

  • 1
    This is very helpful, thanks. In fact I could check if the PC is awake by pinging it a couple of times and processing the result, given the PC has a static IP, don't I? – asco Dec 18 '13 at 22:35
  • @Ascorbin surely, that would be quite simple indeed, but as I said, if you do need a robust algorithm, then maybe you'd be better off using daemons. – brunocodutra Dec 18 '13 at 22:40
  • This is pure fun & play, I'll see if I can get a 'hackish' solution to work, then move on to a more elaborate approach ;) Thanks very much! – asco Dec 18 '13 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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