Sunday, June 19, 2011

Control the Window Geometry of Any Linux / BSD Graphical Application....

Super short and sweet post this time...

Did you know that you can can control the window geometry and placement of any graphical application in most, if not all, desktop environments/window managers, permitted an x window environment is available (e.g. Xorg)?

This is useful with machines when you notice the same graphical applications staying open.  Another benefit is that it provides the means to be more efficient when you can start to remember where specific windows are located at any given moment.  Here's the one liner -  feel free to use this code to automate your desktops into productive and friendly minions:


















Said one-liner:

$ wmctrl -l | grep -i konqueror | awk '{print $1}' | while read win_id; do transset-df -i $win_id 0.9; wmctrl -ir $win_id -e 0,0,1700,1200,185; done

Here's a quick explanation of the aforementioned one-liner (ensure konqueror is already running):

  1. "wmctrl -l" provides a list of every graphical window currently running.  
  2. "grep -i konqueror" looks for a case-insensitive window titled, "konqueror." 
  3. "awk '{print $1}'" only displays the first column of the "konqueror" line from stdout via "wmctrl -l."  The first column is the window id for Konqueror.
  4. "while read wind_id; do " read in the id column (first column) from "wmctrl -l"
  5. "transset-df -i $wind_id 0.9;" change translucency of window to 0.9. Love it.
  6. "wmctrl -ir $wind_id -e 0,0,1700,1200,185;" change the geometry (size) of the window and its placement (where the window will actually reside on the desktop)
  7. "done" all done ;)

Hope you find this useful!

adiĆ³s amigos!
  

If you enjoyed this post, send us = kudos = 
(Bitcoin Addr: 19n6q3GZfoM64oqv5HsDnhzqvcEvJUvmdx)

Control a Synergy Server and its Clients from One Server Script, One Keyboard, and One Mouse - the Easy Way

Today we're going to cover an amazing little utility that has made many-a-boxen here at the hang4r look like Tank's operator station from the movie, "The Matrix."




This little utility is known as synergy and is used for controlling multiple computers from one keyboard and mouse over any network.  For more information on installation/configuration, please visit here.

Unfortunately, synergy comes with one noticeable caveat - no network traffic encryptie supportie.  Guess we'll have to remedy that, won't we?

Now, there are plenty of blogs out there on the ol dub-dub-dub that describe how to enable network-based encryption for synergy using stunnel or openssh.  Unfortunately, they typically present this with a solution that requires a series of scripts that live on the synergy server (synergys) and the synergy client (synergyc).  If you're as serious about synergy as we are, you'll notice it gets a bit "twitchy" when you constantly find yourself having to disconnect/reconnect often...calling different scripts here and there, rubbish.  Time to change all that :)

Today, you'll have the opportunity to learn how to connect your synergy clients to a synergy server all sourcing from one synergy server.  This is all accomplished through our friend, OpenSSH and its reliable wingman, reverse tunneling.   

Again, this all in one, easy shell script connects synergy clients to the synergy server from...you got it, the synergy server.  That's right, no more annoying typing on this keyboard or that keyboard to get your synergy clients connected...clackity-click-clack.  We heart that idea almost as much as we heart lock picking as a past-time in the shop.  While you're thinking about lock picking in the shop, consider picking up this book...it's one of the better ones out there on lock picking:

















Our apologies for the digression - back to the regularly scheduled program...here we go:


STEP 1: Okay, first you must setup key-based authentication for OpenSSH so that your synergy server can authenticate with the synergy clients without requiring a person to actually type a password (or at least a moderately sophisticated zombie robot).

STEP 1.1: Run the following command from your synergy server:
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/<user>/.ssh/id_rsa):<PRESS_ENTER>
Enter passphrase (empty for no passphrase):<LEAVE BLANK, PRESS_ENTER>
Enter same passphrase again:<LEAVE BLANK, PRESS_ENTER>
Your identification has been saved in /home/<user>/.ssh/id_rsa.
Your public key has been saved in /home/<user>/.ssh/id_rsa.pub.
The key fingerprint is:
cc:d9:cb:d8:cd:24:ad:87:41:1c:57:90:14:73:00:fb user@hostname
The key's random art image is:
+--[ RSA 2048]----+
|    ..+el...     |
|     . n...      |
|    .   o .      |
|     . + * .     |
|      N O p e    |
|         = O     |
|        . * +    |
|           .     |
|                 |
+-----------------+

STEP 1.2: Transfer the synergy server file, id_rsa.pub to your remote synergy client:
$ scp /home/<user>/.ssh/id_rsa.pub <user>@<remote synergy client>:/home/<user>/

STEP 1.3: SSH into your remote synergy client computer:
$ ssh <user>@<remote synergy client>
<user>@<remote synergy client> password: <TYPE PASSWORD>

STEP 1.4: Append the contents of id_rsa.pub to the authorized_keys file:
$ cat /home/<user>/id_rsa.pub >> /home/<user>/.ssh/authorized_keys

