Well I’m doing a great job of avoiding my homework by writing these posts… Anyway, I wrote a couple Bash scripts this weekend to automate some common tasks involved with my site. The first one was to automate processing and updating of my chat logs. I run Xchat2 as an IRC client on my channel #the_end on irc.cyberarmy.net and keep logs. Every day, I run the IRC stats analyzer pisg and then FTP the resultant HTML file to my chat stats page. With the help of a cronjob and a few Bash commands, however, I no longer have to remember to do that little annoyance myself. Here it is:
#!/bin/bash
FTPSERVER='ftp.themikecam.com'
FTPUSER='username'
FTPPASS='password'
SOURCE='/data/mikecam/public_html/newmikecam/chatstats/index.html'
DEST='/public_html/newmikecam/chatstats/index.html'
echo -e "\nRunning pisg to analyze stats...\n"
/data/mikecam/utils/chatstats/pisg;
echo -e "Running ftp to upload stats page...\n"
ftp -n $FTPSERVER <<EOF
user $FTPUSER $FTPPASS
put $SOURCE $DEST
quit
EOF
echo "Done!"
exit 0
The only difficult/annoying part of this was figuring out how to pass commands to ftp once it was running. I couldn’t use echo because that would wait until FTP finished running to execute. I couldn’t manage to get the right search terms to find a solution on Google, but luckily Kyle came to my rescue again, making a suggestion that led me to the magic of Here Documents, which let me stream commands to FTP while it was running. Now I just have to figure out either how to make pisg only process the past week or two or write something to lop old days off of the current xchat log.
Woops, forgot the crontab! Here ’tis, every night at midnight:
0 0 * * * /data/mikecam/utils/chatstats.sh
My second script is to grab a webcam image and upload it. I’ve been using Camorama, which is a very nice little Gnome program to view a webcam and automate capture and FTP upload. Unfortunately, it suffers from a few deficiencies, including a lack of flexibility on image size (only choices are “small,” “medium,” and “large”) and on capture interval (it is in minutes rather than seconds). I also had come up with the idea of using my ASU web space to store my cam so I wouldn’t have to worry about using up transfer here on the MikeCam and could decrease the interval between shots. ASU, however, only permits secure FTP connections to manage files, so I had to write a script to use one of the already available capture scripts and the sftp program. Ta-da:
#!/bin/bash
export FTPUSER='username'
export FTPSERVER='general.asu.edu'
export FTPDIR='www'
export FTPPASS='password'
export CAMFILE='webcam/webcam.jpg'
while [ 1 ]; do
vidcat -d /dev/video0 -f jpeg -o $CAMFILE -q 60 -s 320x240
expect <<'END'
set timeout -1
match_max 1000000
spawn sftp $env(FTPUSER)@$env(FTPSERVER):$env(FTPDIR)/
expect "password: "
send -- "$env(FTPPASS)\r"
expect "sftp> "
send -- "put $env(CAMFILE)\r"
expect "sftp> "
send -- "quit\r"
END
sleep 5.1;
done
exit 0
A couple challenges here: First, finding a capture script that I could use to just capture and let me handle the transferring manually. I went with w3cam for now, but hopefully I can find something, either a replacement or an additional program, that will allow me to easily overlay text on the image so I can add “themikecam.com” and a timestamp. The second challenge was how to deal with SFTP. At first I thought I could do the same as FTP, but the SFTP program doesn’t allow you to pass it a password through an argument, nor does it allow you to use run it and use the “open” and “user” commands, so I had to figure out how to give it the password when it prompted for it. I searched on Google a bit, found someone who had posted about a similar problem, and someone suggested the “expect” program, which lets you wait until you get a certain output before continuing. As you can see, I used it to wait until it prompts for the password, then wait until it authenticates, then wait until the file is transferred. There’s also a loop to capture and upload the cam image every 5.1 seconds. I should probably rewrite the loop so that it doesn’t disconnect and reconnect every time.