Posted: Thu Dec 08, 2011 15:29 Post subject: Automatic WOL by monitoring IP active
Hello!
I'm desperately searching a script or different way to send a WOL to my server if my TV is on.
I think it could be possible to monitor the active clients on the router (my TV is in static IP) and if the IP of my TV is found, the router send over the lan a WOL for my home server.
After i have a software in the server that monitor the traffic and the clients to prevent him from hybernating, so my problem is "only" to find a way to send this packet automatically....
I'm a real newbee and don't know anything about scripting, so all help is welcome!
Thanks
remember to use crontab to modify cron jobs, so they are injected into the cron daemon and done without a reboot/restart of cron.
The dirty core of a script to check if an host is ping-able could be
Code:
ping -c 3 -w 3 192.168.3.12 | wc -l
this is to be put into an if statement that checks how many lines are there....
Try to run it in your network conditions with an unreachable host and see you are answered with a number that should be much less then the one given when using it with an active host (here 4 vs 8 for example).
Then your script could simply be
Code:
if [ "`ping -c 3 -w 3 192.168.x.y | wc -l`" -gt 6]
then
/usr/sbin/wol -i 192.168.x.255 -p PP AA:BB:CC:DD:EE:FF
fi
Where be warned that 192.168.x.255 assumes a 255.255.255.0 LAN subnet mask, if not change it accordingly.
Should be almost done
Thanks for your help, i'll go to wiki trying to understand a little about CRON and scripting, i'm really not at ease with that.
My first question is about traffic, is there a problem doing a ping every minutes?
Is it not more easy to control if the ip is client of the router?
Also it's a good idea, and my TV answer to ping, so it'll be probably works.
About Cron,how i set the path? In wiki i saw differents path with differents times to execute?
Code:
*/1 * * * * script_path
And the start code of the script is
ping -c 3 -w 3 192.168.x.y | wc -l
And after i really don't understand anything
(Try to run it in your network conditions with an unreachable host and see you are answered with a number that should be much less then the one given when using it with an active host (here 4 vs 8 for example).
Then your script could simply be)
Code:
if [ "`ping -c 3 -w 3 192.168.x.y | wc -l`" -gt 6]
then
/usr/sbin/wol -i 192.168.x.255 -p PP AA:BB:CC:DD:EE:FF
fi
Hey, I've tried to give you hints, now you have to do your researches, I can't teach you bash scripting here, also because I'm not able to and what I say comes from web-learning you can do yourself, too. I tried just giving hints to make your work really a lot easier. Now you have to use google if you didn't get something, this is known to be "bash scripting". See how "if" statements work. See how cron works on the web and on the wiki. They're all well-documented for sure.
The whole thing is anyway almost done, your final script should look like:
Code:
if [ "`ping -c 3 -w 3 192.168.x.y | wc -l`" -gt 6]
then
/usr/sbin/wol -i 192.168.x.255 -p PP AA:BB:CC:DD:EE:FF
fi
ONLY.
As I said
ErMeglio wrote:
Code:
ping -c 3 -w 3 192.168.3.12 | wc -l
this is to be put into an if statement that checks how many lines are there....
so, not at the beginning of a script, it's useless.
This is done in the if/then/fi code I wrote after. Look that I wrote it by hand and didn't test it, it could be erroneous.
The part where I say:
ErMeglio wrote:
Try to run it in your network conditions with an unreachable host and see you are answered with a number that should be much less then the one given when using it with an active host (here 4 vs 8 for example).
means you have to try to launch from telnet/ssh the ping commands to your active tv's ip AND to any inactive ip on YOUR network and check your numbers.
This means you have to launch ping -c 3 -w 3 192.168.x.y | wc -l where x.y is a known host and check the number it answers with (8 for me).
Then you check the same with x.y being an unreachable host in your network. My number is 4 here.
Your numbers MAY vary for a number of reasons, that's why you should do it.
What I've put in the script is the mean between 8 and 4 = 6. That script means, when this number is more than 6 (ie the tv has its active ip) then wake on lan the target pc. End of the if (fi).
I struggled with the script above , it looks like the return value from the ping command is in string format or something ,on my system it returns [4] or [8]
So the compare fails.
I am just learning bash but here is my solution :-
Code:
#!/bin/sh
ping -c 1 -t 1 192.168.1.50;
if [ $? -eq 0 ]; then
echo "PS3 is active"
/usr/sbin/wol -i 192.168.1.255 -p 7 xx:xx:xx:xx:xx:xx ;
else
echo "PS3 is down" ;
fi
So once my PS3 powers on , this script wakes my media server
I just need to get cron running now which will check ping my PS3 every minute
Thanks for all the earlier suggestions , it has pointed me in the right direction.