Android and Linux

Sunday, October 30, 2011

Run Tasker task from command line

I have been looking for an alternative to Notify My Android in order to send myself notifications to my phone when I'm at home. NMA notifications work fine, but you have to open the app to clear them and I just wanted a vibration or sound that didn't leave anything else on the phone for when a job finished at the computer or something.

I first looked at playing a media file with the following command:

am start -n -d /sdcard/foo.mp3

That sorta works. It opens the media app and plays the file then immediately goes to the next song. Adding && sleep 1 && pkill makes it work more like I wanted, but that's too ugly.

I ran across this post in the Tasker forum about invoking a task from the command line, but the consensus was that it wouldn't work unless the command line app has the right permissions.

But then I discovered that this works:

am broadcast -a -e task_name YOUR_TASK_NAME

The only difference being the "-e" option instead of "-es."

So I made a simple script called "task" for the phone.
#! /system/bin/sh
am broadcast -a -e task_name "$*"
Now I can bounce the command off the phone via ssh and run any Tasker task I want.

Wednesday, October 19, 2011

The Pogoplug is taking over my life

The $50 Pogoplug that I bought to goof around with is now my main server.

The Pogoplug is about the size of a small, thick book. To the right is an Edge DiskGo network attached storage drive. That's where I keep all my media to be played on the TV via the Roku and the MyMedia Server Roku app. The Pogoplug is running a small python server that acts as the companion to the Mymedia app and I simply mounted the drive over the network because I didn't have any USB cables to plug it in directly, so it acts as if it is serving the files from it's own drive.

Debian is installed on the USB stick in the front of it. The white cable in the back is the power cord, and there's a cat5 plugged into the back which you can't see, and the two black cords are USB cords running to my CM11A and W800RF32A, which are an X10 controller and wireless receiver respectively.

It's not only serving media and running the automation, but other scripts to check weather, watch my IP address, back up files from my phone when I connect to my home wifi, etc. Everything installed and runs just as easily as it would on any desktop Linux. The only hard part was getting accustomed to Debian, but it's not so different from Slackware and at least it's not Ubuntu.

That's the beauty of Linux, very small programs requiring very little resources can do as much, if not more, than some $100 program you'd have to buy for a Windows computer, and they can do it in the background without you having to be there to point and click, and they can do it on very minimal hardware.

Thursday, October 13, 2011

remote ssh to multiple hosts on same network in Android

I have four devices running ssh at home. Since they're all attached to my router, they all have the same IP address to the outside world, but ssh runs on different ports on each device. In theory, you can ssh into each one by specifying the correct port. In practice, however, it's not always that simple, especially with Android.

In order to prevent a man-in-the-middle attack, when ssh makes a connection, it saves the computer's IP and host key in the ~/.ssh/known_hosts file and the next time you try to connect, it checks the IP in that file to make sure the host key is the same.

Let me illustrate. Let's say my home IP address is I have 2 devices, comp1 and comp2 running ssh on port 111 and 222, respectively. And let's just say their host keys are AAA and BBB (the keys are actually much, much longer). If I ssh into port 111, then this gets entered into the known_hosts file: AAA.

Now, if I try to ssh into comp2 at port 222, it gets the host key BBB from the computer and looks in the known_hosts file and says, "whoa, I expected AAA from the computer at, something is wrong" then the connection fails.

There are normally a couple of solutions for this. First, you can sometimes connect to comp1, copy the key from known_hosts then remove it from the file and connect to comp2. Now put the comp1 key back in known hosts. I've never done this and heard that it doesn't always work, but sometimes ssh will keep trying entries in known_hosts until it gets one right.

The better solution is to set up ssh config file like so:

Host comp1
Port 111
HostKeyAlias = comp1
CheckHostIP = no

Host comp2
Port 222
HostKeyAlias = comp2
CheckHostIP = no

Then you can connect with the command ssh comp1 or ssh comp2.

Unfortunately, that doesn't work with Android because it doesn't have an ssh config file. I'm not entirely sure you can do anything about it with the normal Android ssh program. I deleted it long ago and use the ssh binary from Better Terminal Emulator Pro (see this post) and there is a workaround with it.

The first trick doesn't work with the BTEP ssh binary, and neither does the second trick because it doesn't seem to use a config file. As often is the case with Android, the ugliest solution is the only solution.

BTEP looks for the known_hosts file in /data/data/com.magicandroidapps.bettertermpro/home/.ssh (sorry, I originally posted the wrong directory. Fixed now!). You can use the first trick to get a copy of the host keys, then write a script to overwrite known_hosts depending on which computer you want to connect to.
#! /system/bin/sh

case "$1" in

