Turn on / off Linux servers with IFTTT button

Rob

Administrator
Staff member
IFTTT stands for "If This Then That" and currently allows me to power on and off my Linux servers. Below i'll lay out exactly how I accomplished this.

Requirements
  • Linux server with out-of-band (ilo, idrac)
  • Always-on Linux server (I'm using an odroid c2)
  • Dropbox account
  • IFTTT
  • Optional: Ability to create IFTTT widget buttons on your phone
Our setup
  • L1: Linux Server we want to control
  • L2: L1's out-of-band connection
  • L3: Always-on linux server
Set up the control scripts
SSH to L3, cd to /usr/local/bin/ and create two scripts; one to turn on L1, one to turn off L1

Turning on L1
Create a file called L1-on.exp and paste in one of the two expect scripts below to control power-on depending on whether you have a Dell with iDrac or HP with iLo.

(Dell with iDrac)
Code:
#!/usr/bin/expect -f
# Replace [email protected] and IDRAC-PASSWORD-HERE with your settings
spawn ssh [email protected]
expect "password: "
send "IDRAC-PASSWORD-HERE\r"
expect "admin1-> "
send "racadm serveraction powerup\r"
sleep 2
expect "admin1-> "
send "exit\r"
(HP with iLo)
Code:
#!/usr/bin/expect -f
# Replace [email protected] and ILO-PASSWORD-HERE with your settings
spawn ssh [email protected]
expect "password: "
send "ILO-PASSWORD-HERE\r"
expect "</>hpiLO-> "
send "power on\r"
sleep 2
expect "iLO-> "
send "exit\r"
Turning off L1 using SSH
(note: you will need to have ssh keys set up from L3 to L1, or use an expect script to enter in the password, etc..)

Code:
#!/bin/bash
ssh [email protected] "shutdown -h now"
Download / set up the dropbox CLI tool
Follow instructions to set up the dropbox_uploader.sh tool here:
https://github.com/andreafabrizi/Dropbox-Uploader
(I keep dropbox_uploader.sh in /usr/local/bin/)

Set up ifttt
Create two applets. Each one will create a text file on dropbox.
To turn on server
Create applet to create a file called l1up.txt with any/no content and note the Dropbox path. My path is Google/ifttt/

To turn off server
Create applet to create a file called l1off.txt with any/no content and note the Dropbox path. My path is Google/ifttt/

optional widget buttons
Create widget buttons on your phone to easily power on/off your servers from the applets you created. I'm not sure if iOS can do this or not but know Android can.

Call one L1-up and the other L1-off.

Create and set up the dropbox monitoring script
(Back on L3 again.. ) We need to create a script in /usr/local/bin/ which I call monitor-ifttt.sh. Here are the contents. You will want to change your hostnames. This file grabs files from the Dropbox: Google/ifttt/ directory. It will also log startups / shutdowns in /tmp/L1.log.

Code:
#!/bin/bash

# Check your paths here
/usr/local/bin/dropbox_uploader.sh list Google/ifttt > /tmp/ifttt

File="/tmp/ifttt"

# Monitor to turn L1 on - check your paths here
if grep -q l1up "$File"; then
   /usr/local/bin/dropbox_uploader.sh delete Google/ifttt/l1up.txt && /bin/rm /tmp/ifttt && echo "Time: $(date) started L1" >> /tmp/L1.log && /usr/local/bin/l1-on.exp
fi

# Monitor to turn L1 off - check your paths here
if grep -q l1off "$File"; then
   /usr/local/bin/dropbox_uploader.sh delete Google/ifttt/l1off.txt && /bin/rm /tmp/ifttt && echo "Time: $(date) shutdown L1" >> /tmp/l1.log && /usr/local/bin/l1-off.sh
fi
Make it executable:

Code:
chmod +x /usr/local/bin/monitor-ifttt.sh
Set it up in cron to run every minute:

Code:
* * * * * /usr/local/bin/monitor-ifttt.sh
Put it all together
By clicking your L1-up IFTTT shortcut/button you create the l1up.txt text file in Dropbox. The above script running once/minute will check Dropbox, see it, delete it, log it, and run the script to turn on L1 (by running the expect script into the out-of-band connection).

  1. Push your IFTTT L1-on button
  2. File is created in Dropbox
  3. Script checks for file, deletes it, logs it, starts script to power on L1 through L2
Did it work? Post a comment if you're having trouble.
 


Latest posts

Top