a pic[ google | mail | ip | ov | reg | tv | cnn ]
[ man | chr | en-en | nl-en | en-nl | h4 ]
[ pub-cvs | ic-cvs | blog | scratch | py ]
[ comp | prog | ele | gen | trav | ecs ]
[ Home | Edit | Print | Top | Topics | Help! ]
[ Home | Edit | Print | Top | Topics | Help! ]

Send your PC commands using your (SMS-capable) cellphone


The text below describes a basic setup where you can send your home PC messages and commands through your mobile phone (assuming some things). It can be the basis for home control, remote detonating, and... more! :-)

The basic idea is to send Email from your cellphone using an SMS-to-Email gateway (a special phonenumber that parses your SMS message into an Email address and message contents, and sends it as Email to the intended recipient). On the PC, a target Gmail address is polled using POP3, message contents are interpreted, and commands can be executed (not mentioned in detail).

[ Home | Edit | Print | Top | Topics | Help! ]

Great! What do I need? (...quick!!!)


Here's your basic household chemicals list:
[ Home | Edit | Print | Top | Topics | Help! ]

Ok, check, got it, now what?


[ Home | Edit | Print | Top | Topics | Help! ]

Setting up a Gmail account


This is pretty straightforward; you can get a Gmail account without an invite these days. Go to http://gmail.com and follow the guidelines. Let's assume you set up my.personal.mailgate@gmail.com for this (dedicated) purpose.

