glennji.com

Because life's too short to do it the RIGHT way

Tips and Tricks

Stuff I've stumbled upon that turned out to be quite handy.
Nov 08

Webcam snapshot on login and unlock (Ubuntu 10.10)

And this is why I love GNU/Linux: with a few minutes and the Internet, I've just hacked up a way to take a webcam snapshot every time I login and unlock the PC. The best way to keep track of Movember, if nothing else!

First I created a script to take the snapshot and put it in ~/.local/bin/faces/take-snapshot.sh

  1. #!/bin/sh
  2. # Take a snapshot with the webcam
  3. MYDIR="$( cd "$( dirname "$0" )" && pwd )"
  4.  
  5. BASEFILENAME="$MYDIR/`date +%Y-%m-%d-%T`"
  6. JPEGFILE="$BASEFILENAME.jpeg"
  7. JPGFILE="$BASEFILENAME.jpg"
  8. PNGFILE="$BASEFILENAME.png"
  9.  
  10. # Say cheese!
  11. streamer -o $JPEGFILE
  12. mv $JPEGFILE $JPGFILE
  13. rm $MYDIR/latest.jpg
  14. ln -s $JPGFILE $MYDIR/latest.jpg
  15.  
  16. # Create a dot face file
  17. rm $MYDIR/*.png 2> /dev/null
  18. convert -crop 200x200+0+0 -gravity Center -resize 128x128 $JPGFILE $PNGFILE
  19. rm ~/.face
  20. cp $PNGFILE ~/.face

Test that a few times -- yep, I'm getting a jpeg in the faces directory, converting (and cropping) to a PNG for the dot-face file used by GDM. This uses the program "streamer" (in the Ubuntu repositories), but you could probably find some other webcam snapshot app if streamer doesn't work for your webcam (it is V4L2 only, I think).

Next, I script the GDM login. GDM already has some scripts that it runs at particular points in the login, so I just extend that. Edit /etc/gdm/PreSession/Default (as root) and add:

  1. if [ -x $HOME/.gdm/PreSession/Default ];
  2. then
  3.     sudo -u $USER $HOME/.gdm/PreSession/Default >> /var/log/boo.log
  4. fi

This extends the GDM scripts to also execute user-specific scripts in my home directory. In my case, I add a script called ~/.gdm/PreSession/Default containing:

  1. $HOME/.local/bin/faces/take-snapshot.sh

Yep, calling the take-snapshot.sh script. chmod u+x ~/.gdm/PreSession/Default and login and a couple of times to make sure it is working -- each time a new picture should be taken, so check the ~/.local/bin/faces directory to make sure.

All that's left is to capture the gnome-screensaver deactivation, and a little Perl script comes to the rescue. Create a file screensaver-watch.pl in the ~/.local/bin/faces directory:

  1. #!/usr/bin/perl
  2. #gnome
  3. my $cmd = "dbus-monitor --session \"type='signal',interface='org.gnome.ScreenSaver',member='ActiveChanged'\"";
  4.  
  5. open (IN, "$cmd |");
  6.  
  7. while (<IN>) {
  8.    if (m/^\s+boolean false/) {
  9.       system("bash /home/glennji/.local/bin/faces/take-snapshot.sh");
  10.    }
  11. }

Yep, this watches DBUS for the gnome-screensaver signal, and runs take-snapshot.sh again. (You'll need to edit -- it's got my home dir hardcoded.)

Final step is to make sure screensaver-watch.pl is started on every login. The easiest way I could think of was to create a file called Screensaver Snapshot.desktop in ~/.config/autostart with the following contents:

  1. [Desktop Entry]
  2. Version=1.0
  3. Type=Application
  4. Terminal=false
  5. Icon[en_US]=eog
  6. Exec=/home/glennji/.local/bin/faces/screensaver-watch.pl
  7. Name[en_US]=Screensaver Snapshot
  8. Name=Screensaver Snapshot
  9. Icon=eog

(Again, hardcoded paths. Some things are too important to do the right way after all.)

Logout, login, check the dir, lock screen, unlock and check again. Woooo!

Nov 04

Remove Evolution from the Ubuntu 10.10 indicator panel

Found this all over teh intertubes, but just in case I forget...

  1. sudo apt-get remove evolution-indicator
  2. killall gnome-panel

Oct 06

Detecting USB media players (Rhythmbox, Banshee, Amarok)

The GNU/Linux music players can detect USB devices as media-players and playback from them, just by adding a little metadata to the devices' filesystem. Create a file in the root of the filesystem named .is_music_player with the following content:

  1. audio_folders=Music/

Restart Rhythmbox, Banshee or Amarok and you're done!

Oct 29

Mount an ISO as a DVD drive

To mount an ISO image, use the loopback device:

sudo mount -o loop somedvd.iso /media/fakedvd

Now you can point any DVD programs at /media/fakedvd rather than /dev/dvd, for example.

May 12

Password-less logins with PAM

Sometimes you want certain to be able to log in to certain user accounts without a password -- for a guest account, for example. It's not terribly secure, sure, but sometimes you don't need that security. Read on for how to do this on any GNU/Linux OS that uses PAM and GDM.

To enable password-less logins for any operating system that uses PAM and GDM, edit /etc/pam.d/gdm to add the following line:

   auth    sufficient      pam_listfile.so sense=allow file=/etc/passwordless item=user
   
   @include common-auth
   ... 

Then create the /etc/passwordless file (readable only by root). List each user you want to login without a password, one per line.

If you are using the GDM autologin feature, add the line "@include common-pamkeyring" to /etc/pam.d/gdm-autologin. I haven't tried it, but this is reported to allow the no-password user wireless access without entering the keyring password.

If not, you need a script like the following to your session startup:

   #!/bin/sh
   exec echo -n "MyKeyringPassword" | /usr/lib/libpam-keyring/pam-keyring-tool -u -s

However, pam-keyring-tool is not included in Ubuntu 7.10, so you need to compile it.