Android and Linux

Wednesday, July 21, 2010

Backup photos with rsync

I've never used rsync much but have always wanted to try it more. Maybe it was my inexperience or maybe it's the non-standard Android version, but it turned out to be a pain!

I started with a generic rsync command and customized it, but every step failed and I had to find the answer why. Then, after a successful connection and transfer, it threw errors on every file, which I found was due to a bug in some versions of rsync, so I had to find a workaround to that.

At any rate, here is a basic, working rsync command for transferring photos from the phone to a computer.

It can be used to back up any directory by changing "/sdcard/DCIM/Camera" to the directory you want to backup. It can also transfer files from the computer to the phone by changing the order of the two paths from /sdcard/DCIM/Camera COMPUSER@IP:/PATH/TO/SYNC/TO to COMPUSER@IP:/PATH/ON/COMP /PATH/ON/PHONE
#! /system/bin/sh
rsync -rltDv -e "ssh -l USER -i /PATH/TO/SSHKEY" /sdcard/DCIM/Camera USER@IP:/DIR/TO/SYNC/TO
Copy "syncpics" to your clipboard with this QR code, but don't forget to edit it to fill out your login information.

Monday, July 19, 2010

Various Linux/phone ssh transfer commands

ssh is wonderful for computer to computer communication and here are a few scripts which I use for PC <-> phone communication:

First, most of these use ssh and to log in without a password, they use ssh keys. There are numerous tutorials for setting up ssh keys, but I always point people to this site because it's a relatively clear explanation. Since this is an Android related blog, the Cyanogen wiki article on using Dropbear for ssh is more geared to our phones.

Once ssh keys are set up, one problem that may be encountered is your home IP address. You don't want to hard code it into a bunch of scripts in case it were to change, plus you may not be able to log in if it changes unless the computer notifies you in some way.

My solution is to store my home IP address in a hidden file on my sdcard named ".hip", which stands for "Home IP." Then I have a simple script named "hip" to display the contents of that file:
#! /system/bin/sh
cat /sdcard/.hip
Any time I write a script or command to connect to my computer, I put the "hip" command in a subshell so that it executes and places my IP address wherever it belongs in the script.

If my home IP is 123.45.43.21 and that is contained in the .hip file, then "ssh user@123.45.43.21" is the same thing as "ssh user@$(hip)"

