Posted: Thu Dec 24, 2009 6:40 Post subject: Trying to create startup script that will trigger WOL...
Edit: Forgot to mention... using WRT54G 1.1 with dd-wrt.v24-13064_VINT_openvpn_jffs_small.bin
Okay, my goal: I'm trying to modify a startup script that someone else made which doesn't quite suit my need. My goal is to have DD-WRT listen for a broadcast from UDP 3776 and trigger a WOL when it hears it. UDP 3776 is what a media center extender broadcasts when it is turned on and attempts to find a media center PC. The media center PC is often in S3 standby. Currently I have a small PC app that runs on my desktop which accomplishes this. My goal is to have DD-WRT do it, so that I can also start using S3 on my desktop computer.
This is a script someone made that I have tried. IPs are all static. VMC is the media center, DMA1 is the extender. I removed the real MAC address here.
if [ ${DMA1Last:-1} -eq 1 -a $DMA1Now -eq 0 ] ; then #DMA1 started
WakeVMC
fi
DMA1Last=$DMA1Now
done
This script ONLY works if I do a cold reboot on the extender by turning the switch in the back off and back on. I believe this is because the script checks for a powered on state, and when my extender is in standby, it still responds to PINGs. It was written for a different model extender which probably works for that model (DMA2200) but apparently not my older WMCE54AG.
What I'd like to do is change this script so it works like the PC app: instead of checking the poweron state, instead have it listen for a broadcast from UDP 3776 and send the magic packet when it hears it.
If I ever bothered to learn the first thing about scripting I could probably do this, but instead i'm asking the gurus here for help. Thanks in advance...
if [ ${DMA1Last:-1} -eq 1 -a $DMA1Now -eq 0 ] ; then #DMA1 started
WakeVMC
fi
DMA1Last=$DMA1Now
done
Shameless bump, sorry. I don't expect any answers over Xmas at least. Basically I'm looking to replace this code above with something that recognizes a packet from UDP 3776 and then calls WakeVMC.
Google any number of terms "linux scripting", "bash scripting", "shell scripting"...
One thing I noticed is that it checks the return status of a command with the $? variable, but $? and many other special variables seem to output more than they should.
Here's a somewhat simple modification that should look for anything destined to port 3776 regardless of protocol or IP. It uses the conntrack file so however long you have your udp/tcp timeouts set to will be how long it spams the WOL command when it sees a match.
while sleep 5 ; do
if [ `grep -c dport=3776 /proc/net/ip_conntrack` -gt 0 ] ; then
$WOL -i $BCAST -p 7 $MAC_VMC
fi
done
_________________ Read the forum announcements thoroughly! Be cautious if you're inexperienced.
Available for paid consulting. (Don't PM about complicated setups otherwise)
Looking for bricks and spare routers to expand my collection. (not interested in G spec models)
Google any number of terms "linux scripting", "bash scripting", "shell scripting"...
One thing I noticed is that it checks the return status of a command with the $? variable, but $? and many other special variables seem to output more than they should.
Here's a somewhat simple modification that should look for anything destined to port 3776 regardless of protocol or IP. It uses the conntrack file so however long you have your udp/tcp timeouts set to will be how long it spams the WOL command when it sees a match.
while sleep 5 ; do
if [ `grep -c dport=3776 /proc/net/ip_conntrack` -gt 0 ] ; then
$WOL -i $BCAST -p 7 $MAC_VMC
fi
done
I found a book called "Linux Command Line and Shell Scripting Bible". I'll try it.
Thanks for the idea. It works initially... however, the problem is that once it is triggered the first time by turning on the extender, it never stops sending the WOL packets even after the extender is turned off again so I cannot get the media center PC to go back to sleep and stay asleep.
Edit: So do I need to reduce both the UDP and TCP timeouts, or just the UDP? Is this the reason? They are at defaults of UDP=120 and TCP=3600. I'll play around with this.
Edit2: even with the timeouts dialed down to 5/5 and rebooting the router, the WOL packets never stop coming. The only way to stop this is to cold reboot the router. But once I turn the extender on (and then back off again) to trigger the script, it's the same as before. The WOL packets never seem to stop. I try to put the media center PC back to sleep and it wakes back up within 5 seconds.
If you're using a UDP packet then I would expect it to stop sending the magic packets after 2 minutes. For a TCP packet it could spam it for an hour though. I tested it and it does stop sending magic packets after the connection is forgotten by conntrack. These modifications will make it sleep for 10 minutes after sending a magic packet and it will only match udp now.
while sleep 5 ; do
if [ `egrep -c udp.+dport=3776 /proc/net/ip_conntrack` -gt 0 ] ; then
$WOL -i $BCAST -p 7 $MAC_VMC
sleep 600
fi
done
_________________ Read the forum announcements thoroughly! Be cautious if you're inexperienced.
Available for paid consulting. (Don't PM about complicated setups otherwise)
Looking for bricks and spare routers to expand my collection. (not interested in G spec models)
If you're using a UDP packet then I would expect it to stop sending the magic packets after 2 minutes. For a TCP packet it could spam it for an hour though. I tested it and it does stop sending magic packets after the connection is forgotten by conntrack. These modifications will make it sleep for 10 minutes after sending a magic packet and it will only match udp now.
while sleep 5 ; do
if [ `egrep -c udp.+dport=3776 /proc/net/ip_conntrack` -gt 0 ] ; then
$WOL -i $BCAST -p 7 $MAC_VMC
sleep 600
fi
done
Hey thanks, this appears to work! It's definitely UDP 3776 and not TCP, I know that for sure. The media center box now appears only to wake up the one time instead of over and over.
The only catch now is it appears to also wake my desktop because I'm trying to have pattern match waking enabled on it. I'll just change it to magic packet only and then find a way to have my media center box forward a magic packet. Thanks again, merry Xmas.
I made a mod. I wanted it to also throw a WOL packet at my desktop which is defined as MAC_DT. This is valid, yes? I also reduced that sleeping time to 5 minutes instead of 10.
I realize that this is an old thread, but I was interested in the same thing... to send a WOL packet to my Windows 7 Media Center PC when one of my extenders tried to connect. Only the XBox 360 extender will send a WOL. The others (HP, Linksys, and D-Link) will not. However, they are quieter, generate less heat, use less power, cost less, and are more reliable than the XBox 360.
So... I wanted to post my approach to this problem.
My router is a Linksys WRT-54GL. I am using Win7 MC with HP X280N and Linksys DMA-2200 extenders.
I have to say that the dd-wrt firmware blows away the factory Linksys firmware! There are so many more configuration options in dd-wrt!
Well, on to my solution:
I configured each of my extenders to have a static IP in the Services -> Services (Static Leases) section. This makes sure that each extender ALWAYS gets the same IP address (without the need for configuring static IP's in each extender's settings menu).
I currently have three extenders, two HP X280N's and one Linksys DMA-2200. I made them 192.168.1.101, .102, and .103. I plan to add a fourth one soon. It will be .104.
Then, I wrote the following script and put it in the Administration -> Commands (Command Shell)... and then saved it with the "Save Startup" button.
The gpio commands may differ for you based on your router model. For mine, gpio 2 (white) and 3 (yellow) controls the LED behind the "EZ-Setup" button (which I never use, so it makes a good indicator that my script is working).
Here's the script:
Code:
#! /bin/sh
while sleep 4 ; do #Wait 4 seconds, then loop.
gpio enable 3 #Turn off the yellow LED on the EZ Setup button to indicate that the loop is starting.
if [ `egrep -c udp.+src=192\.168\.1\.10[1-3].+dport=53 /proc/net/ip_conntrack` -gt 0 ] ; then #Look for UDP to port 53 (DNS lookup).
gpio disable 2 #Turn on the white LED on the EZ Setup button to indicate that we got a hit.
/usr/sbin/wol -i 192.168.1.255 -p 7 01:23:45:67:89:AB #Send WOL to the MCE machine.
cat /proc/net/ip_conntrack_flush >/dev/null #Flush the ip_conntrack table.
sleep 1 #Sleep for 1 second.
gpio enable 2 #Turn off the white LED. The WOL has been sent.
fi
sleep 1
gpio disable 3 #Turn the yellow LED back on. End of loop.
#Finish the loop with a redirect to throw away output and put this in the background.
done >/dev/null &
It turns out that the example in the previous posts from phuzi0n and Phreaker47 didn't work for me. The UDP packet sent to port 3776 was NOT a broadcast packet... so it doesn't get sent to the router because the extenders and the media center PC (in my case) are all connected to a gigabit switch that is then up-linked to the router. The switch doesn't send the UDP 3776 packets to the router, because the packet is destined for the media center PC (and switches are supposed to send traffic ONLY to the destination MAC address to which they were addressed, unless they are sent as a broadcast... or if the switch doesn't have that MAC address in its MAC table). The only traffic that goes through the router is traffic that is destined for the internet, or to another node that is connected directly to the router (and not to the gigabit switch).
My solution is simple. Basically, when one of the extenders gets turned on (or awakened from "standby" mode), they "phone home" to check for a newer version of firmware. This request is sent to a DNS server (port 53)... and this can be detected in the ip_conntrack file in the router because the DNS server is on the WAN port (the internet) in my case.
Here's a simple description of what my script above is doing for those who don't read UNIX/Linux scripts:
1) it sleeps 4 seconds. The "sleep" command always returns zero (true), so the "while" loop is always true.
2) It turns off the yellow/amber LED behind the EZ-setup button (gpio enable 3)
3) It uses egrep to check the ip_conntrack file for a matching string. It is looking for a string that contains "udp" followed by "src=192.168.1.101" (or .102 or .103) followed by "dport=53". (The part that looks like "[1-3]" means to search for the range including numbers 1 through 3. The back-slashes before the periods mean to treat the periods as literal strings. It essentially means to look for a string that looks like "udp*src=192.168.1.10", followed by the numbers 1 thru 3, followed by "*dport=53"). This indicates that one of the extenders tried to "phone home" for a firmware upgrade. If the string is found, the egrep returns the number of lines found. The "-gt 0" checks to see if the number of lines found is greater than zero. If so, the "if/fi" block gets executed.
4) If the "if" is false, skip to step 10 below.
5) We turn on the white LED to indicate that the DNS request was found (gpio disable 2),
6) then send a WOL to my Media Center PC. The MAC address in the code above is fake. Use yours instead. Also, change the IP address to your subnet (192.168.x), but leave the ".255" in the last octet because you'll want to broadcast the WOL packet to the entire subnet... only the machine with the specified MAC will wake.
7) Now, we want to empty the ip_conntrack file so we don't find the same DNS request again. Simply displaying the contents of the ip_conntrack_flush file will cause the ip_conntrack file to be emptied. The ip_conntrack_flush file is always empty, but the firmware on the router automatically empties the ip_conntrack file when something accesses the ip_conntrack_flush file. So, we just display the contents of ip_conntrack_flush and throw it away. This prevents false detection of another DNS request the next time the while/do loop executes.
8) Sleep for 1 second so the LED stays white for a while.
9) Turn off the white LED.
10) Sleep for 1 second so the LED stays off for a while.
11) Turn the yellow LED back on to indicate end of loop (gpio disable 3).
12) End the loop. Throw away the output to /dev/null and use the ampersand to indicate that the process should run in the background.
The result of this script is that you will see the LED flash yellow for 4 seconds, then off for 1 second. It will repeat this pattern until you turn on one of your extenders. The extender sends a DNS request to the internet to find newer firmware. Once this DNS request is detected in the ip_conntrack file, the LED turns white, the file is emptied and a Wake-On-LAN (WOL) is sent to the Windows Media Center PC. One second later, the LED turns off for 1 second, and then the cycle begins again. The LED will blink yellow/off all the time... until a DNS request originates from one of your extenders.
Posted: Fri Jul 26, 2013 6:09 Post subject: Trying to create startup script that will trigger WOL...
Hi,
I am trying to do same thing with DD-WRT, i have Firmware DD-WRT v24-sp2 (05/27/13) std. I copied and pasted the same script, changed the IP addresses and put in the correct MAC address and saved as a startup, rebooted but nothing.
I tested both UDP ports 3776 and 53, nothing worked. I even used other ports to set the scripts to look for those ports but nothing!
It seems that the condition (if [ `egrep -c udp.+src=192\.168\.1\.10[1-3].+dport=53 /proc/net/ip_conntrack` -gt 0 ]) is simply never met to triger a wol to the appropriate mac address.
First, you need to understand that the search string for egrep (the part where you put your IP addresses) contains a bunch of wild cards. Google "regular expressions" for more info.
The "dport=53" part is telling it to look for DNS queries. When you first turn on the Linksys and HP extenders, they do a DNS lookup so they can check for firmware updates.
If you have manually changed the DNS settings in your extender's menu so that it never sends the DNS query, then your router will never be able to detect that the extender has been turned on.
Like I said in my post, you will probably want to remove or change the gpio commands, because they vary from one router to another.
Posted: Fri Jul 26, 2013 20:06 Post subject: Trying to create startup script that will trigger WOL...
Thanks for responding, I forgot to mention that I did a factory reset on all the extenders, so I beleive by default they all look for new firmware (will check when I get home). In addition, I already took out all the lines for GPIO because I have a different router (Linksys e3000).
Here is the new script:
Code:
#! /bin/sh
while sleep 4 ; do
if [ `egrep -c udp.+src=192\.168\.2\.15[1-3].+dport=53 /proc/net/ip_conntrack` -gt 0 ] ; then
/usr/sbin/wol -i 192.168.2.255 -p 7 6C:F0:49:73:57:B3
cat /proc/net/ip_conntrack_flush >/dev/null
sleep 1
fi
done >/dev/null &
I also set static IP addresses for all the extenders as you mentioned in your post. In addition, I tried different variations by replacing egrep it with grep and just looking for a single specific port like 20 & 21 (excluding UDP and all), then I initiated a FTP session which has to go through the router and, nothing!
I tested WOL using a single line command(/usr/sbin/wol -i 192.168.2.255 -p 7 6C:F0:49:73:57:B3)and that works, so I know all my Bios settings are fine, for some reason grep or egrep is not working on the conntrack!
Do you still have this setup? There may be something different with new versions of DD-WRT, your script should work just fine, I just don't get why its not!
I hate to go out and buy 3 XBOX-360s just to solve this problem because the server is a power hungry core i7, I can't leave it on 24x7.
You should check to see if the ip_conntrack file is in the same path as what I have in my script. Your ip_conntrack file could be located in a different path.
Same deal for the ip_conntrack_flush file.
Also, make sure you have an egrep command in your version of DD-WRT. Try logging in via telnet and doing the command manually. Does it give you an error?
While you're logged in via telnet, try turning on one of your extenders, and then just "cat" the ip_conntrack file to see if you can find a line that shows the IP address of the extender.