pyLiMaSound

When life gives you lemons, make lemonade. This small application is my first real need to use the python language, and that makes me excited. Furthermore Ubuntu and Debian, in the desktop varieties, appear to come standard with PyGTK (your mileage may vary).

My main issue was that I needed to remotely adjust my OS X workstation’s sound, and I figured a control would be easier than opening a shell and running a script. Basically this will give you a small gtk.VScale widget (a vertical slider widget used to select a value from a range) in a resizable window. When everything is all setup I also run it using AllTray so that it can be docked into the system tray.

pylimasound-001.jpg
pylimasound-002.jpg

When a value is selected using the widget it passes the value off to rsh, which in turn sends the command to a remote workstation (the command I have prefilled into the script works on OS X’s main volume):

rsh -l remote_username mac_workstation.fqdn 
"osascript -e 'set Volume 4.6'"

The following, in the intended deployment requires password-less key based ssh connections between the workstation intended to remotely adjust the sound (any platform that supports PyGTK) and the Macintosh workstation:

#!/usr/bin/env python
#pyLiMaSound.py
 
import os
import gtk
import pygtk
 
pygtk.require("2.0")
 
user="remote_username"
host="mac_workstation.fqdn"
 
def scale_set_default_values(scale):
    scale.set_update_policy(gtk.UPDATE_DELAYED)
    scale.set_digits(1)
    scale.set_value_pos(gtk.POS_BOTTOM)
    scale.set_draw_value(True)
 
class pyLiMaSound:
    def delete_event(self, widget, event, data=None):
        print "delete event occurred"
        return False
 
    def destroy(self, widget, data=None):
        print "destroy signal occurred"
        gtk.main_quit()
 
    def rsh_vol_cmd(self, data):
        payload = "rsh -l " + user + " " + host 
            + " "osascript -e 'set Volume " + 
            str(data.value) + "'""
	print payload
        os.system(payload);
 
    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_default_size(50, 200)
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)
        self.window.set_border_width(10)
 
        adj1 = gtk.Adjustment(0.0, 0.0, 10.1, 0.1, 0.1, 0.1)
	adj1.connect("value_changed", self.rsh_vol_cmd)
 
        self.vscale = gtk.VScale(adj1)
        scale_set_default_values(self.vscale)
        self.window.add(self.vscale)
        self.vscale.show()
        self.window.show()
 
    def main(self):
        gtk.main()
 
if __name__ == "__main__":
    control = pyLiMaSound()
    control.main()

ImageMagick Magic!

I enjoy using ImageMagick… most of it has been via PHP CLI, however here we have a short example in Bash:

#!/bin/bash
for file in `ls -d *.png` ; do
	convert -crop 1280X985+0+24 $file 
	$(basename $file .png).jpg;
done

Basically this does the following:

  • Makes a listing of all .png files in the current directory
  • Runs the “convert” command (part of ImageMagick) on each .png file in that listing

Client to Server Backups

I have intentions to look into Bacula as a disk-to-disk backup solution, however until then I am using the following script for Kubuntu Dapper -> Debian Etch backups, and a modified one for Windows XP Home -> Debian Etch.

It all uses rsync, and requires password-less key based ssh connections. I am using rsync, OpenSSH, and for Windows XP a little Cygwin magic (including modifying this script to make it Windows friendly).

client-to-server-rsync-backup.sh

#!/bin/bash
#ver 1.0.3
 
BACKUPSERVERUSER=limitedUserOnServer
BACKUPSERVER=serverHostName
BACKUPCLIENT=`echo $(hostname -s) | tr '[:upper:]' '[:lower:]'`
BACKUPS=(
  #localFolder,backupDestinationParentFolder
  #/home,/media/md2/backups/clientname
  #/var/www,/media/md2/backups/clientname/var
)
 
if [ "$1" != "--incremental" ] && [ "$1" != "--sync" ] && [ "$1" != "--help" ]; then
  echo $0: missing operand
  echo Try '$0 --help' for more information.
elif [ "$1" = "--help" ]; then
  echo " Usage: $0 [OPTION]... [PASSTHROUGH OPTIONS]";
  echo "";
  echo "   Mandatory arguments:";
  echo "     [OPTION]";
  echo "       --incremental   add to the existing backups";
  echo "       --sync    sync the live data with the existing backup";
  echo "";
  echo "   Optional arguments:";
  echo "     [PASSTHROUGH OPTIONS]";
  echo "       Passthrough options are sent to rsync in this form from this script:";
  echo "";
  echo "       for incremental:";
  echo '         sudo rsync -avz [PASSTHROUGH OPTIONS] $SOURCE $BKUPSRVUSER@';
  echo "         $BKUPSRV:$DEST";
  echo "";
  echo "       and for sync:";
  echo '         sudo rsync -avz [PASSTHROUGH OPTIONS] --delete $SOURCE ';
  echo "         $BKUPSRVUSER@$BKUPSRV:$DEST";
  echo "";
  echo "       * This makes it easy to do something like this:";
  echo "       *   $0 --incremental "--partial --progress"";
  echo "       *";
  echo "       * Thus sending "--partial --progress" to rsync,";
  echo "       * making this script a little more interactive...";
  echo "       * read more via `man rsync'.";