I have a cable connection and my IP changes any time the cable goes out, which is once every couple of months. I have this script running on a cron job at home every hour to check for an IP change.
#! /bin/sh
ip=$(lynx -dump http://whatismyipaddress.com | grep "IP Information" | awk '{ print $3 }')
if
grep -q $ip .ip
then echo "IP matched, not sending mail"
else
echo "Sending IP change notification"
echo $ip > .ip
nail 1234567890@txt.att.net < .ip
fi
It records the address in a file, checks the current IP and compares the two. It then sends a text message to my phone if they change. I can then put the new address in my .hip file and all my scripts work again.

Now, on to the fun stuff. Here are a few for transferring files:

ton1 - used to grab a file from my computer to the phone. To use, type "ton1 /home/me/somedirectory/file.txt"
#! /system/bin/sh
scp -i /PATH/TO/KEY USER@$(hip):"$1" /sdcard
ton1r - recursively transfer a directory. To use, type "ton1r /home/me/somedirectory"
#! /system/bin/sh
scp -i /PATH/TO/KEY -r USER@$(hip):"$1" /sdcard
tohome - Sends a file to my computer. To use, type "tohome /sdcard/somedirectory/file.txt"
#! /system/bin/sh
scp -i /PATH/TO/KEY "$1" USER@$(hip):/PATH/TO/DIRECTORY
tohomer - Sends a directory to my computer. To use, type "tohomer /sdcard/somedirectory"
#! /system/bin/sh
scp -i /PATH/TO/KEY -r "$1" USER@$(hip):/PATH/TO/DIRECTORY
With my copy/paste trick for the terminal, I use these scripts:

First, I have this script named "paste2n1" on my home computer so I can log in at any time and get the clipboard contents without having to export the display:
#! /bin/sh
export DISPLAY=:0.0 && xsel -p && unset DISPLAY
And on the phone, this one is named "homeclip" and it logs in and runs the paste2n1 script, displaying the PC clipboard in the phone's terminal.
#! /system/bin/sh
ssh USER@$(hip) -i /PATH/TO/SSH/KEY 'paste2n1'
This one, named "tohomeclip" will take the contents of the phone's clipboard and copy it to the computer.
#! /system/bin/sh
clip | ssh USER@$(hip) -i /PATH/TO/SSH/KEY 'export DISPLAY=:0.0 && xclip && unset DISPLAY' && echo "copied to PC:
$(clip)"
I also have these commands set up in GScript, which allows you to run commands or scripts from a home screen shortcut.
homeclip | copy
- transfers PC's clipboard contents to the phone's clipboard
ton1 $(homeclip)
- Allows me to copy a file name on the PC and transfer it to the phone.
ton1r $(homeclip)
- Same thing, except it works with a directory.

Once you can access your computer with your phone from anywhere in the world, and do it with very simple scripts, your mind tends to think of ways to use it, and these are just some examples of simple fun you can have with ssh.

Wednesday, June 30, 2010

QR Codes

I'm going to start adding QR codes to scripts I post here. QR codes are neat, but I've never found much use for them until I realized Barcode Scanner has an option to copy text from QR codes to the clipboard. Cool.

Here is an example using my locate.sh script, and the preview from Barcode Scanner.





Note, there is no button in Barcode Scanner to save as a file, but if you hit the menu, there is an option to automatically save text to the clipboard. If that's turned on, you just scan the code then open a text editor and paste.

I'm not sure anyone will find any use for the codes, but getting the script to the phone is the number one problem users have with my iphone language deletion script. I get 100 hits a day to that page and it seems like 50% have trouble getting it on the phone. Too bad QR codes can only handle 4000 characters and that puppy is over 9700.

None of the online qr code generators work very well. I couldn't find any that encoded newlines so the whole script would end up being copied on one line. I finally decided to see what was available for Linux and ran across the qrencode command line utility which seems to work perfectly.

I'll be going back and adding them soon. EDIT: Done!

Saturday, June 26, 2010

Another unix tool (poorly) imitated

Android doesn't have a whois command, so I hammered one out the best I could. Unfortunately, it looks up the info from the who.is website, which could potentially change and render this useless, and it currently only works with IP addresses. It's not perfect, but it's the only whois for Android I know of.

It can be done with domain names, but the output is different depending on the server the whois site gets the info from, and that makes it impossible to predict which lines to extract. I did put in a -p "please" option to semi-work with domain names. It will look up the IP address for the domain, then do a whois on the IP. It fails sometimes, which is why I used it as an option.

This will also be uploaded to the script section of the L.E.S. Linux Android app, which just had an update and is working with Froyo now.

#! /system/bin/sh
if test -z "$1"; then exec echo "USAGE: whois.sh ip_address, -p for please, --help for more info"; fi
case "$1" in

--help)
echo 'whois.sh by fubaya (a-more-common-hades.blogspot.com).
Another cheap imitation of a missing unix tool. This simply displays the output of a search on the who.is website.
It currently only works with IP addresses, however with the -p (please) option,
it can use the ping command to get the IP address then look up the whois information for that IP.'
;;

-p)
if echo $2 | grep -q [a-z]
then
ip=$(ping -c 1 ${2} | grep PING | awk '{ print $3 }' | tr -d "()")
if test -z $ip; then exec echo "Sorry, something went wrong with the ping lookup for this domain.
Try getting rid of the 'http://'"; fi
wget -q -O - http://who.is/whois-ip/ip-address/${ip}/ | sed -e 's/&nbsp;/ /g' -e 's#<[^>]*>##g' | awk 'BEGIN{ RS="Extended Info"}{gsub(/.*Domain Search/,"");print;exit}' | sed "/^$/{N
/^\n$/D
}"
else exec echo "I was looking for an alphabetic domain name but didn't see one and got confused. Exiting."
fi
;;

*)
if echo $1 | grep -q [a-z]
then exec echo "Sorry, this only works with IP addresses unless you specify the -p option. Please see --help first."; fi
wget -q -O - http://who.is/whois-ip/ip-address/${1}/ | sed -e 's/&nbsp;/ /g' -e 's#<[^>]*>##g' | awk 'BEGIN{ RS="Extended Info"}{gsub(/.*Domain Search/,"");print;exit}' | sed "/^$/{
N
/^\n$/D
}"
;;

esac
Copy whois.sh to your clipboard with this QR code:

Thursday, May 27, 2010

Copy/paste between Linux and N1

Using my post about copy/paste in the Android terminal, it becomes easy to share clipboard contents between your phone and Linux.

