<< snuffbunny<< crotch cam
Crotch cam technical notes
[ Intro | SHS keys | Shell script | Post copy script | PHP image generator ]

Introduction
The Crotch cam is running on an SGI O2. Finally I found a use for this machine.

There were a things to set up and get used to:

 

SSH keys, for those hard to reach places
This was the interesting one. Since our network philosophy is based on paranoia, I could not get any easy access to the webserver machine. However, I could if I used ssh. Not having used ssh before though, I had to understand how it works.

First, you need to make yourself a set of private and public keys. If you do not already have a .ssh directory in your home directory, go and make one. The go there and make your keys:
ssh-keygen -b 1024 -f mykey -t dsa
LEAVE THE PASSWORD BLANK so you can use the key from a script. If your authentication setup does not use dsa, then use the other type. Some versions of ssh-keygen use a -d flag which is equivalent to -t dsa.

Ok, this will generate you keys. These keys could be anywhere but it seems like a good idea to keep your keys in one place.

You will be loging in to the remote machine as a particular user, say user. Now, copy the public key ONLY in ~/user/.ssh on the remote machine. Then depending on the type of key you generated, append the public key to the authorized_keys (non dsa keys) or authorized_keys2 (dsa keys) file. These files may or may not be there. If you went to the remote machine, via ssh, then they should be there (duh!).

Now you should be ready to try loging in from the local machine to the webserver, try:
ssh -i ~/.ssh/mykey user@remotehost
it should have logged you in provided your ssh setup allows no-password keys. If any this did not work, I cannot help, read about ssh, search
Google groups.

 

The Shell script
At some point the video deamon would get so stuffed up, it started taking over a minute (!) to run the script instead of a few seconds, a reboot fixed that (ok, I was angry enough already to look for the deamon). The cron delay is what made me make the refresh rate 3 minutes, even though I would have prefered something much much shorter. May try shorter in a few days. The same delay made me use the $$ process id variable to prefix the base name of the work files. This way, even if many scripts are running conqurently, they will not randomly delete each other's working files. That's an idea I got from the cgi-bin script that came with the machine.

For those of use that have an SGI O2 or similar and want to use my script, here it is. Some commands may need their full path to run in the limited "cron" environment. I have changed all user and server names to protect the innocent. Use it as you please:


#! /usr/freeware/bin/bash

#camera="DISABLED"
camera="ENABLED"
#scaling="DISABLED"
scaling="ENABLED"

remote_img=webcam_work.jpg
local_img=webcam_now.jpg
basename=$$webcam_work
off_pic="camera_off.rgb"

# come here
cd /home/user/webcam_script_dir
# start date
echo "$$ started:  "`date` > last_snap.txt

if [ $camera = "ENABLED" ]
then
    # grab
    /usr/sbin/vidtomem -f $basename >/dev/null 2>&1
    if [ -r $basename-00000.rgb ]
    then
	sgipic=$basename-00000.rgb
    else
	sgipic=$off_pic
    fi
else
    # use camera-off image if we are disabled
    sgipic=$off_pic
fi


# scale and save the grab to a pnm
if [ $scaling = "ENABLED" ]
then
    sgitopnm $sgipic 2>/dev/null | pnmscale -xsize 320 > $basename.pnm 2>/dev/null
else
    sgitopnm $sgipic 2>/dev/null > $basename.pnm 2>/dev/null
fi
# make a jpeg
/usr/sbin/imgcopy -q75 -fJFIF $basename.pnm $basename.jpg 2>/dev/null
# delete the work and grabbed images
rm -f $basename.pnm $basename-00000.rgb 2>/dev/null

# ------------------------------
# We use scp for the file transfer
# and ssh to run the post copy script
# copy   : copies current image
# finish : renames the current image to the final name
# ------------------------------
scp -i /home/user/.ssh/mykey $basename.jpg user@remotehost:public/$remote_img
ssh -i /home/user/.ssh/mykey user@remotehost s/finish_camera.sh

# ------------------------------
# local mirror (optional)
# ------------------------------
cp $basename.jpg /usr/local/$local_img 2>/dev/null

# delete the final image
rm -f $basename.jpg 2>/dev/null

# finished date
echo "$$ finished: "`/sbin/date` >> last_snap.txt

 

Post copy script
This is very simple but I am including it for completion:


#!/bin/sh
cd /home/user/public 2>/dev/null
mv -f webcam_work.jpg webcam_now.jpg 2>/dev/null

 

PHP image generator
We have php on our webserver so I used it to make sure the image refreshes properly. Note the camera OFF image. It comes up if no camera file is available. One could make it come up if the camera file has not been updated for a while

/*
 * FILE: conf.inc
 *
 * Config file for the webcam image filenames
 * I use a config file since these variables
 * need to be used by other scripts that utilise
 * the webcam image
 */
$cam_off	= "camera_off.jpg";
$cam_now	= "webcam_now.jpg";
$refresh_t	= "180";
$CAM_DIR        = ".";
$cam_src        = (file_exists($CAM_DIR."/".$cam_now)
                  ? $CAM_DIR."/".$cam_now
                  : $cam_off);

/* * FILE: vid.php * * PHP Script for streaming out the webcam picture * making sure caching is disabled */ include("./conf.inc"); Header("Content-type: image/jpeg"); //mime header("Content-Length: " . filesize($cam_src)); //data length header("Pragma: no-cache"); header("Expires: 0"); readfile($cam_src);

 

[ Intro | SHS keys | Shell script | Post copy script | PHP image generator ]