else
  if [ "$1" = "--incremental" ]; then
    incremental_or_sync="";
  elif [ "$1" = "--sync" ]; then
    incremental_or_sync="--delete";
  fi
 
  for BACKUPS in ${BACKUPS[@]}; do
    SOURCE=`echo $BACKUPS | cut -d',' -f1`
    DESTINATION=`echo $BACKUPS | cut -d',' -f2`
    echo
    echo "# starting backup"
    echo "# from: $BACKUPCLIENT:$SOURCE"
    echo "# to: $BKUPSRV:$DEST"
    echo
    echo sudo rsync -avz $2 $inc_or_sync $SOURCE $BKUPSRVUSER@$BKUPSRV:$DEST
    sudo rsync -avz $2 $inc_or_sync $SOURCE $BKUPSRVUSER@$BKUPSRV:$DEST
    echo
  done
fi

Windows CopSSH Clear

On a default install of CopSSH 1.4.2 I noticed that you cannot run “clear” after launching “Start a Unix BASH Shell”.

Type in “cmd /c edit” to open the MS-DOS Editor and enter the following:

#!/bin/bash
cmd /c cls

Save the file as “clear” and exit the editor. Back in the shell, try running “./clear” and you should receive pleasant results.

wavtomp3

Here’s a quick little bash shell script to get your current working directory full of .wav’s converted to .mp3′s using lame.

#!/bin/bash
for i in *.wav; do
     lame -b 256 -q 0 -s 44.1  "$i" "`basename "$i" .wav`".mp3
done

Pinglog

The ability to ping a host can be such a useful tool in establishing network connectivity. Being able log such activity is quite valuable also. Below is a short bash script that can assist you in those endeavors.

#!/bin/bash
logname=`date "+%Y-%m-%d-%H-%M-%S"`-$1.log
if [ "$1" = "-help" ]; then
   echo "Usage: pinglog.sh [HOST]"
   echo "Usage: pinglog.sh [OPTION] [LOGFILE] [HOST]"
   echo "Log pings of the HOST."
   echo ""
   echo "[HOST]   | shortname of the station to be logged."
   echo "[OPTION] | one option available. --missed-pings"
   echo ""
   echo "-missed-pings     shows the date and time a ping was"
   echo "                  missed from a generated log file"
   echo ""
   echo "Usage examples: pinglog.sh wiretop"
   echo "                pinglog.sh -missed-pings"
   echo "                   2007-04-10-02-12-22-wiretop.log wiretop"
   echo ""
   echo "pinglog.sh will log the ping information into the same"
   echo "directory"
   echo "that it was started from in a file called 'ping.log'"
elif [ "$1" = "-missed-pings" ]; then
   cat $2 | grep -v $3
elif [ -z "$1" ]; then
   echo "pinglog.sh: too few arguments"
   echo "Try 'pinglog.sh -help' for more information."
else
   while true; do
      echo "$(date) $(ping -c 1 $1 | grep time=)" >> $logname
      sleep 1
   done
fi

Cron crontab window windowed gui app application

The title shows all of the different terms I was googling for… and what was near impossible to find, I finally found.

Here I am to share share my new found knowledge with you. Do you want to run a windowed application with cron? The basics are that you have to export your display.

Yeah, I read that in other places. But where? I thought to myself… Some said, “IN YOUR CRONTAB FILE” Yeah, okay, whatever that means. I would like some more detail. As they say, the devil is in the details. So, with detailed instructions I continue.

If you have your cron permissions setup properly and run:

crontab -e

you can set a cron job that looks something like:

0,30 * * * * export DISPLAY=:0 && killall skype && sleep 5 && skype

So what does this mean? It means, every 30 minutes, run the following commands:

export DISPLAY=:0
killall skype
sleep 5
skype

Okay, difficult to explain… but basically as I understand it, it means: When you run the cron job, it is in a terminal environment. It cannot find the X Windows display without setting the DISPLAY variable within the command string for the cron job.

So, as a recap, set your display variable within the command string of your cron job. :-)

Exch for Linux

I just uploaded Exch for Linux. It was excitingly difficult to make a tar.gz file off of the command line because Ark wasn’t playing nice on Kanotix.

For those who would like to make a tar.gz file of an entire directory, here’s how:

tar -cvf filename.tar directoryName

And then issue this command:

gzip filename.tar

Voila, you should have:

filename.tar.gz