121: what I read next made me so mad that I headbutt myself in the face

Latest Entry:

Run Scripts by Email or SMS

Posted by Tom on the 25th of January, 2012, at 7:15 am.

  1. Put a few prepared script or executables in /home/tsbertalan/bin/emailclibin/ and chmod +x them.
  2. Make a new gmail account with cli_inbox and cli_archive IMAP folders, and a new filter that puts messages from your approved senders (say, number@vtext.com, for Verizon phones) in that cli_inbox folder.
  3. Run python emailcli.py with nohup or screen or something.
  4. Text the name of one of your prepared scripts to EMAIL@gmail.com

I can already see certain, possibly quite atrocious security holes here, but this could be fun when combined with, say, an Arduino that's interfaced with a coffeemaker or garage door.

#!/usr/bin/env python
# This file should be called emailcli.py (or something like that).

''' emailcli.py : Scan mailbox for cli messages, and then execute specific bound commands.
'''
import os, sys, imaplib, rfc822, re, StringIO

server  ='imap.gmail.com'
username='EMAIL@gmail.com'
password='PASSWORD'

def connect(email,password):
    imap = imaplib.IMAP4_SSL("imap.gmail.com")
    imap.login(email, password)
    return imap

def disconnect(imap):
    imap.logout()

pattern_uid = re.compile('\d+ \(UID (?P\d+)\)')
def parse_uid(data):
    match = pattern_uid.match(data)
    return match.group('uid')

imap = connect(username,password)
print "Listening for approved commands at %s..." % username
while True:
    imap.select("cli_inbox")
    resp, items = imap.search(None, 'ALL')
    email_ids  = items[0].split()
    for num in email_ids:
        resp, data = imap.fetch(num, "(UID)")
        typ, msg_data = imap.fetch(num, '(uid BODY[TEXT])')
        try:
            msg_uid = parse_uid(data[0])
            message = msg_data[0][1]
            result = imap.uid('COPY', msg_uid, 'cli_archive')
            if result[0] == 'OK':
                mov, data = imap.uid('STORE', msg_uid , '+FLAGS', '(\Deleted)')
                imap.expunge()
            command = message.rstrip().lstrip()
            print "%s$ %s" % (os.getcwd(), command)
            binpath = "/home/tsbertalan/bin/emailclibin/"
            dirlist = os.listdir(binpath)
            shortcommand = command.split()[0].rstrip()
            if shortcommand in dirlist:
                system_command = "~/bin/emailclibin/"
                system_command += command
                movit = os.system('cd ~/')
                result=os.system(system_command).rstrip().lstrip()
            else:
                print "Command '%s' is not on the pre-approved list." % command
        except:
            done = 1;

Permalink

Tom's Picasa Web Albums

Five Recent Projects

  1. Simulation of Paracrine Effects on Neural Dynamics
    2012 01 09 17 56 06
  2. Polyhedral Discretizations
    2012 01 01 06 54 55
  3. Joe Saves Tom
    2011 12 04 16 49 34
  4. KP Site
    2011 12 01 09 45 33
  5. TomBertalan.com CI-powered site
    2011 12 01 09 44 49

Five Recent Entries

  1. Run Scripts by Email or SMS
    2012 01 25 07 15 51
  2. Pies, pies, and PIEs
    2012 01 12 09 32 14
  3. Store!
    2011 11 06 16 18 28
  4. Concert
    2011 10 06 03 29 53
  5. My Crontab
    2011 09 20 12 08 48

Five Random Entries

  1. Violin Lesson 090711.12
    2009 07 11 12 42 07
  2. Light on the Floor 090714
    2009 08 05 13 35 56
  3. MEXICO AND JAPAN ARE REALLY THE SAME COUNTRY!!1
    2010 02 21 09 00 50
  4. Random Division of a Population Into Two Groups
    2009 07 11 11 03 28
  5. Barry Long—Purveyor of Fine Bullshit
    2010 04 22 17 54 14