Android and Linux

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:

Monday, May 3, 2010

Paste in an Android terminal

I've always found it useful to be able to paste the clipboard contents in a terminal. I often find links in the Browser that I want to download with wget, but how to get the link into terminal without having to type it out?

My brief search for how clipboard content is stored yielded no results. It would be nice if it was stored in a file so we could grab the contents, but it's probably held in memory. At any rate, there is a workaround very similar to the workaround I used on the iPhone.

I went looking for some sort of clipboard helper app that does store the clipboard in a text file. The one I settled on is called Clipstore by benishouga and it stores the clipboard contents in a plain text file at /data/data/jp.benishouga.clipstore/files/clip.txt.

It is meant to keep a clipboard history of multiple items and it stores every entry on a single line. It replaces newlines with a "n" tag so even if you have 20 clipboard history items consisting of multiple lines, the most recent item will always be on top and it will all be on a single line. So the paste script can simply be:
#! /system/bin/sh
# Requires the app "Clipstore" by benishouga and app must be opened or allowed to
# run in the background in order to transfer system clipboard to app clipboard
sed 's/<n>/\n/g;q' /data/data/jp.benishouga.clipstore/files/clip.txt
I suggest not naming it "paste" to avoid any conflicts with the unix command of the same name. I named it "clip".

Copy "clip" to your clipboard with this QR code:


I may try to figure out a way to do a copy command soon, but it isn't usually as handy.

Update: Copying text is actually pretty easy using Clipstore. You just have to stick the text in the file at /data/data/jp.benishouga.clipstore/files/clip.txt, but to use it, you have to open Clipstore and select the text. This makes Clipstore put it in the system's clipboard and it can then be pasted in any app. Neither the copy or paste are perfect, but I don't know of a way to access the system clipboard directly. Neither command is needed very often, although they're lifesavers when they are, so having an extra step isn't really a problem.

Here's the copy script:
#! /system/bin/sh
# Requires the app "Clipstore" by benishouga and text must be
# selected in Clipstore to transfer it to the system clipboard
in=$(cat -)
lines=$(echo "$in" | wc -l)
if [ "$lines" = 1 ]
then sed -i "1i\\
$in" /data/data/jp.benishouga.clipstore/files/clip.txt
else
inn=$(echo "$in" | sed ':a;N;s/\n//g')
sed -i "1i\\
$inn" /data/data/jp.benishouga.clipstore/files/clip.txt
fi

Copy the "copy" script to your clipboard with this QR code:

Wednesday, April 28, 2010

Dump a webpage in terminal

This doesn't work as well an lynx -dump, but is a simple alternative that does work pretty well for some pages. It uses wget to dump the raw html to stdout, then sed to clean out the javascript and html tags. I also have an optional grep tacked on the end to get rid of lines of text from Google Adsense which can add up to quite a bit on pages which use it. That was just something that was present on the pages I tested it with and I figured I might as well leave it.

The first line is a test to check for proper formatting of the link. The wget present on Android phones is a stripped down Busybox version which requires the http before the address and simply won't work for "google.com". The script checks for the http and adds it if it's not present, so if you type "dump google.com", it should still work.

Unfortunately, it doesn't work well at all on pages containing css.

#! /system/bin/sh
echo $1 | grep -q http || pre="http://"
wget -q ${pre}${1} -O - | sed -e '/<script type="text\/javascript"/,/<\/script>/d' -e 's#<[^>]*>##g' | grep -v googleAdd

Copy "dump" to your clipboard with this QR code:

Followers