After making the account, give yourself a decent password, enable 'POP3' (choose 'Settings', then the 'Forwarding and POP' tab from within your Gmail inbox page, and select to 'delete Gmail's copy' when messages are accessed with POP. Alternatively, you can delete them from fetchmail by passing it the '-K' option.

[ Home | Edit | Print | Top | Topics | Help! ]

Setting up your cellphone


Check with your provider how to use your SMS-to-Email gateway; with Orange .nl, I can send a specially crafted SMS message to number 126, which will then be sent along as an Email message (if composed properly).

Test this by sending an SMS like this...

email my.personal.mailgate@gmail.com test

...to number 126. If all is well, you should see this message appear in your Gmail inbox. Delete it.

[ Home | Edit | Print | Top | Topics | Help! ]

Setting up fetchmail


Get and build fetchmail (on NetBSD, it's in 'pkgsrc/mail/fetchmail, and compiles with SSL enabled by default. You can test whether SSL support is enabled by running...

$ fetchmail --help|grep ssl
      --ssl         enable ssl encrypted session
      --sslkey      ssl private key file
      --sslcert     ssl client certificate
      --sslcertck   do strict server certificate check (recommended)
      --sslcertpath path to ssl certificates
      --sslfingerprint fingerprint that must match that of the server's cert.
      --sslproto    force ssl protocol (SSL2/SSL3/TLS1)

(These options are missing in case SSL support is not enabled.)

[ Home | Edit | Print | Top | Topics | Help! ]

Test fetchmail by sending mail from PC


Test fetchmail by running

$ fetchmail --ssl -u 'my.personal.mailgate@gmail.com' -m 'cat' -p POP3 pop.gmail.com
Enter password for my.personal.mailgate@gmail.com@pop.gmail.com: 
fetchmail: Server certificate verification error: unable to get local issuer certificate
fetchmail: Server certificate verification error: certificate not trusted
fetchmail: Server certificate verification error: unable to verify the first certificate
fetchmail: No mail for my.personal.mailgate@gmail.com at pop.gmail.com
$ 

The error is prolly because I didn't set up SSL properly (or something) - sue me. I would happily have gone along with using plaintext passwords for something like this.

As you can see, there are no messages pending. Notice that when using the '-m' option, fetchmai uses an alternate filter ('cat' on stdin in this case) instead of trying to deliver the message to your local machine.

Try sending yourself a message, and running fetchmail again, verifying it works (the raw message contents should be dumped on your screen). An example:

 1 :   $ fetchmail --ssl -u 'my.personal.mailgate@gmail.com' -m 'cat' -p POP3 pop.gmail.com
 2 :   Enter password for my.personal.mailgate@gmail.com@pop.gmail.com: 
 3 :   fetchmail: Server certificate verification error: unable to get local issuer certificate
 4 :   fetchmail: Server certificate verification error: certificate not trusted
 5 :   fetchmail: Server certificate verification error: unable to verify the first certificate
 6 :   1 message for my.personal.mailgate@gmail.com at pop.gmail.com (687 octets).
 7 :   Delivered-To: my.personal.mailgate@gmail.com
 8 :   Received: from gmail-pop.l.google.com [66.249.93.111]
 9 :           by roy.LAN with POP3 (fetchmail-6.3.8)
10 :           for <michai@localhost> (single-drop); Mon, 30 Jul 2007 21:09:41 +0200 (CEST)
11 :   Received: by 10.141.91.18 with SMTP id t18cs71929rvl;
12 :           Mon, 30 Jul 2007 12:09:18 -0700 (PDT)
13 :   Received: by 10.90.81.14 with SMTP id e14mr2420270agb.1185822558437;
14 :           Mon, 30 Jul 2007 12:09:18 -0700 (PDT)
15 :   Received: by 10.90.86.2 with HTTP; Mon, 30 Jul 2007 12:09:18 -0700 (PDT)
16 :   Message-ID: <4f8bd7300707301209j55d3915xcdb913793f8b4749@mail.gmail.com>
17 :   Date: Mon, 30 Jul 2007 21:09:18 +0200
18 :   From: "Michai Ramakers" <m.ramakers@gmail.com>
19 :   To: my.personal.mailgate@gmail.com
20 :   Subject: test subject
21 :   MIME-Version: 1.0
22 :   Content-Type: text/plain; charset=ISO-8859-1
23 :   Content-Transfer-Encoding: 7bit
24 :   Content-Disposition: inline
25 :   
26 :   test contents
27 :   reading message my.personal.mailgate@gmail.com@gmail-pop.l.google.com:1 of 1 (687 octets) flushed
28 :   $ 

Things to note:
[ Home | Edit | Print | Top | Topics | Help! ]

Testing sending a message from your cellphone


Good, all works; now try the same using your cellphone, sending for example an SMS...

email my.personal.mailgate@gmail.com this is another test

...to your SMS-to-Email gateway. If this worked, run fetchmail again:

 1 :   $ fetchmail --ssl -u 'my.personal.mailgate@gmail.com' -m 'cat' -p POP3 pop.gmail.com
 2 :   Enter password for my.personal.mailgate@gmail.com@pop.gmail.com: 
 3 :   fetchmail: Server certificate verification error: unable to get local issuer certificate
 4 :   fetchmail: Server certificate verification error: certificate not trusted
 5 :   fetchmail: Server certificate verification error: unable to verify the first certificate
 6 :   1 message for my.personal.mailgate@gmail.com at pop.gmail.com (1133 octets).
 7 :   reading message my.personal.mailgate@gmail.com@gmail-pop.l.google.com:1 of 1 (1133 octets).Delivered-To: my.personal.mailgate@gmail.com
 8 :   Received: from gmail-pop.l.google.com [66.249.93.111]
 9 :           by roy.LAN with POP3 (fetchmail-6.3.8)
10 :           for <michai@localhost> (single-drop); Mon, 30 Jul 2007 21:19:24 +0200 (CEST)
11 :   Received: by 10.141.91.18 with SMTP id t18cs72065rvl;
12 :           Mon, 30 Jul 2007 12:18:32 -0700 (PDT)
13 :   Received: by 10.86.63.19 with SMTP id l19mr4126602fga.1185823111840;
14 :           Mon, 30 Jul 2007 12:18:31 -0700 (PDT)
15 :   Return-Path: <+31612345678@sms.orange.nl>
16 :   Received: from mailer.gin.nl (mailer.gin.nl [194.165.79.90])
17 :           by mx.google.com with ESMTP id f19si1149602fka.2007.07.30.12.18.31;
18 :           Mon, 30 Jul 2007 12:18:31 -0700 (PDT)
19 :   Received-SPF: neutral (google.com: 194.165.79.90 is neither permitted nor denied by best guess record for domain of +31612345678@sms.orange.nl)
20 :   Received: from orange.gin.nl ([194.165.79.82])
21 :           by mailer.gin.nl (8.12.10/8.12.10) with SMTP id l6UJIUmp087042
22 :           for <my.personal.mailgate@gmail.com>; Mon, 30 Jul 2007 21:18:30 +0200 (CEST)
23 :   Message-Id: <200707301918.l6UJIUmp087042@mailer.gin.nl>
24 :   Date: Mon, 30 Jul 2007 21:18:29 +0200
25 :   From: +31612345678@sms.orange.nl
26 :   Organization: Orange GSM Email
27 :   To: my.personal.mailgate@gmail.com
28 :   Subject: Email van een mobiele telefoon
29 :      (msg: this is another test)
30 :   
31 :   >--- Begin bericht ---
32 :   
33 :   this is another test  
34 :   
35 :   >--- Einde bericht ---
36 :   
37 :    
38 :    
39 :    
40 :    
41 :    
42 :    
43 :    
44 :    
45 :    
46 :    
47 :    
48 :   
49 :    flushed
50 :   $ 

Some things to note:
[ Home | Edit | Print | Top | Topics | Help! ]

Making a script using 'expect' to automatically poll mail and execute commands


The rest is sort of trivial (in fact, all was trivial :-) - doing the above at fixed intervals, parsing message contents, and doing stuff when a proper message arrives. For expect examples, you might like this page. Behold:

 1 :   $ su
 2 :   Password:
 3 :   # cd /usr/libexec
 4 :   # ls -l poll_phone_cmds 
 5 :   -rwx------  1 root  wheel  840 Jul 29 22:05 poll_phone_cmds
 6 :   # sed 's/^/>   /' < poll_phone_cmds  
 7 :   >   #!/bin/bash
 8 :   >   
 9 :   >   PHONE_CMD=/tmp/phone_cmd.txt
10 :   >   
11 :   >   ### poll until we have received a new message
12 :   >   while ( true ); do
13 :   >     expect - << HERE > /dev/null 2>&1
14 :   >   spawn fetchmail --ssl -p POP3 -u my.personal.mailgate@gmail.com -m "cat > $PHONE_CMD" pop.gmail.com
15 :   >   expect "Enter password"
16 :   >   send "MySecretPassword\r"
17 :   >   expect eof
18 :   >   HERE
19 :   >     if [ -f $PHONE_CMD ]; then
20 :   >       FROMLINE=`grep '^From: ' < $PHONE_CMD | sed -n '1p'`
21 :   >       if [ "$FROMLINE" = "From: +31612345678@sms.orange.nl" ]; then
22 :   >         CMDLINE=`grep '^cmd: ' < $PHONE_CMD | sed -n 1p`
23 :   >         if [ -n "$CMDLINE" ]; then
24 :   >           CMD=`echo $CMDLINE | sed 's/^cmd: \(.*\) *$/\1/'`
25 :   >           #
26 :   >           # HERE: we have the command string in "$CMD", happy hacking :-)
27 :   >           #
28 :   >         fi
29 :   >       fi
30 :   >       rm $PHONE_CMD
31 :   >     fi
32 :   >     sleep 10
33 :   >   done
34 :   >   
35 :   >   
36 :   # 

Things to note:
That's all! :-) You can run this script from /etc/rc.local or whatever, or remove the polling and run it straight from cron, which is prolly more sane than this. An example SMS:

email my.personal.mailgate@gmail.com cmd: foo bar baz

[ Home | Edit | Print | Top | Topics | Help! ]

If you are bored


Things to do with this new technology:
Note that you too can send free SMS's by using Google (thank you Google :-) Calendar, and its notify-by-SMS service. This too is left as an exercise for the reader ;-)

Enjoy!

Powered by MicKI - MICKI_2008_05_09