First, you need a way to access the Linux clipboard from a terminal. I use either xclip or xsel. Both are old programs which always seem to work differently for each application, so this example uses both.

You need to set up ssh keys to login to your home computer from the phone. The general layout for using keys in a script is something like: ssh USER@IP -i KEYFILE. You can also briefly login to a computer and run a command over ssh by using: ssh USER@IP -i KEYFILE 'command' and that's what we'll be doing.

In order to get the computers clipboard contents, we'll use the command "xsel -p." Since xclip and xsel are utilities meant to interact with the X display, you may also need to export the display then unset the display in order to get a clean logout with ssh. For simplicity, I made a script called paste2n1 which can be ran with the ssh command.

#! /bin/sh
export DISPLAY=:0.0
xsel -p
unset DISPLAY


Now I can run this command to display the computer's clipboard on my phone:
ssh USER@IP -i KEYFILE 'paste2n1'

By piping it to the copy script I posted previously and running this command from gscript, I can copy the computer's clipboard contents to the phone with one tap of an icon on the phone.

ssh USER@IP -i KEYFILE 'paste2n1' | copy

To paste the phone's clipboard contents to the computer, I'll use xclip with this script named n1_to_comp on the computer:
#! /bin/sh
in=$(cat /dev/stdin) # or "in=$(cat -)"
export DISPLAY=:0.0
echo "$in" | xclip
unset DISPLAY


ssh supports most commands through pipes so you can send any text to the computer with the command: ssh USER@IP -i KEYFILE 'n1_to_comp'

You could use that as a flexible all-around paste command/script to paste any output to the comptuer such as: cat textfile | ssh USER@IP -i KEYFILE 'n1_to_comp'

Or use my previous command I called "clip" for pasting the phone's clipboard in the terminal for a one touch clipboard-to-clipboard transfer using gscript: clip | ssh USER@IP -i KEYFILE 'n1_to_comp'

These are just examples and instead of piping to different scripts, you could write a simple one-liner for each operation. I may write a single script with options to handle both operations if I get bored.

Sunday, May 23, 2010

locate.sh

I polished and uploaded my locate script to L.E.S. Linux. It can be found in L.E.S. under the name "locate.sh" or here:
#! /system/bin/sh

if test -z "$1"; then exec echo "USAGE: locate.sh [filename|-u|--help]"; fi

case "$1" in
--help)
echo 'locate.sh by fubaya (a-more-common-hades.blogspot.com)
This is a cheap imitation of the UNIX locate command. It stores
a list of all the files on your system to /sdcard/.locatedb (about 2MB)
then greps that file to find whatever you are looking for.

USAGE: locate.sh [filename|-u|--help]

[filename] case insensitive search for any file of any type.

-u update the "database." This must be ran periodically if you want to
find new files. I have it set to run at boot on phone.

--help display this help message

EXAMPLE: $ cd /; touch /data/testfile
$ locate.sh testfile
$
$ locate.sh -u
database updated
$
$ locate.sh testfile
/data/testfile
$
'
;;
-u)
find / -print > /sdcard/.locatedb && echo "database updated"
;;
*)
grep -i "$1" /sdcard/.locatedb
;;
esac


Copy locate.sh to your clipboard with this QR code:

Thursday, May 20, 2010

Whereis redux

I made my little whereis script a little better and uploaded it to the Android app L.E.S. Linux.

L.E.S. Linux is a cool little app which allows you to download Linux executables, scripts and libraries which have been compiled or made for Android. I will probably put any scripts which I think may be useful on L.E.S. The only other one I have in mind so far is the little "locate" script which I'll upload after I make it a little more presentable.

Here is the whereis script I uploaded. I named it whereis.sh to avoid confusion with the real (but missing on Android) whereis executable but, oops, I accidentally called it "whereis" instead of "whereis.sh" in the actual script. Oh well.

#! /system/bin/sh

if test -z "$1"
then
echo "whereis.sh by fubaya (a-more-common-hades.blogspot.com)
This is a cheap imitation of the UNIX whereis command.
It doesn't imitate all the whereis functions, it only looks in your
path for the binary (or other file) which you specify as an argument.

USAGE: whereis [arg]
EXAMPLE:
$ whereis date
/system/bin/date
/system/xbin/date
$"
else
echo $PATH | tr ':' '\n' | while read line; do ls "${line}/${1}" 2>/dev/null; done
fi


Copy whereis.sh to your clipboard with this QR code:

Followers