echo "$(hip) ssh-rsa AAA==" > /data/data/com.magicandroidapps.bettertermpro/home/.ssh/known_hosts
ssh USER@$(hip) -i PATH/TO/KEYFILE -p PORT;;

echo "$(hip) ssh-rsa BBB==" > /data/data/com.magicandroidapps.bettertermpro/home/.ssh/known_hosts
ssh USER@$(hip) -i PATH/TO/KEYFILE -p PORT;;

You would fill in your user, path to the ssh key and port number. You can also fill in the IP address, but that's what the $(hip) part does. hip is a script that tells me my home computer's IP address which I've blogged about here and here. Essentially, I keep my home IP address in a file and the hip command just displays that file. That way, I don't need to hard code my IP in any scripts. If my IP changes, I can just update it in that file and not worry about changing the scripts.

I named the script above simply "s". So, if I run s comp2, it will write ssh-rsa BBB== to known_hosts then connect to comp2.

Incidentally, this also takes care of another problem- IP changes. The first time you connect to a host, ssh asks if you really want to connect and you have to type yes or no.

If you normally have ssh-rsa BBB== in your known_hosts file and your home IP changes for some reason, the new IP won't be in the file and your scripts will break until you manually log in and tell ssh yes or no. But now that no longer matters because we are controlling known_hosts and filling it with the correct up-to-date IP and associated key.

The other solutions are better for computers that are running the real OpenSSH, but this is the only solution I could figure out for Android and it's funky programs.

Tuesday, October 11, 2011


I was in Best Buy today and noticed that they sell Pogoplugs for $50 so I picked one up. I believe it is the first generation without wifi, but I don't care about that, I prefer using network cable when available over wifi anyway. The downside to Pogoplugs is that they have very little space available on them, but you can install Linux on an external hard drive plugged into them via USB. I didn't have an external drive, per se. I have a NAS network drive, and that may work, but I didn't have a USB cord to attach it, so I installed Linux on a USB stick for the time being.

Using this guide, it only took a few minutes to have a very minimalistic version of Debian running on it, and after installing the gcc compiler and other tools, I was able to install Heyu. Then I plugged my CM11A X10 controller into the Pogoplug and was a little surprised that the darn thing actually worked. Awesome!

The NAS drive is what I use to store videos for streaming to the Roku. I tried to install the server program on the Pogoplug using the USB stick just to see if it would work, but python threw socket errors for some reason. I'm going to investigate whether I can use the NAS with the Pogoplug. If not, I'll probably get an external drive and use it to serve videos, control the home automation, and various other things I have a computer doing now. I'm not entirely certain I want to turn over everything to a little box like this, but I want to do it to see for myself that it can be done, then change back to the computer if I prefer it.

I think it's pretty amazing that, for $50, you can get a computer the size of a book that can do all this. Then again, the Raspberry is only going to be $25, so maybe I got ripped off!

Friday, October 7, 2011

Yay, new computer.

I bought a new computer and, boy, why didn't someone tell me computers had gotten so darn good these days? My main desktop was a 2.2 GHz box with 2 gigs of ram. That's pretty outdated but was enough for everything I needed, but I wanted to turn it into a server and get a new one for general use. Now I have an i7 quad core with eight 3.3Ghz processors. What a difference!

Since I got the Roku, I found myself wanting to encode videos to play on it. With the old computer, encoding a 175mb 30 minute TV show took about 45-50 minutes and the fans on the computer sounded like a jet trying to take off. And movies? They weren't worth spending hours to encode.

The new computer? It can encode a TV show in 1-2 minutes and a 3 Gig HD movie in 15-30 minutes and it doesn't make a sound the entire time.

I've had it for a couple weeks, but soon after getting it, half of my home automation stopped working. I finally figured out that the computer was to blame. The instant the power cord is plugged into the computer, it causes interference that drowns out the X10 signals. I ordered a noise filter that finally arrived so now I can leave it plugged in as long as I want and still have my automation working.

Anyway, I've been slowly setting up the old computer to act as a server. It isn't a lot of work, it was really a desktop/server before, but I need to get everything set up so I can stick it in a closet somewhere and forget about, administering it from the new computer when needed.

I should have a lot to post about soon. I have a transceiver for my home automation on the way in order to pick up RF signals. With it, I will be able to set up motion detectors and things that can send a signal to the computer instead of only the other direction. Automation is only half useful without that.

I'm also planning to start toying around with plug computing. A plug computer is a full fledged linux computer that fits in a box the size of an electric plug. With the right setup, I could do away with my computer that acts as a server and use a plug computer to run the automation, and mount a cheap hackable display like the Chumby in the house to control it all graphically, and of course Android to control it all remotely.

Anyway, sorry for not having much to post about but hopefully that will change soon.