Ubiquiti mPower Automation

So it’s been a while since my last (and first) post, been trying to wait for something worthwhile to write about.  So here it goes…

I’ve been having some issues with devices requiring power cycling.  Home automation/security devices, wireless APs (access points), raspberry Pi, etc.  In the past I used a X10 controller using a program called “heyu”.  It worked well for what it did, but every device had to have a dedicated outlet and was very clunky. After doing some research I found  this. I already have a ubiquiti edge router lite and have been pretty pleased with it, so I decided to pull the trigger to buy it.  They make a smaller 3 port device that is wifi only, but I wanted a hardwired version (especially since I’m going to be resetting access points). I read that you needed to install the mFi Controller software, so I ended up getting it running (had to change some ports, install (deprecated) JDK7, create a systemd startup script, etc.) and it turns out I didn’t really even need it, so not sure if it’s worth it or not.  Anyone feel free to comment on this.

I read on a forum, here, that shows how to use SSH to control the mPower device.  In their example they used expect to log in, which is OK, but I prefer to use SSH keys whenever possible.  Since this runs on busybox, I wasn’t sure if it would work, but I thought I would give it a try.

mkdir ~/.ssh
chmod 0700 ~/.ssh
vi ~/.ssh/authorized_keys
chmod 0600 ~/.ssh/authorized_keys

In the “vi” command above I put my ssh public key.  This is usually located /Users/steve/.ssh/id_rsa.pub (Mac) or /home/steve/.ssh/id_rsa.pub. You just can “cat” the file then copy and paste the output into the authorized_keys file.

If you don’t have one you can create one with something similar to:

ssh-keygen -t rsa

For this to be passwordless and automated, you just hit enter for the password (blank password) or you will have to enter it everytime you SSH.

Also, I had to change the algorithm to SSH to allow me to log in.  You can do this everytime you SSH…. or just put it in your ~/.ssh/config:

Host <Your mPower Device IP>
    User ubnt
    KexAlgorithms +diffie-hellman-group1-sha1

Now, you should just be able to “ssh <IP of mPower Device>”.  It should put in your user and your KexAlgorithm automatically. Voila! Passwordless SSH.

From the forum post I mentioned above, it mentions the /proc/power/relay${num} method of switching on and off the specific port.  I thought this was great, but I wanted to give a little more meaning to the names of the ports and not have to remember their location. So I created some symlinks:

ln -s /proc/power/relay8 ~/modem
ln -s /proc/power/relay3 ~/accesspoint
ln -s /proc/power/relay1 ~/homeautomation

The number in the relay${num} correlates to the port on the mPower power strip. You can plug them into any port you want, just make sure you document it (and maybe label the port).

0 = Off, 1 = On

You just echo 0 or 1 to your newly created symlink.  You can do this without logging in like:

ssh <IP Address of mPower Device> "echo 0 > ~/modem"

To turn off the modem for example, this can easily be scripted with a ping check, or some kind of monitoring tool like Zabbix, Nagios, Zenoss, OpManager, etc. etc. If you create any scripts I’d love to see what you’re doing with it!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.