Total Pageviews

Wednesday, 15 November 2017

Wake and suspend machines on local Ethernet LAN

Suppose you have a spare PC (say A) that supports Wake-on-LAN, you can first enable it by following this guide. Then you can hack on some scripts on other local machines connected through Ethernet to remotely control A to make it suspend/hibernate and wake up as you want.

Wake

To wake up A is simple. Suppose A’s MAC address is 11:aa:22:bb:33:cc and the hostname is “machine-a”, and you want to wake it up from other machines (e.g. Machine B) on LAN, first install the wakeonlan utility (on Machine B).
On Debian/Ubuntu:
$ sudo apt-get install etherwake
or:
$ sudo apt-get install wakeonlan
On Fedora/RHEL:
$ sudo apt-get install net-tools
On OS X:
$ brew install wakeonlan
To send magic Wake-on-LAN packets to A:
$ etherwake 11:aa:22:bb:33:cc
or:
$ ether-wake 11:aa:22:bb:33:cc
or:
$ wakeonlan 11:aa:22:bb:33:cc

Suspend/hibernate

  1. Configure static IP for Machine A, for example 192.168.0.201.
  2. Edit /etc/hosts on Machine B by adding the following line:
    192.168.0.201   machine-a
    
  3. Setup SSH server on Machine A.
  4. Create a user account (say “yourname”) on Machine A for SSH login
  5. Configure public and private keys for SSH login without password
  6. Install pm-utils on Machine A.
  7. Edit /etc/sudoers on Machine A to add the following line:
    yourname ALL=(root)      PASSWD:ALL, NOPASSWD: /usr/sbin/pm-suspend
    
  1. Install GNU Screen on A.
Then, you can use the following script suspend_machine_a.sh on Machine B to remote suspend Machine A:
#!/bin/bash


session_name="suspend"
ssh yourname@machine-a "if screen -list | grep -E '\<[0-9]+\.$session_name\>' >/dev/null;
then echo -n ''; else screen -dmS $session_name; fi"
ssh -t yourname@machine-a "screen -S $session_name -p 0 -X stuff 'sudo pm-suspend
'
"
To use hibernate, substitute pm-hibernate for pm-suspend.
Finally, you can make some shortcut aliases so that suspend/wake of Machine A can be done by a single command.
--------------------

Synchronize sleep and wakeup of machines on Ethernet LAN from Mac using SleepWatcher

The above post discusses remote suspend (sleep) and wakeup of machines on Ethernet LAN using pm-utils, GNU Screen and Wake-on-LAN (LAN). This post will discuss the synchronization of such behaviors of local and remote machines.
Suppose you are regularly working at home on a Mac (either MacBook or Mac mini or something else), and you have a spare PC running as an alternative to NAS or workstation on the same Ethernet LAN. If the spare PC is Linux, you may have set up netatalk on it to enable AFP file sharing or use it as a Time Capsule. However, since it is probably a netbook and may work only when your Mac is up, you don’t want it to run all days without doing anything. You can thus synchronize sleep and wakeup of your Mac with that PC.
You can download and install SleepWatcher to monitor sleep and wakeup on Mac and write some scripts upon these events.
  1. Download SleepWatcher (version 2.2 as of writing)
  2. Install it following the ReadMe.rtf instructions
  3. After the SleepWatcher LaunchAgent or LaunchDaemon is loaded, run the following commands to add sleep and wakeup scripts:
    $ echo "/path/to/your/sleep_script.sh" > ~/.sleep
    $ echo "sleep 5; /path/to/your/wakeup_script.sh" > ~/.wakeup
    $ chmod +x ~/.sleep ~/.wakeup
    
/path/to/your/sleep_script.sh and /path/to/your/wakeup_script.share scripts to suspend and wakeup the remote machine (reference the previous post). sleep 5 performs a delay to make sure the network is up to avoid “send : No route to host” error. 4. Test the setup by sleeping and waking up your Mac and check whether the remote machine does the same.

No comments:

Post a Comment