Tag Archives: Wake On LAN

Wake on LAN for PC’s via Raspberry Pi

I found a nice PHP script for Wake on LAN. I loaded it on the Raspberry Pi that I have and configured it for my system. The Raspberry Pi runs 24/7, so I can just navigate to a web page that it serves, hit a button and start up one of my machines at home from anywhere. Mostly this is useful for the starting my Linux file server remotely but I do use it to fire off the desktop too.

Right from the read me file for the code…

REMOTE WAKE/SLEEP-ON-LAN SERVER
=========================
This is simple webapp that runs on your Raspberry Pi to turn it into a remotely accessible Wake/Sleep-On-LAN Server. [Follow the detailed tutorial](http://www.jeremyblum.com/2013/07/14/rpi-wol-server/) on my website for instructions on how to get this working, and forwarded through a router. This is very useful when you have high-powered machine that you don’t want to keep on all the time, but that you want to keep remotely accessible for Remote Desktop, SSH, FTP, etc.

http://www.jeremyblum.com/2013/07/14/rpi-wol-server/

Results

It is rare when something works right out of the box. But, this did, I followed Jeremy Blum’s instructions and within a few minutes I had this working. It has a nice drop-down menu where you can select a computer. It pings it to see if it is awake, then you can wake it from anywhere in the world. Once the WOL packet is sent, the application keeps pinging the PC at a defined interval and you can see when it wakes. I have not tried the sleep functionality as I am using it with Linux PC’s and his outline covers Windows machines. I am sure the code could me modified to shut down a Linux PC somehow. Perhaps it automatically SSH’s in and sends a shutdown command, something like that. I have my Linux server set to shutdown automatically so I don’t need this functionality myself.

It is configurable through an easily understood config.php file as well. You can set the computers name and IP address, MAC address, timing between pings, amount of times to ping the machine and etc.

Also see on this site…

Original Wake on LAN via Ubuntu Linux Post

Windows Wake on LAN Post

Wake on LAN via Windows

Windows

To wake a machine from a Windows computer there are a few choices.

wolcmd

wolcmd for the command line from Depicus.com is good to use in scripts or by itself. It works 100% of the time for me.

 wolcmd yourmacaddr localserveripaddr 0.0.0.0 9

wolcmd can start the Linux server using wolcmd <-download page, from Depicus.com

Wake on LAN GUI

A GUI version of the wolcmd tool from Depicus.com WakeOnLanGui

WOL Magic Packet Sender Tool

WOL Magic Packet Sender, uses a WOL Setup MSI file.  I have used this quite a bit and it does work nicely. It is the first one that I used and have it on my Windows machines.

Online

At Depicus.com you can wake your machine directly from the Internet as well without loading any application via this page –> http://www.depicus.com/wake-on-lan/woli.aspx

There is even a way with the Depicus site to make up a URL that will have the MAC address, IP address and Port as parameters to send a magic packet. I’ve tried it and it works.

 

 

 

 

Auto-shutdown and Auto-suspending a Linux Server

The article below talks about auto-suspending…

http://rolandtapken.de/blog/2013-07/suspend-nas-when-idle

A Simpler Idea

I found another article on auto-suspending that requires only a simple bash script that I have placed in /etc/cron.hourly.

WordPress did not like me uploading autosuspend.sh, for security reasons, it will give an error, so I have the script autosuspend.sh , named autosuspend.sh.txt  here->  autosuspend.sh . The file goes in /etc/cron.hourly naming it just autosuspend, cron won’t run if the filename has an extension.

The file must be owned by root and executable. So you have to use the following commands before running it.

sudo chown root:root autosuspend.sh
sudo chmod u+x autosuspend.sh

I used it as autosuspend.sh and ran it a few times manually running sudo autosuspend.sh, just to see it run properly before sticking the file renamed as autosuspend and placed it into /etc/cron.hourly.

And the autosuspend.conf  named as autosuspend.conf.txt here-> autosuspend.conf  goes in the /etc/ directory.

Both are UNIX formatted files, modify them accordingly for your use.

syslog

CRON logs things when it runs autosuspend into /var/log/syslog, so you can execute…

tail /var/log/syslog

…to see if everything is OK by seeing the traces, the autosuspend script  gives good useful error messages. It also will send an email on the server to root@yourservername, every time it runs. You can use mailx from the CLI ( or some other program ) to read the local email. Mailx is very simple and good enough to quickly page through CRON emails, using return to move down through the unread ones.

Resources

The article I got the script from is…

https://bbs.archlinux.org/viewtopic.php?id=157268

…it does not give you the autosuspend.conf file, but it references another article in German…

http://wiki.ubuntuusers.de/Skripte/AutoSuspend

…that does have the autosuspend.conf file and it seems to work, at least it runs fine so far with some mods.

Files

Once again below are the script and conf file from those sites, labeled with a txt extension. I put them here in case those sites disappear for some reason. This is good knowledge and it works so well, I’d hate to see it get lost.

The script taken from the Archlinux page, requires systemd and uses systemctl suspend to suspend the machine, named autosuspend.sh.txt. Formatted for UNIX/Linux.

autosuspend.sh

Original autosuspend.sh that uses pm-utils from the German ubuntuusers.de site, named as pm-utils_autosuspend.sh.txt and the autosuspend.conf named as autosuspend.conf.txt. Formatted for UNIX/Linux

pm-utils_autosuspend.sh
autosuspend.conf

Auto Shutdown – Mods

I decided to modify the autosuspend.sh file rather than loading the package that it needed (systemd) to execute systemctl suspend, which is what the script file from the first article uses. The other option would be to use pm-utils as the second German article has the original autosuspend.sh formatted to use. For more info on pm-utils see https://wiki.archlinux.org/index.php/pm-utils

Instead of auto suspending, I decided that since the server starts fast enough from a cold boot (17 secs. to usable), why not just replace the…

systemctl suspend

…line with…

shutdown -P +5

This will shut the server down, with a 5 minute warning and guard band. I say guard band, because it can guard against a potential loop. If I play with the script more and make a mistake, I do not want to wind up with a server that starts, jumps to the script and starts shutting down immediately. I know I put the file in /etc/cron.hourly, so it will kick off every hour, but I am just guarding against unforeseen things to be safe and it’s only 5 minutes of delay. If it goes to shut down while testing at some point, I have 5 minutes to execute a shutdown -c to cancel.

I also put the line…

ethtool eth0 -s wol g

…before the shut down line, because that same piece of code, which I tried put into rules.d. But it was not setting the wake on to g, When I ran ethtool, it was staying at d. Not sure why, but since I will be allowing this server to shutdown by itself 90%+ of the time, I opted to put it right in the shutdown script. After a second thought, I also put that line into the /etc/rc.local ( which runs at start up ) as well so it is armed even if I shutdown manually! See the post of Wake On Lan via Ubuntu Linux for more info on Wake on LAN.

Here is the modded autosuspend called autoshutdown.txt. Remove the txt extension and place into the /etc/cron.hourly folder, it is formatted for UNIX.

I forgot about the UNIX and DOS endlines being different while I was working on this. See my post on UNIX vs DOS file endlines, as I had a bit of brain fog and struggled a bit with this while working on the autoshutdown script.

Winbind

Once I got the autoshutdown running. I realized that the Linux machine was not able to resolve the names the Windows machines on the network. The server could only ping the Windows machines by IP address and not their names! I saw this when I was logged out of the server and logged in a while later and the shutdown script had recorded failed pings into syslog, when checking to see if the server was idle. The script correctly saw that no one is logged it by executing, who | wc -l yielding a zero and next it was testing for attached clients ( the Windows machines named in the autosuspend.conf file) using ping $i -c1. And ping was failing as the names were unreachable.

  • arp -a could see all the machines by IP address from both Linux and Windows.
  • net view on the Windows machine could see all the machines by name.
  • smbstatus can see every computer by name fine from my Linux server machine. Particularly since I had installed Samba, the servers name is visible from Windows PC’s due to Samba.
    Samba must send out net-bios information about itself, I see in the config file for Samba where it can act as a wins server as well.

In order for the autosuspend/shutdown script to work pinging by name is a must. To fix, install winbind and configure /etc/nsswitch.conf.

sudo apt-get install winbind

In /etc/nsswitch.conf add wins to the end of the line that starts with
hosts: Mine now reads…

hosts: files dns wins

I got the info from…
http://www.serenux.com/2009/09/howto-configure-ubuntu-to-be-able-to-use-and-respond-to-netbios-hostname-queries-like-windows-does/

Samba Connected test in shutdown script

The autosuspend script does a test to see if anyone is accessing files using Samba via smbstatus. Smbstatus is great to see what is going on, it is good to troubleshoot Samba when you can make connections. It is interesting once you play with it when various computers are accessing the server to understand what it is telling you.
But the script is just looking to see if computers are accessing Samba
shares. The autosuspend.conf shows an IP address for the test using
$SAMBANETWORK as that value and grepping on it. I am not sure how this works as I don’t see any IP numbers when I run smbstatus. So for now I decided that I will use the word Public in the autosuspend.conf instead of 192.168.1. Most likely if a computer is accessing Samba shares on my network and the computers name is not one of the “clients” ( my own machines at home, that have listed names) it is going to be only accessing the Public Samba share. For now this seems to work!

Test used in autosuspend script to look for machines accessing Samba…

/usr/bin/smbstatus | grep $SAMBANETWORK | wc -l
Other conditions for shutdown

The other two tests that autosuspend does (IsRunning() and
IsDaemonActive() ), I have not validated them.

That is a TBD. So far, so good, the server has not shutdown unexpectedly and I have not seen it held up by IsRunning() yet, based on it’s tests. If something is running and a shutdown occurs, a sigterm is generated as the system is going down, so anything in process should terminate cleanly in theory. I’d like to test for OwnCloud activity at some point, I have shut the machine down and restarted a few minutes later on purpose with an OwnCloud file transfer in progress and it picks back up. I have to figure out a test for this, TBD.

IsRunning() tests for the following applications…

 APPLICATIONS='"^nxagent$" "^rsnapshot$" "^wsus$" "^wget$" "^screen$" "^mlnetp$" "^apt-get$" "^aptitude$" "^dpkg$" "^cp$"'

The following features were not copied from the autosuspend.sh on the German site to the one on https://bbs.archlinux.org/viewtopic.php?id=157268 that I used…

# Turning suspend by day (8 a.m. to 3 a.m.) off
 DONT_SUSPEND_BY_DAY='no'
# Automatically reboot once a week when the system isn't in use
 REBOOT_ONCE_PER_WEEK='yes'
  • DONT_SUSPEND_BY_DAY seems to control suspending by blocking it out during the day between 8AM and 3PM, it uses /sys/class/rtc/rtc0/wakealarm. I wasn’t interested in this so I was fine with it being carved out.
  • REBOOT_ONCE_PER_WEEK uses cat /proc/uptime | cut -d’ ‘ -f1-1\` / 3600 / 24 )>= 7\ as a test to see if the machine has been running for more that one week and then it reboots the next time it is idle. This is not of interest to me as my machine will shutdown rather than suspend, so this is not needed either.

Interestingly, I do see a test to see if power management is supported in the original autosuspend.sh that relies on pm-utils. This does not exist in the modified script that uses systemctl, perhaps it is not neccessary as calling systemctl is fine without or it was omitted, because such a test does not exist when using systemctl.

  /usr/bin/pm-is-supported

Basically I am fine with the simpler script, if I need to add features back in, so be it!

I have been using the shutdown script for over a month with no issues so far.

Follow Up

I have been using this code on two servers, one for almost three years and one for a year. The older one does not suspend and it requires a shutdown and the newer one suspends nicely via systemctl suspend.

I decided to modify the code a bit to allow a hybrid-sleep and also allow for restarts when the system requires them. Read more about this here….

Autoshutdown Code Modded to hybrid-sleep and allow required restarts

 

Wake On LAN via Ubuntu Linux

Wake on LAN (WOL), works great, sometimes it is a bit tricky to configure. With Linux, the client that sends the magic packet requires etherwake and the unit to be waked requires ethtool to be installed. Ethtool configures the OS to actually boot on the Network Interface Card NIC’s command. The NIC has to support Wake on LAN and the BIOS has to be set up to use it.

Installing etherwake…

sudo aptitude install etherwake

Installing this package actually installs both etherwake and wakeonlan. To wake the computer….

wakeonlan MAC-Address-Here

or

etherwake MAC-Address-Here

MAC Address

MAC Address, aka Physical Address of a Linux machines NIC can be found by executing ifconfig. In Windows, you can use ipconfig /all.

 

Ethtool

Install…

sudo apt-get install ethtool

Use for eth0, enabling WOL…

sudo ethtool -s eth0 wol g

To check if the system is armed for WOL, run…

sudo ethtool eth0

Towards the bottom you should see

Supports wake on: g
Wake on : g

On my PC, I noticed that once it is waked. It is necessary to run…

sudo ethtool -s eth0 wol g

To re-arm the WOL, every time it is remote waked.

I will write a bash script to shutdown the server. It will execute…

sudo ethtool -s eth0 wol g
sudo shutdown -P now

Ensuring that when I power the server down remotely, it will be armed for WOL.

Better than that I found out by looking online while researching how to auto-suspend the server, this little piece…

Create “/etc/udev/rules.d/50-wol.rules” with the following contents:

ACTION=="add", SUBSYSTEM=="net", KERNEL=="eth*", RUN+="/usr/bin/ethtool -s %k wol g"

Check the syslog after creating it, errors will appear there if you spelled something wrong or whatnot.

tail /var/log/syslog

but as of now, no errors, but not setting wol g either. Still trouble shooting.

I found this piece at …

http://rolandtapken.de/blog/2013-07/suspend-nas-when-idle

The article talks about auto-suspending. But I found another article on auto-suspending that requires only a simple bash script that I have placed in /etc/cron.hourly but the article

https://bbs.archlinux.org/viewtopic.php?id=157268

does not give you the conf file, but it references another article in German

http://wiki.ubuntuusers.de/Skripte/AutoSuspend

that does have a conf file and it seems to work, at least it runs. I have yet to see if it actually will auto-suspend! I am working on it right now and will post an entire write up when the bugs are worked out.

 

Remote vs Local

The first try most likely be on your own LAN. To do it for real remotely requires setting up your router to forward ports. I had to set it to forward port 7 and 9 to the outside world. Then it is a matter of sending a magic packet to the ISP address that your router reports of a FQDN ( Fully Qualified Domain Name), remotely I had to have the bit mask set to 255.255.255.255 when using a utility on a website. Locally I noticed 0.0.0.0 ( broadcast ) worked.

 

I read a bit about WOL here before attempting to set it up.