***At this point you should be able to authenticate to your remote synergy client from your synergy server machine.  Please note that you also must ssh from the synergy server as the synergy server user that possesses the id_rsa.pub file within their "/home/<user>/.ssh" directory.  Of course you ssh to the synergy client using the synergy client user that contains the authorized_keys file within their "/home/<user>/.ssh" directory.  So far, so good.

STEP 2: The script...now, here comes the beautiful part.  Create a file called onesyn.sh on your synergy server and paste the following:

#!/bin/bash
# onesyn.sh

# Start the synergy server daemon and sleep to ensure it's up
# prior to kicking off our uber-simple client initialization
synergys &
sleep 2

# now, reverse-ssh into synergy client using key-based auth
# and kick off synergy client over an encrypted network tunnel
ssh -R 127.0.0.1:24800:127.0.0.1:24800 <user>@<remote synergy client> "synergyc -f 127.0.0.1"

# end of script

That's it folks!  Simply save your file and make it executable by issuing the following command:

$ chmod 755 onesyn.sh

Have this script auto-run each time you authenticate with your favorite desktop environment (KDE, Gnome, etc.).  Better yet, use  your favorite window manager instead (OpenBox, FluxBox, FVWM, etc.) since desktop environments are made for people with an unhealthy amount of patience.

Logout, log back in, and enjoy your lavishly excessive array of flat-panel displays, crts, and computing devices all accessible and dominated from one keyboard and mouse!  Yes, it's even okay to sit back, raise your hands towards the back of your head, and totally bask in your amazing awesomeness ;)

Kickin ass n chewin bubble gum.  Later skaterz!

If you enjoyed this post, send us = kudos = 
(Bitcoin Addr: 19n6q3GZfoM64oqv5HsDnhzqvcEvJUvmdx)


quickiedisc.sh - An Oldie but a Goodie for Linux Burning

Copy->paste->save this into a file named quickiedisc.sh. 

#!/bin/bash
usage() {
   printf "*Usage: quickiedisc.sh [--copy --data --iso --rip] <\"file\">\n"
   printf " --copy: Copy CD or DVD utilizing one drive :)\n"
   printf " --data: Burn a specified file or folder to CD or DVD\n"
   printf " --iso : Burn an ISO file to CD or DVD\n"
   printf " --rip : Rip a CD or DVD\n"
   exit
}

if [[ $# != 2 ]]; then
   usage
fi

case $1 in
   "--copy" ) dd if=/dev/scd0 of=/tmp/quickie_tmp.iso;
              eject /dev/scd0;
              printf "Please insert blank disk.  Press any key to continue..."; read anykey;
              cdrecord -v speed=16 dev=ATA:1,0,0 /tmp/quickie_tmp.iso;
              rm /tmp/quickie_tmp.iso;;
   "--data" ) mkisofs -r -o "${2}.iso" "${2}";
              cdrecord -v speed=16 dev=ATA:1,0,0 "${2}.iso";
              #cdrecord -v speed=16 dev=ATAPI:0,0,0 "${2}.iso";
              rm ${2}.iso;;
   "--iso"  ) cdrecord -v speed=16 dev=/dev/scd0 "${2}";;
   "--rip"  ) dd if=/dev/scd0 of="${2}.iso";;
         *  ) printf "Invalid Flag.\n"; usage;;
esac
eject /dev/scd0
# End of script

Also, be sure you set permissions as executable:


$ chmod 755 quickiecd.sh

Enjoy, cheers!

If you enjoyed this post, send us = kudos = 
(Bitcoin Addr: 19n6q3GZfoM64oqv5HsDnhzqvcEvJUvmdx)


Saturday, June 18, 2011

monkeyrunner - Android Automation

Today, we're going to show you how simple it is to automate android events via the Android SDK with a tool known as, monkeyrunner.

The monkeyrunner tool provides an API for writing programs that control an Android device or emulator from outside of Android code (for more information, click here).  The interesting thing about monkeyrunner from our own experience is that if you run:

$ <android_sdk_path>/tools/monkeyrunner

with no parameters, you'll be presented with a wonderful Jython prompt...this means that possibilities are endless and limited only by one's imagination. :)



Now that the cogs are spinnin' and you can say to yourself, "this totally has potential" - let's dive right into automating some Android stuff!

Here's a python script that automates opening an android application, running various keypad events, and taking two screenshots for upload to the Android Developers Market.  Create a file called, monkeyrunner_job1.py and paste the following into the file (of course replacing the proper values of variable objects marked with the comment, "# INFO_NEEDED_ON_LINE_BELOW":

# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
import time
import sys

# Connects to the current device, returning a MonkeyDevice object
# INFO_NEEDED_ON_LINE_BELOW
device = MonkeyRunner.waitForConnection(600,'<adb device value taken from sdk command, "android_skd_path/adb devices">') # Wait 10 Minutes Until Things Start Exploding...

# Installs the Android package. Notice that this method returns a boolean, so you can test to see if the installation worked.
# INFO_NEEDED_ON_LINE_BELOW
install_status = device.installPackage('<apk_path>/app.apk')
print install_status
if install_status != True:
  print "Something went wrong with the installation!"
  sys.exit(1)

## sets a variable with the package's internal name
# INFO_NEEDED_ON_LINE_BELOW
package = 'com.yourmobileapp.prod.YourOwnAndroidApplication'

## sets a variable with the name of an Activity in the package
# INFO_NEEDED_ON_LINE_BELOW
activity = '.Yourmobileapp'

# sets the name of the component to start
runComponent = package + '/' + activity

# Runs the component
device.startActivity(component=runComponent)

time.sleep(60)
device.press('KEYCODE_DPAD_DOWN') # Press Down

# Takes a screenshot
result1 = None
while result1 == None:
  result1 = device.takeSnapshot()

print "Value of result1 Screenshot object"
print result1



# INFO_NEEDED_ON_LINE_BELOW
result1.writeToFile('./SB/screens/screen1.png','png')

# Press the Down Arrow and Long Press to Obtain Context Menu
time.sleep(3)
device.drag((150,300), (150,300), 10, 1) # Long Press
time.sleep(3)
device.press('KEYCODE_DPAD_DOWN') # Down
time.sleep(3)

# Take another Screenshot
result2 = None
while result2 == None:
  result2 = device.takeSnapshot()

print "Value of result2 Screenshot object"
print result2
result2.writeToFile('./SB/screens/screen2.png','png')


Save monkeyrunner_job1.py - now, to actually run it and see each item run line by line (great for debugging) execute the following command at a shell prompt:

$ <android_sdk_path>/tools/monkeyrunner <  monkeyrunner_job1.py

I hope you find this useful!

If you enjoyed this post, send us = kudos = 
(Bitcoin Addr: 19n6q3GZfoM64oqv5HsDnhzqvcEvJUvmdx)



For more information about writing Android applications in Java, check out this book on Amazon:



Cheers!

Tuesday, June 14, 2011

Productive Linux Aliases Initialized Following User Authentication

Sup Folks,

I wanted to take this opportunity to share some of my favorite Linux aliases used by many people in the community.  Some may believe that *nix aliases are the spawn del diablo...mostly because it's easy for aliases to get out of hand if typing descriptive, unique, and easy-to-remember aliases aren't your cup o' tea.

Aliases are really helpful if you find yourself typing long commands on a regular basis, kind of like this simple one-liner that displays the types of files located in the current working directory:


$ ls | while read f; do file $f; done

The small list of aliases found below certainly increase productivity because, well...less keys are required to be pressed on your happy hacker keyboard. 

Setting up aliases in the manner described below will have them initialized following user authentication into your favorite Linux distro (Some of the aliases below are specific to distros with apt available as the package manager):

1. Edit ~/.bashrc and add the following line at the very bottom of the file:


$ source /etc/profile.d/aliases.sh

Save your changes and exit.

2. Create /etc/profile.d/aliases.sh and add the following:

#!/bin/bash
alias acache="apt-cache search"
alias ainstall="apt-get install"
alias aremove="apt-get remove"
alias aupdate="apt-get update"
alias aupgrade="apt-get upgrade"
alias adupgrade="apt-get dist-upgrade"
alias cless="/usr/share/vim/vim72/macros/less.sh"
alias dadd="darcs add"
alias doupgrade="/usr/bin/do-release-upgrade -d"
alias dpull="darcs pull"
alias dpush="darcs push"
alias drecord="darcs record"
alias grep="grep --color=auto"
alias jgeminstall="jruby -S gem install"
alias kpid="kill -9"
alias kall="killall -9"
alias ls="ls --color=auto"
alias ll="ls -l --color=auto"
alias lh="ls -lh --color=auto"
alias netpain="netstat -pane | grep -e udp -e tcp | less"
alias prep="ps -ef | grep"
alias sup="sudo su -"
alias s_rhost="ssh <user>@<rhost>" # Replace <user> with remote ssh user and <rhost> with remote hostname of SSH server.  Replace the alias name with the same <rhost> hostname.

3. Setup executable permissions on /etc/profile.d/aliases.sh.  Not even sure this is necessary due to the way this file is executed from /etc/profile but I do it any way:

$ chmod 755 /etc/profile.d/aliases.sh


4. Log out and log back in...

That's it!

If you enjoyed this post, send us = kudos = 
(Bitcoin Addr: 19n6q3GZfoM64oqv5HsDnhzqvcEvJUvmdx)




For more info on UNIX and Linux system administration, check out this book:



If you have any other aliases that have personally helped you, feel free to post them as comments to help everyone else!

Lat3r sk4t3rz :p